Beispiel #1
0
        public void StartGameLoop()
        {
            var time1 = DateTime.Now.Ticks;

            while (!_hardware.IsButtonPressed(ButtonPressed.Select))
            {
                // Draw
                _hardware.ClearScreen();
                drawScene();
                _hardware.UpdateScreen();

                // Input
                var buttonPressed = _hardware.GetButtonPressed();

                switch (buttonPressed)
                {
                case ButtonPressed.Right:
                    if (board.IsMovementPossible(currentPiece, buttonPressed))
                    {
                        currentPiece.PosX++;
                    }
                    break;

                case ButtonPressed.Left:
                    if (board.IsMovementPossible(currentPiece, buttonPressed))
                    {
                        currentPiece.PosX--;
                    }
                    break;

                case ButtonPressed.Down:
                    if (board.IsMovementPossible(currentPiece, buttonPressed))
                    {
                        currentPiece.PosY++;
                    }
                    break;

                case ButtonPressed.Drop:
                    while (board.IsMovementPossible(currentPiece, buttonPressed))
                    {
                        currentPiece.PosY++;
                    }
                    currentPiece.PosY--;
                    board.StorePiece(currentPiece);
                    board.DeleteAllPossibleRows();

                    if (board.IsGameOver())
                    {
                        return;
                    }
                    CreateNewPiece();
                    break;

                case ButtonPressed.Rotate:
                    if (board.IsMovementPossible(currentPiece, buttonPressed))
                    {
                        currentPiece.Rotation = (byte)((currentPiece.Rotation + 1) % 4);
                    }
                    ;
                    break;
                }

                // Vertical movement
                var time2 = DateTime.Now.Ticks;

                if ((time2 - time1) > waitTime)
                {
                    if (board.IsMovementPossible(currentPiece, ButtonPressed.Down))
                    {
                        currentPiece.PosY++;
                    }
                    else
                    {
                        board.StorePiece(currentPiece);
                        board.DeleteAllPossibleRows();

                        if (board.IsGameOver())
                        {
                            return;
                        }

                        CreateNewPiece();
                    }
                    time1 = DateTime.Now.Ticks;
                }
            }
        }