Esempio n. 1
0
 /// <summary>
 /// Starts a new game of Tetris
 /// </summary>
 public void StartGame()
 {
     FixedPieces.Clear();
     MovingPiece = TetrisPiece.GetRandomPiece();
     FuturePiece = TetrisPiece.GetRandomPiece();
     Score       = 0;
     Timer.Start();
 }
Esempio n. 2
0
        /// <summary>
        /// Moves on to the next piece
        /// </summary>
        private void NextPiece()
        {
            PrevPosition.Clear();
            FixedPieces.Add(MovingPiece);
            MovingPiece = FuturePiece;
            FuturePiece = TetrisPiece.GetRandomPiece();

            CheckFilledRows();
        }
Esempio n. 3
0
        /// <summary>
        /// Shifts the moving piece 1 square right, if possible
        /// </summary>
        private void MovePieceRight()
        {
            List <Square> squares = MovingPiece.OccupiedSquares;

            // Can't move out of bounds
            if (squares.Any(ms => ms.X == Width - 1))
            {
                return;
            }
            // Can't move into an already occupied square
            else if (FixedPieces.Any(fp => fp.OccupiedSquares.Any(fs => squares.Any(ms => ms.X + 1 == fs.X && ms.Y == fs.Y))))
            {
                return;
            }

            MovingPiece.MoveRight();
        }
Esempio n. 4
0
        /// <summary>
        /// Finds all rows that are completely filled and removes them
        /// </summary>
        private void CheckFilledRows()
        {
            for (int y = 0; y < Height; y++)
            {
                int filled = FixedPieces.Sum(fp => fp.OccupiedSquares.Where(fs => fs.Y == y).Count());
                if (filled == Width)
                {
                    // Clear row
                    foreach (TetrisPiece piece in FixedPieces)
                    {
                        piece.EraseRow(y);
                    }

                    ErasedRows.Add(y);
                    Score++;
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Rotates the moving piece
        /// </summary>
        private void RotatePiece()
        {
            List <Square> afterRotation = MovingPiece.SimulateRotation();

            // Can't move out of bounds
            if (afterRotation.Any(ms => ms.X < 0) || afterRotation.Any(ms => ms.X >= Width) ||
                afterRotation.Any(ms => ms.Y < 0) || afterRotation.Any(ms => ms.Y >= Height))
            {
                return;
            }
            // Can't move into an already occupied square
            if (FixedPieces.Any(fp => fp.OccupiedSquares.Any(fs => afterRotation.Any(rs => rs.X == fs.X && rs.Y == fs.Y))))
            {
                return;
            }

            MovingPiece.Rotate();
        }