private void frmAsteroids_KeyDown(object sender, KeyEventArgs e) { // Check escape key if (e.KeyData == Keys.Escape) // Escape { // Escape during a title screen exits the game if (gameStatus == Modes.TITLE) { gameStatus = Modes.EXIT; Application.Exit(); } // Escape in game goes back to Title Screen if (gameStatus == Modes.GAME) { score.CancelGame(); currTitle = new TitleScreen(); gameStatus = Modes.TITLE; } } else // Not Escape { // If we are in tht Title Screen, Start a game if (gameStatus == Modes.TITLE) { score.ResetGame(); currGame = new Game(); gameStatus = Modes.GAME; bLeftPressed = false; bRightPressed = false; bUpPressed = false; bHyperspaceLastPressed = false; bShootingLastPressed = false; bPauseLastPressed = false; } else // In Game { // Keydown handled in game // Rotate Left if (e.KeyData == Keys.Left) { bLeftPressed = true; } // Rotate Right if (e.KeyData == Keys.Right) { bRightPressed = true; } // Thrust if (e.KeyData == Keys.Up) { bUpPressed = true; } // Hyperspace (can't be held down) if (!bHyperspaceLastPressed && (e.KeyData == Keys.Down)) { bHyperspaceLastPressed = true; currGame.Hyperspace(); } // Shooting (can't be held down) if (!bShootingLastPressed && (e.KeyData == Keys.Space)) { bShootingLastPressed = true; currGame.Shoot(); } // Pause can't be held down) if (!bPauseLastPressed && (e.KeyData == Keys.P)) { bPauseLastPressed = true; currGame.Pause(); } } } }
/// <summary> /// Метод инициализации событий и добавления кнопок на основую форму /// </summary> /// <param name="form">Основная форма</param> public static void ControlsInit(Form form) { BtnList.Clear(); BtnList.Add(GameContinueBtn); BtnList.Add(NewGameBtn); BtnList.Add(RecordsBtn); BtnList.Add(ExitBtn); ShowMenu(form, true); // Показать меню foreach (var b in BtnList) { form.Controls.Add(b); } #region Описание событий // Событие нажатия "Начать игру" NewGameBtn.Click += (object sender, EventArgs e) => { ShowMenu(form, false); if (MessageBox.Show("Игра началась!\n" + Settings.GameRules, $"Привет, {Settings.UserName}!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK) { Game.Restart(); } }; // Событие нажатия "Выход" ExitBtn.Click += (object sender, EventArgs e) => { if (MessageBox.Show("Вы уверены, что хотите выйти?", "Выход", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { Application.Exit(); } }; // Событие нажатия "Продолжить игру" GameContinueBtn.Click += (object sender, EventArgs e) => { if (Game.GameStarting) { Game.Pause(); form.Focus(); } }; // Событие нажатия "Продолжить игру" RecordsBtn.Click += (object sender, EventArgs e) => { ShowRecordsFile(); }; // Событие нажатия клавиш form.KeyDown += (object sender, KeyEventArgs e) => { if (e.KeyCode == Keys.Space) { Game.Ship?.Fire(true); } if (e.KeyCode == Keys.Up) { Game.Ship?.Up(true); } if (e.KeyCode == Keys.Down) { Game.Ship?.Down(true); } if (e.KeyCode == Keys.Left) { Game.Ship?.Left(true); } if (e.KeyCode == Keys.Right) { Game.Ship?.Right(true); } if (e.KeyCode == Keys.Escape && Game.GameStarting) { Game.Pause(); } }; form.KeyUp += (object sender, KeyEventArgs e) => { if (e.KeyCode == Keys.Space) { Game.Ship?.Fire(false); } if (e.KeyCode == Keys.Up) { Game.Ship?.Up(false); } if (e.KeyCode == Keys.Down) { Game.Ship?.Down(false); } if (e.KeyCode == Keys.Left) { Game.Ship?.Left(false); } if (e.KeyCode == Keys.Right) { Game.Ship?.Right(false); } }; #endregion }