private void update(object sender, EventArgs e) { //Check if the game is over if (InitialSettings.GameOver) { //Check if user presses enter. If yes, begin a new game if (KeyboardInput.checkForKeyPress(Keys.Enter)) { beginNewGame(); } } //This set of if statements ensures that user cannot press a direction button while going in the opposite one //e.g. user cannot choose to go right while currently moving left else { if (KeyboardInput.checkForKeyPress(Keys.Right) && InitialSettings.playerDirection != direction.Left) { InitialSettings.playerDirection = direction.Right; } else if (KeyboardInput.checkForKeyPress(Keys.Left) && InitialSettings.playerDirection != direction.Right) { InitialSettings.playerDirection = direction.Left; } else if (KeyboardInput.checkForKeyPress(Keys.Down) && InitialSettings.playerDirection != direction.Up) { InitialSettings.playerDirection = direction.Down; } else if (KeyboardInput.checkForKeyPress(Keys.Up) && InitialSettings.playerDirection != direction.Down) { InitialSettings.playerDirection = direction.Up; } //Call the method that will move the player in the direction he pressed moveCharacter(); } //Use the Invalidate function to refresh the game board pbGameBoard.Invalidate(); }
//Method that checks if a key is no longer pressed private void Form1_KeyUp(object sender, KeyEventArgs e) { KeyboardInput.changeKeyState(e.KeyCode, false); }