public void GameSmallestFieldSizeGameplayTest() { Game game = new Game(5, 5); Assert.DoesNotThrow(() => { for (int i = 0; i < 5; i++) { game.Tick(); } }); }
static public void Tick(Object stateInfo) { if (_isDrawing) { return; } _isDrawing = true; if (Console.KeyAvailable) { ConsoleKeyInfo cki = Console.ReadKey(false); _exit = _game.ControllCallback(cki.Key); } Console.Clear(); _game.Tick(); _game.Render(); DrawScreen(); _isDrawing = false; }
private static bool StartGame() { const int size = 20; _game = new Game(size, size); CurrentScore = 2; CurrentSpeed = 2.0; var direction = Snake.Direction.Up; ConsoleKey?lastKeyPressed = null; while (true) { if (Console.KeyAvailable) { lastKeyPressed = Console.ReadKey(true).Key; } switch (lastKeyPressed) { case null: break; case ConsoleKey.W: if (direction != Snake.Direction.Down) { direction = Snake.Direction.Up; } break; case ConsoleKey.A: if (direction != Snake.Direction.Right) { direction = Snake.Direction.Left; } break; case ConsoleKey.S: if (direction != Snake.Direction.Up) { direction = Snake.Direction.Down; } break; case ConsoleKey.D: if (direction != Snake.Direction.Left) { direction = Snake.Direction.Right; } break; case ConsoleKey.Escape: return(true); } switch (_game.Tick(direction)) { case Game.GameEvent.Lose: Console.SetCursorPosition(_game.Field.Width / 2, _game.Field.Height / 2); Console.Write($"Game Over! Your score:{CurrentScore}"); Console.ReadKey(); Console.Clear(); return(false); case Game.GameEvent.Upscore: CurrentScore++; CurrentSpeed += 0.1; if (CurrentScore > Properties.Settings.Default.HighScore) { Properties.Settings.Default.HighScore = CurrentScore; Properties.Settings.Default.Save(); } break; default: break; } Update(_game.Field); Thread.Sleep((int)(500 / CurrentSpeed)); } }