public void Update(Keyboard keyboard) { if (player == Player.Player1) { if (keyboard.IsKeyPress(Keys.W)) { position.Y = position.Y - SPEED; Boundary = new Rectangle((int)position.X, (int)position.Y, 10, 50); } } if (player == Player.Player1) { if (keyboard.IsKeyPress(Keys.S)) { position.Y = position.Y + SPEED; Boundary = new Rectangle((int)position.X, (int)position.Y, 10, 50); } } if (Boundary.Top <= 0) { position.Y = 0; } if (Boundary.Bottom >= Game1.vWindowSize.Y) { position.Y = Game1.vWindowSize.Y - HEIGHT; } // if (player == Player.Player2) { if (keyboard.IsKeyPress(Keys.Up)) { position.Y = position.Y - SPEED; Boundary = new Rectangle((int)position.X, (int)position.Y, 10, 50); } } if (player == Player.Player2) { if (keyboard.IsKeyPress(Keys.Down)) { position.Y = position.Y + SPEED; Boundary = new Rectangle((int)position.X, (int)position.Y, 10, 50); if (Boundary.Top <= 0) { position.Y = 0; } if (Boundary.Bottom >= Game1.vWindowSize.Y) { position.Y = Game1.vWindowSize.Y - HEIGHT; } } } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) // this.Exit(); // TODO: Add your update logic here myKeyboard.Update(); Random random = new Random(); x = random.Next(0, 255); y = random.Next(0, 255); z = random.Next(0, 255); if (change == 1) { a = random.Next(0, 200); b = random.Next(0, 200); c = random.Next(0, 200); } if (gameState == GameState.Menu) { fromMenu = true; player1 = new Paddle(vPaddle, Paddle.Player.Player1); player2 = new Paddle(vPaddle2, Paddle.Player.Player2); ball = new Ball(); //check if enter present to set to play game if (myKeyboard.IsNewKeyPress(Keys.Enter)) { gameState = GameState.Play; } else if (myKeyboard.IsNewKeyPress(Keys.I)) { gameState = GameState.Instructions; } //menu to settings else if (myKeyboard.IsNewKeyPress(Keys.S)) { gameState = GameState.Settings; } else if (myKeyboard.IsNewKeyPress(Keys.Escape)) { gameState = GameState.Exit; } //initalize the ball and the player paddles } if (gameState == GameState.Instructions) { //instructions to main menu if (myKeyboard.IsKeyPress(Keys.M)) { gameState = GameState.Menu; } } //setting buttons if (gameState == GameState.Settings) { //settings to main menu if (myKeyboard.IsNewKeyPress(Keys.M)) { gameState = GameState.Menu; } if (myKeyboard.IsNewKeyPress(Keys.E)) { difficulty = Difficulty.Easy; } if (myKeyboard.IsNewKeyPress(Keys.H)) { difficulty = Difficulty.Hard; } if (myKeyboard.IsNewKeyPress(Keys.G)) { theme = Theme.Green; } if (myKeyboard.IsNewKeyPress(Keys.B)) { theme = Theme.Black; } if (myKeyboard.IsNewKeyPress(Keys.P)) { theme = Theme.Pink; } if (myKeyboard.IsNewKeyPress(Keys.R)) { theme = Theme.Rave; } if (myKeyboard.IsNewKeyPress(Keys.C)) { theme = Theme.Changing; } } //in game buttons if (gameState == GameState.Play) { fromMenu = false; //pause game if (myKeyboard.IsNewKeyPress(Keys.P)) { gameState = GameState.Pause; } //exit game if (myKeyboard.IsNewKeyPress(Keys.Escape)) { gameState = GameState.Exit; } } //pause buttons if (gameState == GameState.Pause) { //pause to resume if (myKeyboard.IsNewKeyPress(Keys.Enter)) { gameState = GameState.Play; } //pause to escape if (myKeyboard.IsNewKeyPress(Keys.Escape)) { gameState = GameState.Exit; } //pause to main menu if (myKeyboard.IsNewKeyPress(Keys.M)) { gameState = GameState.Menu; } if (myKeyboard.IsNewKeyPress(Keys.R)) { gameState = GameState.Play; ball.points1 = 0; ball.points2 = 0; player1 = new Paddle(vPaddle, Paddle.Player.Player1); player2 = new Paddle(vPaddle2, Paddle.Player.Player2); ball.Reset(); } } //exit buttons if (gameState == GameState.Exit) { //exit if (myKeyboard.IsNewKeyPress(Keys.Y)) { Exit(); } //resume if (myKeyboard.IsNewKeyPress(Keys.N)) { if (fromMenu == false) { gameState = GameState.Play; } else { gameState = GameState.Menu; } } } if (gameState == GameState.Play) { ball.Update(); player1.Update(myKeyboard); player2.Update(myKeyboard); } if (ball.Boundary.Intersects(player1.Boundary)) { if (ball.speed < 12) { ball.velocity.X *= -1.1f; ball.speed = (float)ball.velocity.Length(); paddle.Play(); } else { ball.velocity.X *= -1; paddle.Play(); } } // if (ball.Boundary.Intersects(player2.Boundary)) { //paddle if (ball.speed < 12) { ball.velocity.X *= -1.1f; ball.speed = (float)ball.velocity.Length(); paddle.Play(); } else { ball.velocity.X *= -1; paddle.Play(); } } if (ball.points1 == 10 || ball.points2 == 10) { gameState = GameState.GameOver; } if (gameState == GameState.GameOver) { if (myKeyboard.IsNewKeyPress(Keys.R)) { gameState = GameState.Play; ball.points1 = 0; ball.points2 = 0; player1 = new Paddle(vPaddle, Paddle.Player.Player1); player2 = new Paddle(vPaddle2, Paddle.Player.Player2); } if (myKeyboard.IsNewKeyPress(Keys.M)) { gameState = GameState.Menu; } if (myKeyboard.IsNewKeyPress(Keys.Escape)) { Exit(); } } base.Update(gameTime); }