protected override void Update(GameTime gameTime) { gameObjects.TouchInput = new TouchInput(); GetTouchInput(); playerPaddle.Update(gameTime, gameObjects); computerPaddle.Update(gameTime, gameObjects); ball.Update(gameTime, gameObjects); score.Update(gameTime, gameObjects); //ballOrigin = new Vector2(ballRectangle.Width / 2, ballRectangle.Height / 2); // used for ball spinning base.Update(gameTime); }
/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } playerPaddle.Update(gameTime, gameObjects); opponentPaddle.Update(gameTime, gameObjects); ball.Update(gameTime, gameObjects); score.Update(gameTime, gameObjects); base.Update(gameTime); }
/// <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) { switch (currentGameState) { case GameState.Menu: break; case GameState.Game: ball.Update(gameTime); CheckCollision(); player1.Update(gameTime); player2.Update(gameTime); KeyboardState newState = Keyboard.GetState(); if (newState.IsKeyDown(Keys.W)) { player1.Up(bounds); } if (newState.IsKeyDown(Keys.S)) { player1.Down(bounds); } if (newState.IsKeyDown(Keys.Up)) { player2.Up(bounds); } if (newState.IsKeyDown(Keys.Down)) { player2.Down(bounds); } break; case GameState.Paused: break; } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here //animatedSprite.Update(); base.Update(gameTime); }
/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } InputManager.UpdateCurrentStates(); // TODO: Add your update logic here paddleOne.Update(gameTime); paddleTwo.Update(gameTime); ball.Update(gameTime); collisionManager.Update(gameTime); InputManager.UpdatePreviousStates(); base.Update(gameTime); }
public void Update(float delta) { if (Keyboard.IsKeyDown(KeyCode.Space)) { _ball.CanMove = true; } _leftPaddle.Update(delta); _rightPaddle.Update(delta); _ball.Update(delta); if (_ball.CollidesWithPaddle(_leftPaddle)) { _ball.BounceFromPaddle(_leftPaddle); Assets.PaddleHit.Play(); } else if (_ball.CollidesWithPaddle(_rightPaddle)) { _ball.BounceFromPaddle(_rightPaddle); Assets.PaddleHit.Play(); } else if (_ball.CollidesWithBoardSide()) { _ball.BounceFromSide(); Assets.WallHit.Play(); } if (_ball.CollidesWithEndOfPlayfield(out var left)) { if (left) { RightScore++; } else { LeftScore++; } _ball.Center(); Assets.OutsidePlayfield.Play(); _ball.CanMove = false; } }
/// <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) { inputState.Update(); // Exit game if (inputState.CurrentGamePadState.Buttons.Back == ButtonState.Pressed || inputState.CurrentKeyboardState.IsKeyDown(Keys.Escape)) { Exit(); } // Update GameObjects paddleOne.Update(gameTime, inputState, GraphicsDevice); paddleTwo.Update(gameTime, inputState, GraphicsDevice); ball.Update(gameTime); // Check collisions if (ball.BoundingBox.Intersects(paddleOne.BoundingBox) || ball.BoundingBox.Intersects(paddleTwo.BoundingBox)) { ball.Bounce(true); } if (ball.BoundingBox.Intersects(topWall) || ball.BoundingBox.Intersects(bottomWall)) { ball.Bounce(false); } // Winning conditions if (ball.BoundingBox.Intersects(leftWall)) { paddleTwo.Score++; ball.Reset(GraphicsDevice); } if (ball.BoundingBox.Intersects(rightWall)) { paddleOne.Score++; ball.Reset(GraphicsDevice); } base.Update(gameTime); }
/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } KeyboardState key = Keyboard.GetState(); // update the left paddle if (key.IsKeyDown(Keys.W)) { padLeftVel = -1; } else if (key.IsKeyDown(Keys.S)) { padLeftVel = 1; } else { padLeftVel = 0; } padLeft.Update(gameTime, padLeftVel); // update the right paddle if (key.IsKeyDown(Keys.Up)) { padRightVel = -1; } else if (key.IsKeyDown(Keys.Down)) { padRightVel = 1; } else { padRightVel = 0; } padRight.Update(gameTime, padRightVel); // manages collision (strokes) if (ball.CollisionRectangle.Intersects(padLeft.CollisionRectangle) || ball.CollisionRectangle.Intersects(padRight.CollisionRectangle)) { ball.Stroke(); ball.PlaySound(); ball.Accelerate(); } else { if (ball.LeftDrop) { scoreRight++; Spawn(false); } else if (ball.RightDrop) { scoreLeft++; Spawn(true); } } // update ball ball.Update(gameTime); base.Update(gameTime); }
/// <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 //mouse resets score when clicked mouseStateCurrent = Mouse.GetState(); if (mouseStateCurrent.LeftButton == ButtonState.Pressed && mouseStatePrev.LeftButton == ButtonState.Released) { leftCount = rightCount = 0; } mouseStatePrev = mouseStateCurrent; left.Update(gameTime); right.Update(gameTime); mBall.Update(gameTime); // new logic if (mBall.center.X - mBall.Source.Width <= left.center.X && mBall.center.X - mBall.Source.Width >= left.center.X - CUSHION) // range "in" the paddle where the paddle can return the ball { float dist = mBall.center.Y - left.center.Y + mBall.Radius(); if (Math.Abs(dist) <= left.Source.Height / 2) { mBall.mSpeed.Y = (float)Math.Pow(Math.Abs(dist), 1.5); mBall.mDirection.X = 1; if (dist > 0) { mBall.mDirection.Y = 1; } else { mBall.mDirection.Y = -1; } mBall.mSpeed.X += X_SCALE / (Math.Abs(dist) + .5f); // the .5f is there so we don't divide by 0 } } if (mBall.center.X + mBall.Source.Width >= right.center.X && mBall.center.X + mBall.Source.Width <= right.center.X + CUSHION) { float dist = mBall.center.Y - right.center.Y + mBall.Radius(); if (Math.Abs(dist) <= right.Source.Height / 2) { mBall.mSpeed.Y = (float)Math.Pow(Math.Abs(dist), 1.5); mBall.mDirection.X = -1; if (dist > 0) { mBall.mDirection.Y = 1; } else { mBall.mDirection.Y = -1; } mBall.mSpeed.X += X_SCALE / (Math.Abs(dist) + .5f); } } if (mBall.Position.Y > GraphicsDevice.Viewport.Height - mBall.Source.Height || mBall.Position.Y < 0) //top and bottom { mBall.mDirection.Y *= -1; } if (mBall.mSpeed.X >= mBall.MaxSpeed()) { mBall.mSpeed.X = mBall.MaxSpeed(); } if (mBall.mSpeed.Y >= mBall.MaxSpeed()) { mBall.mSpeed.Y = mBall.MaxSpeed(); } if (mBall.Position.X < 0) { leftCount++; //score mBall.Position.X = GraphicsDevice.Viewport.Width / 2 - mBall.Source.Width / 2; mBall.Position.Y = GraphicsDevice.Viewport.Height / 2 - mBall.Source.Height / 2; mBall.mSpeed = Vector2.Zero; mBall.mDirection = Vector2.Zero; left.speedReset(); right.speedReset(); } if (mBall.Position.X + mBall.Source.Width > GraphicsDevice.Viewport.Width) { rightCount++; //score mBall.Position.X = GraphicsDevice.Viewport.Width / 2 - mBall.Source.Width / 2; mBall.Position.Y = GraphicsDevice.Viewport.Height / 2 - mBall.Source.Height / 2; mBall.mSpeed = Vector2.Zero; mBall.mDirection = Vector2.Zero; left.speedReset(); right.speedReset(); } //disallow paddles from going off screen if (left.Position.Y + left.Source.Height > GraphicsDevice.Viewport.Height) { left.Position.Y = GraphicsDevice.Viewport.Height - left.Source.Height; } if (left.Position.Y < 0) { left.Position.Y = 0; } if (right.Position.Y + right.Source.Height > GraphicsDevice.Viewport.Height) { right.Position.Y = GraphicsDevice.Viewport.Height - right.Source.Height; } if (right.Position.Y < 0) { right.Position.Y = 0; } //tickCounter++; //interval to increase paddle speed //if (tickCounter == 100) //{ // left.speedUp(); // right.speedUp(); // tickCounter = 0; //} base.Update(gameTime); }
/// <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); }
protected override void Update(GameTime gameTime) { if (input.GamePadState.Buttons.Back == ButtonState.Pressed || input.KeyboardState.IsKeyDown(Keys.Escape)) { Exit(); } // start management if (input.KeyboardState.IsKeyDown(Keys.Enter)) { if (gameState == GameState.Start) { gameState = GameState.Serve; } else if (gameState == GameState.Serve) { gameState = GameState.Play; } else if (gameState == GameState.Done) { gameState = GameState.Serve; ball.Reset(); player1Score = 0; player2Score = 0; if (winningPlayer == 1) { servingPlayer = 2; } else { servingPlayer = 1; } } } if (gameState == GameState.Serve) { ball.dy = random.Next(-50, 50); if (servingPlayer == 1) { ball.dx = random.Next(140, 200); } else { ball.dx = -random.Next(140, 200); } } else if (gameState == GameState.Play) { if (ball.Collides(player1)) { ball.dx = -ball.dx * 1.03f; ball.x = player1.x + player1.transform.Width; paddleHitSound.Play(); if (ball.dy < 0) { ball.dy = -random.Next(10, 150); } else { ball.dy = random.Next(10, 150); } } if (ball.Collides(player2)) { ball.dx = -ball.dx * 1.03f; ball.x = player2.x - ball.transform.Width; paddleHitSound.Play(); if (ball.dy < 0) { ball.dy = -random.Next(10, 150); } else { ball.dy = random.Next(10, 150); } } if (ball.y < 0) { ball.y = 0; ball.dy = -ball.dy; wallHitSound.Play(); } if (ball.y > VIRTUAL_HEIGHT - ball.transform.Height) { ball.y = VIRTUAL_HEIGHT - ball.transform.Height; ball.dy = -ball.dy; wallHitSound.Play(); } if (ball.x < 0) { servingPlayer = 1; player2Score++; scoreSound.Play(); if (player2Score == 10) { winningPlayer = 2; gameState = GameState.Done; } else { ball.Reset(); gameState = GameState.Serve; } } if (ball.x > VIRTUAL_WIDTH) { servingPlayer = 2; player1Score++; scoreSound.Play(); if (player1Score == 10) { winningPlayer = 1; gameState = GameState.Done; } else { ball.Reset(); gameState = GameState.Serve; } } } if (input.KeyboardState.IsKeyDown(Keys.W)) { player1.dy = -PADDLE_SPEED; } else if (input.KeyboardState.IsKeyDown(Keys.S)) { player1.dy = PADDLE_SPEED; } else { player1.dy = 0; } if (input.KeyboardState.IsKeyDown(Keys.Up)) { player2.dy = -PADDLE_SPEED; } else if (input.KeyboardState.IsKeyDown(Keys.Down)) { player2.dy = PADDLE_SPEED; } else { player2.dy = 0; } float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; if (gameState == GameState.Play) { ball.Update(delta); } player1.Update(delta); player2.Update(delta); base.Update(gameTime); }
/// <summary> /// Här sker spelets updatering av allt. Mynten rör sig genom att deras position uppdateras ständigt genom en metod i coin klassen som update loopar igenom med en foreach loop /// Här i update går man bara igenom listan som mynten sitter i och addar nya mynt med en timer som går av varje sekund. Bakgrundsklassen anropas också och update /// kollar kollision mellan mus och respektive bakgrundsbild genom en bool. Kolission mellan paddel, boll, bonus kollas också genom att Update anropar respektive klass. /// Här i Update kollas också vilken paddel som har träffat bollen och ifall bollen träffat bonusen med bools. Ifall båda boolsen är sanna så anropar update paddel klassen /// och gör den paddel som har sin bool false mindre. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> #region Update metod för main protected override void Update(GameTime gameTime) { //Ifall menyn är borta, börja räkna tiden if (menuScreen.GameState == false) { globalTimer += gameTime.ElapsedGameTime.TotalSeconds; } //För varje sekund så addas ett nytt coin objekt till coin listan, samtidigt som timern resettar sig själv if (menuScreen.GameState == true) { coinTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (coinTimer <= 0) { coins.Add(new Coin(Content.Load <Texture2D>("Coin"), new Vector2(random.Next(0, 780), 0))); coinTimer = 1; } if (coins.Count > 20) { coins.RemoveAt(19); } foreach (Coin coin in coins) { coin.Update(); } } //Musens position på skärmen state = Mouse.GetState(); mousePos = new Point(state.X, state.Y); //Ifall musens position är innanför respektive bakgrundsbild och man vänsterklickar, så väljs bakgrund //Intersect håller koll på vilken bakgrund det är #region if (state.LeftButton == ButtonState.Pressed && backGround1.BackGroundHitbox.Contains(mousePos)) { backGround1.Intersect = true; } if (state.LeftButton == ButtonState.Pressed && backGround2.BackGroundHitbox.Contains(mousePos)) { backGround2.Intersect = true; } if (state.LeftButton == ButtonState.Pressed && backGround3.BackGroundHitbox.Contains(mousePos)) { backGround3.Intersect = true; } if (state.LeftButton == ButtonState.Pressed && backGround4.BackGroundHitbox.Contains(mousePos)) { backGround4.Intersect = true; } if (state.LeftButton == ButtonState.Pressed && backGround5.BackGroundHitbox.Contains(mousePos)) { backGround5.Intersect = true; } #endregion //Kollar ifall man tryckt på en bakgrund, och ifall musen är innanför objektets hitbox menuScreen.Update(backGround1, backGround2, backGround3, backGround4, backGround5); if (score1.Pause == false && globalTimer > 8) { //Kollar poängen score1.Update(leftPaddle, rightPaddle, ball1, Window, box, gameTime); //Paddlarnas rörelse och bollens rörelse ifall spelet är igång if (score1.StartRound == true) { ball1.Update(); leftPaddle.Update(box); rightPaddle.Update(box); } //Bollens kolission med paddlarna, anropar boll klassen ifall true if (ball1.BallHitbox.Intersects(leftPaddle.PaddleHitbox)) { ball1.Colission(); } if (ball1.BallHitbox.Intersects(rightPaddle.PaddleHitbox)) { ball1.Colission(); } box.Update(ball1, gameTime); //Kollar vilken paddel som knuffade bollen till bonusen, så att man kan avgöra vilken sida som träffade den //Ifall höger paddel träffade bonusen med bollen blir vänster paddel mindre och tvärtom #region if (ball1.BallHitbox.Intersects(leftPaddle.PaddleHitbox)) { leftPaddle.Intersect = true; rightPaddle.Intersect = false; } if (ball1.BallHitbox.Intersects(rightPaddle.PaddleHitbox)) { rightPaddle.Intersect = true; leftPaddle.Intersect = false; } if (leftPaddle.Intersect == true && box.Intersect == true) { rightPaddle.PaddleHitbox = new Rectangle((int)rightPaddle.PaddlePos.X, (int)rightPaddle.PaddlePos.Y, 21, 80); box.Intersect = false; rightPaddle.Small = true; rightPaddle.Big = false; } if (rightPaddle.Intersect == true && box.Intersect == true) { leftPaddle.PaddleHitbox = new Rectangle((int)leftPaddle.PaddlePos.X, (int)leftPaddle.PaddlePos.Y, 21, 80); box.Intersect = false; leftPaddle.Small = true; leftPaddle.Big = false; } #endregion } base.Update(gameTime); }