/// <summary> /// /// </summary> /// <param name="keyboardState"></param> /// <param name="gameTime"></param> /// <returns></returns> public bool Update(KeyboardState keyboardState, GameTime gameTime) { #region Barrier movement if (Game1.gameSettings.barriers == false) { botBarrier.position = new Vector2(topBarrier.position.X, -topBarrier.size.Y); topBarrier.position = new Vector2(topBarrier.position.X, Game1.graphics.PreferredBackBufferHeight + 10); } else { topBarrier.Move(); botBarrier.Move(); if (topBarrier.Collides(botBarrier)) { topBarrier.velocity *= -1; botBarrier.velocity *= -1; } } #endregion #region Paddle and Ball Movement P1.move(gameBall, keyboardState, gameTime); P1.paddle.withinScreen(); P2.move(gameBall, keyboardState, gameTime); P2.paddle.withinScreen(); gameBall.Move(); gameBall.withinScreen(); #endregion #region PowerUps if (Game1.gameSettings.powerUps == true) { //Load location of powerups into game if the power up is active if (ballSpeedUp.active) { ballSpeedUp.position = new Vector2(312, 178); } else { ballSpeedUp.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } if (ballSpeedDown.active) { //Make it so that the X coordinates match on right side of gym ballSpeedDown.position = new Vector2(936 - (barrierSpeedUp.size.X / 2), 445); } else { ballSpeedDown.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } if (barrierSpeedUp.active) { barrierSpeedUp.position = new Vector2(936 - (barrierSpeedUp.size.X / 2), 178); } else { barrierSpeedUp.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } if (barrierSpeedDown.active) { barrierSpeedDown.position = new Vector2(312, 445); } else { barrierSpeedDown.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } } //Green Pokeball if (ballSpeedUp.CircleCollides(gameBall)) { ballSpeedUp.speedBall(gameBall); ballSpeedUp.active = false; ballSpeedUp.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } //Yellow Pokeball if (ballSpeedDown.CircleCollides(gameBall)) { ballSpeedDown.slowBall(gameBall); ballSpeedDown.active = false; ballSpeedDown.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } //Ponyta if (barrierSpeedUp.CircleCollides(gameBall)) { barrierSpeedUp.speedBarrier(topBarrier); barrierSpeedUp.speedBarrier(botBarrier); barrierSpeedUp.active = false; barrierSpeedUp.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } //Snorlax if (barrierSpeedDown.CircleCollides(gameBall)) { barrierSpeedDown.slowBarrier(topBarrier); barrierSpeedDown.slowBarrier(botBarrier); barrierSpeedDown.active = false; barrierSpeedDown.position = new Vector2(Game1.graphics.PreferredBackBufferWidth + 200, Game1.graphics.PreferredBackBufferHeight + 200); } #endregion #region Scoring if (gameBall.position.X < P2.paddle.size.X - gameBall.size.X) { scoreCue = soundsBank.GetCue("Score"); scoreCue.Play(); P1.scorePoint(); //Reset the ball's position to the center after each point gameBall.Reset(); botBarrier.Reset(); topBarrier.Reset(); //Ball gets new random velocity each time a point is scored gameBall.velocity = new Vector2(randPowerUp.Next(5, 7), randPowerUp.Next(-2, 3)); if (gameBall.velocity.Y == 0) { gameBall.velocity = new Vector2(gameBall.velocity.X, randPowerUp.Next(-3, -1)); } } if (gameBall.position.X > Game1.graphics.PreferredBackBufferWidth - P1.paddle.size.X) { scoreCue = soundsBank.GetCue("Score"); scoreCue.Play(); P2.scorePoint(); //Reset the ball's position to the center after each point gameBall.Reset(); botBarrier.Reset(); topBarrier.Reset(); //Ball gets new random velocity each time a point is scored gameBall.velocity = new Vector2(randPowerUp.Next(5, 7), randPowerUp.Next(-2, 3)); if (gameBall.velocity.Y == 0) { gameBall.velocity = new Vector2(gameBall.velocity.X, randPowerUp.Next(-3, -1)); } } #endregion #region Game Ball Collision if (gameBall.CircleCollidesPaddle(P1.paddle)) { ballCue = soundsBank.GetCue("BallHit"); ballCue.Play(); gameBall.velocity = new Vector2(-gameBall.velocity.X, randPowerUp.Next(-2, 3)); if (gameBall.velocity.Y == 0) { gameBall.velocity = new Vector2(gameBall.velocity.X, randPowerUp.Next(-3, -1)); } gameBall.Move(); if (P1.paddle.Collides(gameBall)) { gameBall.position -= gameBall.velocity; gameBall.velocity = new Vector2(-gameBall.velocity.X, gameBall.velocity.X); } } if (gameBall.CircleCollidesPaddle(P2.paddle)) { ballCue = soundsBank.GetCue("BallHit"); ballCue.Play(); gameBall.velocity = new Vector2(-gameBall.velocity.X, randPowerUp.Next(-2, 3)); if (gameBall.velocity.Y == 0) { gameBall.velocity = new Vector2(gameBall.velocity.X, randPowerUp.Next(-3, -1)); } gameBall.Move(); if (P2.paddle.Collides(gameBall)) { gameBall.position -= gameBall.velocity; gameBall.velocity = new Vector2(-gameBall.velocity.X, gameBall.velocity.X); } } if (gameBall.CircleCollidesPaddle(botBarrier)) { ballCue = soundsBank.GetCue("BallHit"); ballCue.Play(); gameBall.velocity *= -1; topBarrier.velocity *= -1; botBarrier.velocity *= -1; //if (gameBall.center.X <= botBarrier.position.X && gameBall.center.X >= botBarrier.position.X + botBarrier.size.X && gameBall.position.Y >= botBarrier.position.Y + gameBall.size.Y) //{ // topBarrier.velocity = new Vector2(0, 5f); // botBarrier.velocity = new Vector2(0, -5f); // gameBall.velocity = new Vector2(gameBall.velocity.X, -gameBall.velocity.Y); //} //else if (gameBall.center.X <= botBarrier.position.X && gameBall.center.X >= botBarrier.position.X + botBarrier.size.X && gameBall.position.Y <= botBarrier.position.Y + botBarrier.size.Y) //{ // topBarrier.velocity = new Vector2(0, -5f); // botBarrier.velocity = new Vector2(0, 5f); // gameBall.velocity = new Vector2(gameBall.velocity.X, -gameBall.velocity.Y); //} //else if (gameBall.position.X == botBarrier.position.X) //{ // gameBall.velocity *= -1; //} //else if (gameBall.position.X == botBarrier.position.X + botBarrier.size.X) //{ // gameBall.velocity *= -1; //} } if (gameBall.CircleCollidesPaddle(topBarrier)) { ballCue = soundsBank.GetCue("BallHit"); ballCue.Play(); gameBall.velocity *= -1; topBarrier.velocity *= -1; botBarrier.velocity *= -1; //if (gameBall.center.X <= topBarrier.position.X && gameBall.center.X >= topBarrier.position.X + topBarrier.size.X && gameBall.position.Y <= topBarrier.position.Y + topBarrier.size.Y) //{ // topBarrier.velocity = new Vector2(0f, 5f); // botBarrier.velocity = new Vector2(0f, -5f); // gameBall.velocity = new Vector2(gameBall.velocity.X, -gameBall.velocity.Y); //} //else if (gameBall.center.X <= topBarrier.position.X && gameBall.center.X >= topBarrier.position.X + topBarrier.size.X && gameBall.position.Y >= topBarrier.position.Y + gameBall.size.Y) //{ // topBarrier.velocity = new Vector2(0f, -5f); // botBarrier.velocity = new Vector2(0f, 5f); // gameBall.velocity = new Vector2(gameBall.velocity.X, -gameBall.velocity.Y); //} //else if (gameBall.position.X >= topBarrier.position.X) //{ // gameBall.velocity *= -1; //} //else if (gameBall.position.X <= topBarrier.position.X + topBarrier.size.X) //{ // gameBall.velocity *= -1; //} } #endregion if (P1.score == winningScore || P2.score == winningScore) { gameActive = false; } audioEngine.Update(); destAsh = new Rectangle((int)P1.paddle.position.X, (int)P1.paddle.position.Y, 51, 235); // updates position of Ash destGary = new Rectangle((int)P2.paddle.position.X, (int)P2.paddle.position.Y, 51, 235); // updates position of Gary return(P1.score == winningScore || P2.score == winningScore); }
public void move(clsSprite gameBall, KeyboardState movementKey, GameTime gameTime) { if (playerType == PongGame.PlayerType.PlayerOne) { if (movementKey.IsKeyDown(Keys.Up)) { this.animateAsh(gameTime); paddle.velocity = new Vector2(0, -5); paddle.Move(); } else if (movementKey.IsKeyDown(Keys.Down)) { this.animateAsh(gameTime); paddle.velocity = new Vector2(0, 5); paddle.Move(); } } else if (playerType == PongGame.PlayerType.PlayerTwo) { if (movementKey.IsKeyDown(Keys.W)) { this.animateGary(gameTime); paddle.velocity = new Vector2(0, -5); paddle.Move(); } else if (movementKey.IsKeyDown(Keys.S)) { this.animateGary(gameTime); paddle.velocity = new Vector2(0, 5); paddle.Move(); } } else if (playerType == PongGame.PlayerType.CPU) { //Add code for difficulty here regarding when the //paddle finds out where the ball is if (Game1.gameSettings.difficulty == pongSettings.Difficulty.Easy) { paddle.Move(); this.animateGary(gameTime); paddle.withinScreen(); } else if (Game1.gameSettings.difficulty == pongSettings.Difficulty.Medium) { if (gameBall.position.X - paddle.position.X < 50 && gameBall.position.Y > (paddle.center.Y - paddle.size.Y / 2)) { this.animateGary(gameTime); paddle.velocity = new Vector2(0, 5); paddle.Move(); } else if (gameBall.position.X - paddle.position.X < 50 && gameBall.position.Y < (paddle.center.Y - paddle.size.Y / 2)) { this.animateGary(gameTime); paddle.velocity = new Vector2(0, -5); paddle.Move(); } else { this.animateGary(gameTime); paddle.Move(); } paddle.withinScreen(); } else if (Game1.gameSettings.difficulty == pongSettings.Difficulty.Hard) { if (gameBall.position.X - paddle.position.X < 300 && gameBall.position.Y > (paddle.center.Y - paddle.size.Y / 2)) { this.animateGary(gameTime); paddle.velocity = new Vector2(0, 5); paddle.Move(); } else if (gameBall.position.X - paddle.position.X < 300 && gameBall.position.Y < (paddle.center.Y - paddle.size.Y / 2)) { this.animateGary(gameTime); paddle.velocity = new Vector2(0, -5); paddle.Move(); } else { this.animateGary(gameTime); paddle.Move(); } paddle.withinScreen(); } } }