Beispiel #1
0
 public TetrisGameManager(ITetrisGame tetrisGame, IInputHandler consoleInputHandler, TetrisConsoleWriter tetrisConsoleWriter, ScoreManager scoreManager)
 {
     this.tetrisGame          = tetrisGame;
     this.consoleInputHandler = consoleInputHandler;
     this.tetrisConsoleWriter = tetrisConsoleWriter;
     this.scoreManager        = scoreManager;
 }
Beispiel #2
0
        public void Run()
        {
            gameState = new TetrisGameState(tetrisRows, tetrisCols);
            Random random = new Random();
            var    tetrisConsoleWriter = new TetrisConsoleWriter(tetrisRows, tetrisCols, tetrisInfo);

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Title           = "Tetris v1.0";
            Console.CursorVisible   = false;
            gameState.CurrentFigure = tetrisFigures[random.Next(0, tetrisFigures.Count)];
            KeyAvailable(tetrisConsoleWriter, random);
        }
        public void DrawCurrentFigure(Tetrominoe currentFigure, int currentFigureRow, int currentFigureCol)
        {
            var tetrisConsoleWriter = new TetrisConsoleWriter(tetrisRows, tetrisCols, tetrisInfo);

            for (int row = 0; row < currentFigure.Body.GetLength(0); row++)
            {
                for (int col = 0; col < currentFigure.Body.GetLength(1); col++)
                {
                    if (currentFigure.Body[row, col])
                    {
                        tetrisConsoleWriter.Write("*", row + 1 + currentFigureRow, col + 1 + currentFigureCol);
                    }
                }
            }
        }
        public void DrawTetrisField(bool[,] tetrisField)
        {
            var tetrisConsoleWriter = new TetrisConsoleWriter(tetrisRows, tetrisCols, tetrisInfo);

            for (int row = 0; row < tetrisField.GetLength(0); row++)
            {
                for (int col = 0; col < tetrisField.GetLength(1); col++)
                {
                    if (tetrisField[row, col])
                    {
                        tetrisConsoleWriter.Write("*", row + 1, col + 1);
                    }
                }
            }
        }
        public static void Main()
        {
            ScoreManager scoreManager = new ScoreManager("scores.txt");
            var          musicPlayer  = new MusicPlayer();

            musicPlayer.Play();

            var tetrisConsoleWriter = new TetrisConsoleWriter(TetrisRows, TetrisCols);
            var random = new Random();

            State.CurrentFigure = TetrisFigures[random.Next(0, TetrisFigures.Count)];
            while (true)
            {
                State.Frame++;
                State.UpdateLevel(scoreManager.Score);
                // Read user input
                if (Console.KeyAvailable)
                {
                    var key = Console.ReadKey();
                    if (key.Key == ConsoleKey.Escape)
                    {
                        // Environment.Exit(0);
                        return; // Becase of Main()
                    }
                    if (key.Key == ConsoleKey.LeftArrow || key.Key == ConsoleKey.A)
                    {
                        if (State.CurrentFigureCol >= 1)
                        {
                            State.CurrentFigureCol--;
                        }
                    }
                    if (key.Key == ConsoleKey.RightArrow || key.Key == ConsoleKey.D)
                    {
                        if (State.CurrentFigureCol < TetrisCols - State.CurrentFigure.Height)
                        {
                            State.CurrentFigureCol++;
                        }
                    }
                    if (key.Key == ConsoleKey.DownArrow || key.Key == ConsoleKey.S)
                    {
                        State.Frame = 1;
                        scoreManager.AddToScore(State.Level);
                        State.CurrentFigureRow++;
                    }
                    if (key.Key == ConsoleKey.Spacebar || key.Key == ConsoleKey.UpArrow || key.Key == ConsoleKey.W)
                    {
                        var newFigure = State.CurrentFigure.GetRotate();
                        if (!Collision(newFigure))
                        {
                            State.CurrentFigure = newFigure;
                        }
                    }
                }

                // Update the game state
                if (State.Frame % (State.FramesToMoveFigure - State.Level) == 0)
                {
                    State.CurrentFigureRow++;
                    State.Frame = 0;
                }

                if (Collision(State.CurrentFigure))
                {
                    AddCurrentFigureToTetrisField();
                    int lines = CheckForFullLines();
                    scoreManager.AddToScore(ScorePerLines[lines] * State.Level);
                    State.CurrentFigure    = TetrisFigures[random.Next(0, TetrisFigures.Count)];
                    State.CurrentFigureRow = 0;
                    State.CurrentFigureCol = 0;
                    if (Collision(State.CurrentFigure)) // game is over
                    {
                        scoreManager.AddToHighScore();
                        tetrisConsoleWriter.DrawAll(State, scoreManager);
                        tetrisConsoleWriter.WriteGameOver(scoreManager.Score);
                        Thread.Sleep(100000);
                        return;
                    }
                }

                // Redraw UI
                tetrisConsoleWriter.DrawAll(State, scoreManager);

                Thread.Sleep(40);
            }
        }
Beispiel #6
0
        public void KeyAvailable(TetrisConsoleWriter tetrisConsoleWriter, Random random)
        {
            while (true)
            {
                if (Console.KeyAvailable)
                {
                    var key = Console.ReadKey();
                    if (key.Key == ConsoleKey.Escape)
                    {
                        return;
                    }
                    if (key.Key == ConsoleKey.LeftArrow || key.Key == ConsoleKey.A)
                    {
                        if (gameState.CurrentFigureCol >= 1)
                        {
                            gameState.CurrentFigureCol--;
                        }
                    }
                    if (key.Key == ConsoleKey.RightArrow || key.Key == ConsoleKey.D)
                    {
                        if (gameState.CurrentFigureCol < tetrisCols - gameState.CurrentFigure.Height)
                        {
                            gameState.CurrentFigureCol++;
                        }
                    }
                    if (key.Key == ConsoleKey.DownArrow || key.Key == ConsoleKey.S)
                    {
                        gameState.Frame = 1;
                        gameState.Score++;
                        gameState.CurrentFigureRow++;
                    }
                    if (key.Key == ConsoleKey.UpArrow || key.Key == ConsoleKey.W || key.Key == ConsoleKey.Spacebar)
                    {
                        var newFigure = gameState.CurrentFigure.GetRotate();

                        if (!Collision(newFigure))
                        {
                            gameState.CurrentFigure = newFigure;
                        }
                    }
                }

                if (gameState.Frame % gameState.FrameToMoveFigure == 0)
                {
                    gameState.CurrentFigureRow++;
                    gameState.Frame = 0;
                }

                if (Collision(gameState.CurrentFigure))
                {
                    AddCurrentFigureToTheTetrisField();
                    int lines = CheckFrFullLines();
                    tetrisConsoleWriter.DrawAll(gameState);
                    gameState.CurrentFigure    = tetrisFigures[random.Next(0, tetrisFigures.Count)];
                    gameState.CurrentFigureRow = 0;
                    gameState.CurrentFigureCol = 0;
                    if (Collision(gameState.CurrentFigure))
                    {
                        tetrisConsoleWriter.Write("Game over!", 10, 7);
                        Console.ReadKey();
                    }
                }
                gameState.Frame++;
                tetrisConsoleWriter.DrawAll(gameState);
                Thread.Sleep(10);
            }
        }