private void StartANewGame(bool Bot = false, dynamic Settings = null) { int width = mapUI.GetLength(0); int height = mapUI.GetLength(1); Map map = new Map(new Size(width, height)); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { mapUI[i, j].Land = map.Locations[i, j]; } } if (!Bot) { GameHandle.Initialize(this, map, new Func <int, int>[] { AddFood }); GameHandle.OnSnakeMoves += GameHandle_OnSnakeMoves; GameHandle.OnSnakeEats += GameHandle_OnSnakeEats; GameHandle.OnGameStops += GameHandle_OnGameStops; GameHandle.GameFinished += (sender, e) => { Speech.SpeakAsync("You are officially, an expert"); MessageBox.Show("The game has finished due to your fenomenal skillz!", "Astonishing!", MessageBoxButtons.OK, MessageBoxIcon.Information); }; } else { SnakeBot bot = new SnakeBot(this, map, Settings.Food); bot.Interval = Settings.Time; this.bot = bot; bot.Start(); bot.snake.OnSnakeMoves += GameHandle_OnSnakeMoves; bot.snake.OnSnakeEats += GameHandle_OnSnakeEats; } }
private void Key_Down(object sender, KeyEventArgs e) { char ec = e.KeyData.ToString()[0]; BotCode.Enqueue(ec); if (IsBotAlive) { return; } var snake = GameHandle.snake; if (snake is null) { return; } if (e.KeyData == Keys.A || e.KeyData == Keys.Left) { if (snake.HeadsTo == Directions.Left) { return; } GameHandle.ChangeDirection(Directions.Left); }/////////////////////////////////////////////////// if (e.KeyData == Keys.D || e.KeyData == Keys.Right) { if (snake.HeadsTo == Directions.Right) { return; } GameHandle.ChangeDirection(Directions.Right); }////////////////////////////////////////////////////// if (e.KeyData == Keys.W || e.KeyData == Keys.Up) { if (snake.HeadsTo == Directions.Up) { return; } GameHandle.ChangeDirection(Directions.Up); }////////////////////////////////////////////////////// if (e.KeyData == Keys.S || e.KeyData == Keys.Down) { if (snake.HeadsTo == Directions.Down) { return; } GameHandle.ChangeDirection(Directions.Down); } }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { int tries = 0; Start: try { GameHandle.StopTheGame(); bot.Stop(); } catch (Exception exc) { tries++; if (tries < 10) { goto Start; } } }
private void CancelGame() { GameHandle.StopTheGame(); }