/// <summary> /// 判断是否移动合法 /// </summary> /// <returns></returns> private Boolean isLegal(Shape tempShape) { Shape ShapeNow = (Shape)tempShape.Clone(); //初步判断是否越界 bool flag = ShapeNow.X >= -2 && ShapeNow.X < TETRIES_WIDTH-1 && ShapeNow.Y >= 0 && ShapeNow.Y < TETRIES_HEIGHT-1; if (!flag) return false; //是否和基地发生碰撞 for (int i = 0; i < 4;i++ ) { for (int j = 0; j < 4;j++ ) { if (ShapeNow.ShapeRegion[4 * i + j] != 0 ) { if (ShapeNow.X + j >= TETRIES_WIDTH || ShapeNow.Y + i >= TETRIES_HEIGHT || ShapeNow.X + j < 0) {//和边界发生碰撞 return false; } if(_map[ShapeNow.Y+i,ShapeNow.X+j]!=0) {//发生碰撞 return false; } } } } return true; }
public void StartGame() { _map = new int[TETRIES_HEIGHT,TETRIES_WIDTH]; // 重新清零/载入开局 _curShape = _sf.refresh(_sf.RandomeCreate()); // 产生一个形状 _nextShape = _sf.refresh(_sf.RandomeCreate()); Socre = 0; }
//根据形状去更新其他的 public Shape refresh(byte[] randomPiece) { KindOfShape temps = (KindOfShape)randomPiece[0]; ShapeDirction tempDir = (ShapeDirction)randomPiece[1]; int Color = randomPiece[2]; //给颜色赋值 Shape shape = new Shape(temps, tempDir,Color); sumofShape[(int)temps]++; reFreshRate(); return shape; }
public bool InputDown() { //判断下移是否成功 Shape tempShapeDown = (Shape)_curShape.Clone(); tempShapeDown.Y++; if (isLegal(tempShapeDown))//下移一格成功 { _curShape.Y++; return true; } else //触底:1.方块变成基地的一部分,消去满行 2.产生新的方块 { AddCurshape(); _curShape = _nextShape; _nextShape = _sf.refresh(_sf.RandomeCreate()); int tempLine = EliminateLines(); Socre = Socre + calSocre(tempLine);//消去满行。累加分数 //判断游戏是否结束 if(!isLegal(_curShape))//不合法,游戏结束 { if(GamerOver!=null) { GamerOver(); } } return false; } }