public bool Down() { // If there's no block below the current one, go 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)) { // Hide the block (in the previous position) Hide(GameField.WinHandle); // Update the block position 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 the block in the new position Show(GameField.WinHandle); return(true); } else { // If there's a block below the current one, doesn't go down // -> put it on the array that controls the game and return FALSE 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); } }
public bool Left() { // If there's no block to the left of the current one, go left 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)) { // Hide the block (in the previous position) Hide(GameField.WinHandle); // Update the block position 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 the block in the new position Show(GameField.WinHandle); return(true); } else { // If there's a block to the left of the current one, // doesn't go left and return FALSE return(false); } }
public void Rotate() { // Store the current block position Point OldPosition1 = square1.Location; Point OldPosition2 = square2.Location; Point OldPosition3 = square3.Location; Point OldPosition4 = square4.Location; RotationDirections OldStatusRotation = StatusRotation; Hide(GameField.WinHandle); // Rotate the blocks switch (BlockType) { case BlockTypes.Square: // Do nothing. Squares don't rotate. break; case BlockTypes.Line: // Rotate all squares around square 2 switch (StatusRotation) { case RotationDirections.North: StatusRotation = RotationDirections.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 RotationDirections.East: StatusRotation = RotationDirections.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: // Rotate all squares around square 3 switch (StatusRotation) { case RotationDirections.North: StatusRotation = RotationDirections.East; square1.Location = new Point(square3.Location.X, square3.Location.Y - squareSize); square2.Location = new Point(square3.Location.X + squareSize, square3.Location.Y); square4.Location = new Point(square3.Location.X + 2 * squareSize, square3.Location.Y); break; case RotationDirections.East: StatusRotation = RotationDirections.South; square1.Location = new Point(square3.Location.X + squareSize, square3.Location.Y); square2.Location = new Point(square3.Location.X, square3.Location.Y + squareSize); square4.Location = new Point(square3.Location.X, square3.Location.Y + 2 * squareSize); break; case RotationDirections.South: StatusRotation = RotationDirections.West; square1.Location = new Point(square3.Location.X, square3.Location.Y + squareSize); square2.Location = new Point(square3.Location.X - squareSize, square3.Location.Y); square4.Location = new Point(square3.Location.X - 2 * squareSize, square3.Location.Y); break; case RotationDirections.West: StatusRotation = RotationDirections.North; square1.Location = new Point(square3.Location.X - squareSize, square3.Location.Y); square2.Location = new Point(square3.Location.X, square3.Location.Y - squareSize); square4.Location = new Point(square3.Location.X, square3.Location.Y - 2 * squareSize); break; } break; case BlockTypes.L: // Rotate all squares around square 3 switch (StatusRotation) { case RotationDirections.North: StatusRotation = RotationDirections.East; square1.Location = new Point(square3.Location.X + squareSize, square3.Location.Y); square2.Location = new Point(square3.Location.X + 2 * squareSize, square3.Location.Y); square4.Location = new Point(square3.Location.X, square3.Location.Y + squareSize); break; case RotationDirections.East: StatusRotation = RotationDirections.South; square1.Location = new Point(square3.Location.X - squareSize, square3.Location.Y); square2.Location = new Point(square3.Location.X, square3.Location.Y + squareSize); square4.Location = new Point(square3.Location.X, square3.Location.Y + 2 * squareSize); break; case RotationDirections.South: StatusRotation = RotationDirections.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 RotationDirections.West: StatusRotation = RotationDirections.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: switch (StatusRotation) { case RotationDirections.North: StatusRotation = RotationDirections.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 RotationDirections.East: StatusRotation = RotationDirections.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 RotationDirections.South: StatusRotation = RotationDirections.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 RotationDirections.West: StatusRotation = RotationDirections.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: // Rotate all squares around square 2 switch (StatusRotation) { case RotationDirections.North: StatusRotation = RotationDirections.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 RotationDirections.East: StatusRotation = RotationDirections.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: // Rotate all squares around square 2 switch (StatusRotation) { case RotationDirections.North: StatusRotation = RotationDirections.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 RotationDirections.East: StatusRotation = RotationDirections.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; } // After rotating the squares, test if they overlap other squares. // If so, return to original position 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))) { StatusRotation = OldStatusRotation; square1.Location = OldPosition1; square2.Location = OldPosition2; square3.Location = OldPosition3; square4.Location = OldPosition4; } Show(GameField.WinHandle); }