/// <summary> /// Main entry point. Provides a menu for player to choose starting level and save scores. /// </summary> /// <returns></returns> public int MainMenu() { bool exit = false; while (!exit) { long score; int startLevel = ChooseStartingLevel(); TetfuzaBackend currentGame = new TetfuzaBackend(_input, _display, startLevel); score = currentGame.Run(); _display.WriteText("Oh no, you topped out... please press start to proceed", .5m, .5m); while (_input.ReadInput() != Input.Pause) { ; } // display final game information & ask to continue _display.ClearScreen(); _display.WriteText("Your final score is: " + CommasInNumber(score), .5m, .55m); SaveScore(score); _display.WriteText("Press start to exit, any other button to play again", .5m, .6m); // Wait for input and end program if start was pressed exit = (_input.ReadInput() == Input.Pause); } return(1); }
/// <summary> /// Main CLI Loop /// </summary> public void StartCLI() { game = new TetfuzaMenu(_keyboard, _screen); _screen.ClearScreen(); // Open game menu. returns when user quits game int exitPath = game.MainMenu(); }
public void Loop() { _display.ClearScreen(); DrawScene(); _display.UpdateScreen(); long firstTime = DateTime.UtcNow.Ticks; while (!_display.IsQuitPressed()) { var buttonPressed = _display.PollButtons(); switch (buttonPressed) { case GameButton.Right: if (_board.IsMovementPossible(CurrentPiece, CurrentPiece.PosX + 1, CurrentPiece.PosY)) { CurrentPiece.PosX++; } break; case GameButton.Left: if (_board.IsMovementPossible(CurrentPiece, CurrentPiece.PosX - 1, CurrentPiece.PosY)) { CurrentPiece.PosX--; } break; case GameButton.Down: if (_board.IsMovementPossible(CurrentPiece, CurrentPiece.PosX, CurrentPiece.PosY + 1)) { CurrentPiece.PosY++; } break; case GameButton.Drop: while (_board.IsMovementPossible(CurrentPiece, CurrentPiece.PosX, CurrentPiece.PosY + 1)) { CurrentPiece.PosY++; } ChangePiece(CurrentPiece, true); break; case GameButton.Rotate: if (_board.IsMovementPossible(CurrentPiece, CurrentPiece.PosX, CurrentPiece.PosY, true)) { var newRotation = ((int)CurrentPiece.Rotation + 1) % 4; CurrentPiece.Rotation = (PieceRotation)newRotation; } break; } var secondTime = DateTime.UtcNow.Ticks; if ((secondTime - firstTime) > waitTime) { if (_board.IsMovementPossible(CurrentPiece, CurrentPiece.PosX, CurrentPiece.PosY + 1)) { CurrentPiece.PosY++; } else { ChangePiece(CurrentPiece, false); } firstTime = DateTime.UtcNow.Ticks; } } }
/// <summary> /// Main Loop /// </summary> /// <returns>Final Score</returns> public long Run() { _screen.ClearScreen(); bool gameOver = false; _timer.Start(); int pieceNum = _rand.Next(0, 7); NextPiece = new FuzaPiece((FuzaPieceType)pieceNum); while (!gameOver) { // Spawn a new piece and reset center CurrentPiece = NextPiece; _pieceCenter = new Coordinate(5, 2); // Determine next piece pieceNum = _rand.Next(0, 7); NextPiece = new FuzaPiece((FuzaPieceType)pieceNum); // Draw Board _screen.DrawGameplayScreen(this); bool isLockDown = false; while (!isLockDown) { // Get user key press int xDirection = 0; int yDirection = 0; int rotation = 0; if (true == _keyboard.InputAvailable) { _keyboard.GetInput(ref xDirection, ref yDirection, ref rotation); // Move and/or Rotate piece as appropriate MovePieceLeftRight(xDirection); RotatePiece(rotation); } // Move piece down at constant rate, or instant if user inputs down bool autoDown = (_frameCount % DropSpeed) == 0; if (yDirection == -1) { autoDown = true; Score += 1; } if (autoDown) { isLockDown = !DropPiece(); } _board.DrawPiece(CurrentPiece, _pieceCenter); // Draw Screen _screen.DrawGameplayScreen(this); StableFrames.Stabilize(MS_PER_FRAME, _timer); _frameCount++; } // Lock Piece and redraw it CurrentPiece.LockPiece(); _board.DrawPiece(CurrentPiece, _pieceCenter); // Check if lines were cleared and update score int linesCleared = _board.ClearLines(); if (linesCleared > 0) { AddClearedLinesToScore(linesCleared); } // Check if player has topped out (game over) gameOver = _board.CheckTopOut(); } // Plays the game over animation TopOutAnimation(); return(Score); }