public void Update(GameTime gameTime, KeyboardState keyboardState, Ball ball) { // this paddle is computer controlled. if (_cpu) { Vector2 center = _position + TEXTURE.Bounds.Center.ToVector2(); Vector2 ball_center = ball.GetBounds().Center.ToVector2(); if (center.Y < ball_center.Y) { _position += new Vector2(0, Math.Min(SPEED * gameTime.ElapsedGameTime.Milliseconds / 1000.0f, ball_center.Y - center.Y)); } else if (center.Y > ball_center.Y) { _position += new Vector2(0, -Math.Min(SPEED * gameTime.ElapsedGameTime.Milliseconds / 1000.0f, center.Y - ball_center.Y)); } } else { if (_up_key.HasValue && keyboardState.IsKeyDown(_up_key.Value)) { _position += new Vector2(0, -SPEED * gameTime.ElapsedGameTime.Milliseconds / 1000.0f); } if (_down_key.HasValue && keyboardState.IsKeyDown(_down_key.Value)) { _position += new Vector2(0, SPEED * gameTime.ElapsedGameTime.Milliseconds / 1000.0f); } } // check for ball collision, and respond accordingly. It's a bit weird // for the paddles to do this, but since there is only two of them and // the response is well defined and restricted, its fineeeee. if (GetBounds().Intersects(ball.GetBounds())) { Vector2 ballv = ball.GetVelocity(); ball.SetVelocity(new Vector2(-ballv.X, ballv.Y)); // This is a weird way to check if we are a 'left' or 'right' paddle. // We want to send the ball towards the middle. // This isn't really a good physical response, but the game is // pretty constrained, so it's fineeeee. if (GetBounds().Center.X < _screen_bounds.Center.X) { ball.SetPosition(new Vector2(GetBounds().Right, ball.GetBounds().Top)); } else { ball.SetPosition(new Vector2( GetBounds().Left - Ball.GetTexture().Width, ball.GetBounds().Top)); } } }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } KeyboardState keyboardState = Keyboard.GetState(); _ball.Update(gameTime); _left_paddle.Update(gameTime, keyboardState, _ball); _right_paddle.Update(gameTime, keyboardState, _ball); if (_ball.GetBounds().Left < _screen_bounds.Left) { // LEFT GOAL! } else if (_ball.GetBounds().Right > _screen_bounds.Right) { // RIGHT GOAL! } base.Update(gameTime); }
/// <summary> /// Handles the Tick event of the game_timer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> private void game_timer_Tick(object sender, EventArgs e) { if (!_paused && !_gameOver) { foreach (var g in _gameEntities) { g.Move(); } if (_secondPaddle.GetBounds().IntersectsWith(_ball.GetBounds())) { var diff = ((_secondPaddle.Y + PaddleHeight / 2.0) - (_ball.Y + BallWidth / 2.0)) / (PaddleHeight / 2.0); _ball.Angle = 2 * 90 - _ball.Angle + (45 * diff); if (_ball.Angle < 90 && _ball.Angle > 0) { _ball.Angle = 75; } else if (_ball.Angle < 0 && _ball.Angle > -90) { _ball.Angle = -75; } if (ts_random.Checked) { _ball.Speed = _rand.Next(1, 4); } _ball.X = _secondPaddle.x - BallWidth - 1; } if (_firstPaddle.GetBounds().IntersectsWith(_ball.GetBounds())) { var diff = ((_firstPaddle.Y + PaddleHeight / 2.0) - (_ball.Y + BallWidth / 2.0)) / (PaddleHeight / 2.0); _ball.Angle = 2 * 90 - _ball.Angle + (45 * -diff); if (_ball.Angle > 90 && _ball.Angle < 180) { _ball.Angle = 75; } else if (_ball.Angle < -90 && _ball.Angle > -180) { _ball.Angle = -75; } if (ts_random.Checked) { _ball.Speed = _rand.Next(1, 4); } _ball.X = _firstPaddle.x + PaddleWidth + 1; } if (_ball.x < 0 - BallWidth) { _gameEntities.Remove(_ball); _ball = new Ball(new SolidBrush(Color.White), BallWidth, _width / 2 - BallWidth / 2, _height / 2 - BallWidth / 2, _speed) { Angle = _rand.Next(-90, 90) - 180 }; _gameEntities.Add(_ball); _secondScore.AddPoint(1); if (_secondScore.Points > 9) { _gameOver = true; _gameOverMessage = "Player 2 wins"; _gameEntities.Remove(_ball); } } if (_ball.x > _width) { _gameEntities.Remove(_ball); _ball = new Ball(new SolidBrush(Color.White), BallWidth, _width / 2 - BallWidth / 2, _height / 2 - BallWidth / 2, _speed) { Angle = _rand.Next(-90, 90) }; _gameEntities.Add(_ball); _firstScore.AddPoint(1); if (_firstScore.Points > 9) { _gameOver = true; _gameOverMessage = "Player 1 wins"; _gameEntities.Remove(_ball); } } if (_ball.Y <= 0 || _ball.Y + BallWidth >= _height) { _ball.Angle = -_ball.Angle; } } }