Exemplo n.º 1
0
        public async Task RunCurrentTetromino()
        {
            //While the tetromino can still move down
            while (CurrentTetromino.CanMoveDown())
            {
                //Wait for the standard delay
                await Delay(standardDelay);

                //Move the tetromino down one row
                CurrentTetromino.MoveDown();

                //Update the display
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Game)));

                //If the tetromino can no longer move down BUT can still move in other directions,
                //delay for an additional half-second to let the user move if they want.
                if (!CurrentTetromino.CanMoveDown() && CurrentTetromino.CanMove())
                {
                    await Delay(500);
                }
            }

            //"Solidify" the current tetromino by adding its covered squares to the board's cells
            this.GameBoard.TakeCells(CurrentTetromino.CoveredCells);
        }
Exemplo n.º 2
0
        //Checks if the current tetromino collides with anything.
        //It uses a virtual clone of the current tetromino in the position that the user wants to move to.
        //If it works out the current tetromino is used to that position, otherwise it doesnt move.
        public bool CanTetroFit(int X, int Y)
        {
            //Clone current tetromino position
            var Clone = CurrentTetromino.Clone();

            if (X == 1)
            {
                Clone.Move("right");
            }
            else if (X == -1)
            {
                Clone.Move("left");
            }
            else if (Y == 1)
            {
                Clone.GravityTick();
            }
            else if (X == 0 && Y == 0)
            {
                Clone.Rotate();
            }
            else if (X == -2 && Y == -2)
            {
                //check spawn
            }

            //Loop through shape-grid to see collission
            for (int row = 0; row < Clone.Shape.Count; row++)
            {
                for (int col = 0; col < Clone.Shape[0].Count; col++)
                {
                    if (Clone.Shape[row][col] == 1)
                    {
                        //check if shape-grid has negative value to prevent out of bounds
                        if (Clone.X + col <= 0)
                        {
                            return(false);
                        }


                        //if collission return false
                        if (GridArea[Clone.Y + row][Clone.X + col] == 1)
                        {
                            return(false);
                        }
                        if (GridArea[Clone.Y + row][Clone.X + col] == 2) //check bounds someway instead
                        {
                            return(false);
                        }
                    }
                }
            }
            //else true
            return(true);
        }
Exemplo n.º 3
0
        private void DropNewTetromino()
        {
            bool gameOver = false;

            if (CurrentTetromino == null)
            {
                tetrominoQueue.Enqueue(GetRandomTetromino());
                CurrentTetromino = GetRandomTetromino();
                CurrentTetromino.Draw();
            }

            else
            {
                CurrentTetromino = tetrominoQueue.Dequeue();
                CurrentTetromino.Draw();

                foreach (var tetrominoOnScreen in tetrominosOnScreen)
                {
                    foreach (var block in tetrominoOnScreen.Blocks)
                    {
                        foreach (var currentblock in CurrentTetromino.Blocks)
                        {
                            if (block.Column == currentblock.Column && block.Row == currentblock.Row)
                            {
                                gameOver = true;
                                StopTimer();
                            }
                        }
                    }
                }

                if (gameOver)
                {
                    StopTimer();
                    //if (MessageBox.Show("Would you like to play again?", "Game Over", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    //{
                    //    StartGame();
                    //}
                    //else
                    //{
                    //    StopTimer();
                    //}
                }

                else
                {
                    tetrominoQueue.Enqueue(GetRandomTetromino());
                }
            }
        }
Exemplo n.º 4
0
 public void TimerTick()
 {
     if (CurrentTetromino.IsAtBottom())
     {
         tetrominosOnScreen.Add(CurrentTetromino);
         ClearCompletedLines();
         DropNewTetromino();
         RedrawBoard();
     }
     else
     {
         if (!IsColliding())
         {
             Drop();
             ClearCompletedLines();
         }
     }
 }
Exemplo n.º 5
0
 //This method moves the current tetromino if the virtual tetromino in CanTetroFit returns true.
 public bool UpdateTetromino(string keyInput)
 {
     if (keyInput == "left" && CanTetroFit(-1, 0))
     {
         CurrentTetromino.Move("left");
     }
     else if (keyInput == "right" && CanTetroFit(1, 0))
     {
         CurrentTetromino.Move("right");
     }
     else if (keyInput == "rotate" && CanTetroFit(0, 0))
     {
         CurrentTetromino.Rotate();
     }
     else if (keyInput == "gravity" && CanTetroFit(0, 1))
     {
         CurrentTetromino.GravityTick();
     }
     else
     {
         return(false);
     }
     return(true);
 }