/*移动*/ public bool down() { //检测是否可以下移 if (GameField.isEmpty(square1.location.X / squareSize, square1.location.Y / squareSize + 1) && GameField.isEmpty(square2.location.X / squareSize, square2.location.Y / squareSize + 1) && GameField.isEmpty(square3.location.X / squareSize, square3.location.Y / squareSize + 1) && GameField.isEmpty(square4.location.X / squareSize, square4.location.Y / squareSize + 1)) { Erase(GameField.winHandle); square1.location = new Point(square1.location.X, square1.location.Y + squareSize); square2.location = new Point(square2.location.X, square2.location.Y + squareSize); square3.location = new Point(square3.location.X, square3.location.Y + squareSize); square4.location = new Point(square4.location.X, square4.location.Y + squareSize); Draw(GameField.winHandle); return(true); } else //如果不能下移了 { GameField.stopSquare(square1, square1.location.X / squareSize, square1.location.Y / squareSize); GameField.stopSquare(square2, square2.location.X / squareSize, square2.location.Y / squareSize); GameField.stopSquare(square3, square3.location.X / squareSize, square3.location.Y / squareSize); GameField.stopSquare(square4, square4.location.X / squareSize, square4.location.Y / squareSize); return(false); //表示可以弹出下一个block了 } }
public bool right() { //检测是否可以右移 if (GameField.isEmpty(square1.location.X / squareSize + 1, square1.location.Y / squareSize) && GameField.isEmpty(square2.location.X / squareSize + 1, square2.location.Y / squareSize) && GameField.isEmpty(square3.location.X / squareSize + 1, square3.location.Y / squareSize) && GameField.isEmpty(square4.location.X / squareSize + 1, square4.location.Y / squareSize)) { Erase(GameField.winHandle); square1.location = new Point(square1.location.X + squareSize, square1.location.Y); square2.location = new Point(square2.location.X + squareSize, square2.location.Y); square3.location = new Point(square3.location.X + squareSize, square3.location.Y); square4.location = new Point(square4.location.X + squareSize, square4.location.Y); Draw(GameField.winHandle); return(true); } else //如果不能右移了 { return(false); } }
/*旋转block*/ public void Rotate() { //保存每个小块的位置 Point oldPosition1 = square1.location; Point oldPosition2 = square2.location; Point oldPosition3 = square3.location; Point oldPosition4 = square4.location; //保存当前的方向 RotateDirections oldRotation = myRotation; //开始试着旋转方块,旋转方向:顺时针方向 旋转中心点为形状拐点 Erase(GameField.winHandle); switch (blockType) { case BlockTypes.square: break; case BlockTypes.line: //直线的旋转只有两种方向 switch (myRotation) { case RotateDirections.North: myRotation = RotateDirections.East; square1.location = new Point(square2.location.X - squareSize, square2.location.Y); square3.location = new Point(square2.location.X + squareSize, square2.location.Y); square4.location = new Point(square2.location.X + 2 * squareSize, square2.location.Y); break; case RotateDirections.East: myRotation = RotateDirections.North; square1.location = new Point(square2.location.X, square2.location.Y - squareSize); square3.location = new Point(square2.location.X, square2.location.Y + squareSize); square4.location = new Point(square2.location.X, square2.location.Y + 2 * squareSize); break; } break; case BlockTypes.J: //J形方块有四种旋转方向 switch (myRotation) { case RotateDirections.North: myRotation = RotateDirections.East; square1.location = new Point(square3.location.X + 2 * squareSize, square3.location.Y); square2.location = new Point(square3.location.X + squareSize, square3.location.Y); square4.location = new Point(square3.location.X, square3.location.Y - squareSize); break; case RotateDirections.East: myRotation = RotateDirections.South; square1.location = new Point(square3.location.X, square3.location.Y + 2 * squareSize); square2.location = new Point(square3.location.X, square3.location.Y + squareSize); square4.location = new Point(square3.location.X + squareSize, square3.location.Y); break; case RotateDirections.South: myRotation = RotateDirections.West; square1.location = new Point(square3.location.X - 2 * squareSize, square3.location.Y); square2.location = new Point(square3.location.X - squareSize, square3.location.Y); square4.location = new Point(square3.location.X, square3.location.Y + squareSize); break; case RotateDirections.West: myRotation = RotateDirections.North; square1.location = new Point(square3.location.X, square3.location.Y - 2 * squareSize); square2.location = new Point(square3.location.X, square3.location.Y - squareSize); square4.location = new Point(square3.location.X - squareSize, square3.location.Y); break; } break; case BlockTypes.L: //L形方块有四种旋转方向 switch (myRotation) { case RotateDirections.North: myRotation = RotateDirections.East; square1.location = new Point(square3.location.X + 2 * squareSize, square3.location.Y); square2.location = new Point(square3.location.X + squareSize, square3.location.Y); square4.location = new Point(square3.location.X, square3.location.Y + squareSize); break; case RotateDirections.East: myRotation = RotateDirections.South; square1.location = new Point(square3.location.X, square3.location.Y + 2 * squareSize); square2.location = new Point(square3.location.X, square3.location.Y + squareSize); square4.location = new Point(square3.location.X - squareSize, square3.location.Y); break; case RotateDirections.South: myRotation = RotateDirections.West; square1.location = new Point(square3.location.X - 2 * squareSize, square3.location.Y); square2.location = new Point(square3.location.X - squareSize, square3.location.Y); square4.location = new Point(square3.location.X, square3.location.Y - squareSize); break; case RotateDirections.West: myRotation = RotateDirections.North; square1.location = new Point(square3.location.X, square3.location.Y - 2 * squareSize); square2.location = new Point(square3.location.X, square3.location.Y - squareSize); square4.location = new Point(square3.location.X + squareSize, square3.location.Y); break; } break; case BlockTypes.T: //T形方块也有四种旋转方向 switch (myRotation) { case RotateDirections.North: myRotation = RotateDirections.East; square1.location = new Point(square2.location.X, square2.location.Y - squareSize); square3.location = new Point(square2.location.X, square2.location.Y + squareSize); square4.location = new Point(square2.location.X - squareSize, square2.location.Y); break; case RotateDirections.East: myRotation = RotateDirections.South; square1.location = new Point(square2.location.X + squareSize, square2.location.Y); square3.location = new Point(square2.location.X - squareSize, square2.location.Y); square4.location = new Point(square2.location.X, square2.location.Y - squareSize); break; case RotateDirections.South: myRotation = RotateDirections.West; square1.location = new Point(square2.location.X, square2.location.Y + squareSize); square3.location = new Point(square2.location.X, square2.location.Y - squareSize); square4.location = new Point(square2.location.X + squareSize, square2.location.Y); break; case RotateDirections.West: myRotation = RotateDirections.North; square1.location = new Point(square2.location.X - squareSize, square2.location.Y); square3.location = new Point(square2.location.X + squareSize, square2.location.Y); square4.location = new Point(square2.location.X, square2.location.Y + squareSize); break; } break; case BlockTypes.Z: //Z形方块有两种旋转方向 switch (myRotation) { case RotateDirections.North: myRotation = RotateDirections.East; square1.location = new Point(square2.location.X, square2.location.Y - squareSize); square3.location = new Point(square2.location.X - squareSize, square2.location.Y); square4.location = new Point(square2.location.X - squareSize, square2.location.Y + squareSize); break; case RotateDirections.East: myRotation = RotateDirections.North; square1.location = new Point(square2.location.X - squareSize, square2.location.Y); square3.location = new Point(square2.location.X, square2.location.Y + squareSize); square4.location = new Point(square2.location.X + squareSize, square2.location.Y + squareSize); break; } break; case BlockTypes.S: //S形方块有两种旋转方向 switch (myRotation) { case RotateDirections.North: myRotation = RotateDirections.East; square1.location = new Point(square3.location.X + squareSize, square3.location.Y + squareSize); square2.location = new Point(square3.location.X + squareSize, square3.location.Y); square4.location = new Point(square3.location.X, square3.location.Y - squareSize); break; case RotateDirections.East: myRotation = RotateDirections.North; square1.location = new Point(square3.location.X - squareSize, square3.location.Y + squareSize); square2.location = new Point(square3.location.X, square3.location.Y + squareSize); square4.location = new Point(square3.location.X + squareSize, square3.location.Y); break; } break; } //旋转之后检测位置是否冲突 if (!(GameField.isEmpty(square1.location.X / squareSize, square1.location.Y / squareSize) && GameField.isEmpty(square2.location.X / squareSize, square2.location.Y / squareSize) && GameField.isEmpty(square3.location.X / squareSize, square3.location.Y / squareSize) && GameField.isEmpty(square4.location.X / squareSize, square4.location.Y / squareSize))) {//如果有冲突则回到原来的状态 myRotation = oldRotation; square1.location = oldPosition1; square2.location = oldPosition2; square3.location = oldPosition3; square4.location = oldPosition4; } Draw(GameField.winHandle); }