public void AddBall() { Ball ball = new Ball() { Radius = BallRadius }; ball.Randomize(_graphicsDevice.Viewport.Bounds); _balls.Add(ball); }
public void Initialize() { _whiteTex = SolidColorTexture(_graphicsDevice, Color.White); Ball ball1 = new Ball() { Radius = BallRadius }; ball1.Randomize(_graphicsDevice.Viewport.Bounds); _balls = new List<Ball> { ball1 }; }
private void UpdateBall(Ball ball, GameTime gameTime) { Vector2 newPosition = ball.Position + ball.Direction * (float)gameTime.ElapsedGameTime.TotalSeconds; Vector2 newDirection = ball.Direction; if (newPosition.X < _graphicsDevice.Viewport.X + ball.Radius) { newPosition = new Vector2(_graphicsDevice.Viewport.X + BallRadius, newPosition.Y); newDirection = new Vector2(newDirection.X * -1, newDirection.Y); } if (newPosition.X >= _graphicsDevice.Viewport.X + _graphicsDevice.Viewport.Width - ball.Radius) { newPosition = new Vector2(_graphicsDevice.Viewport.X + _graphicsDevice.Viewport.Width - ball.Radius, newPosition.Y); newDirection = new Vector2(newDirection.X * -1, newDirection.Y); } if (newPosition.Y < _graphicsDevice.Viewport.Y + ball.Radius) { newPosition = new Vector2(newPosition.X, _graphicsDevice.Viewport.Y + ball.Radius); newDirection = new Vector2(newDirection.X, newDirection.Y * -1); } if (newPosition.Y >= _graphicsDevice.Viewport.Y + _graphicsDevice.Viewport.Height - ball.Radius) { newPosition = new Vector2(newPosition.X, _graphicsDevice.Viewport.Y + _graphicsDevice.Viewport.Height - ball.Radius); newDirection = new Vector2(newDirection.X, newDirection.Y * -1); BottomHitCount++; } ball.Position = newPosition; ball.Direction = newDirection; Rectangle paddleRect = new Rectangle((int)_paddleX, _graphicsDevice.Viewport.Height - PaddleBottomMargin, PaddleWidth, PaddleHeight); if (paddleRect.Intersects(ball.Bounds)) Bounce(ball, gameTime); }
private void Bounce(Ball ball, GameTime gameTime) { Vector2 newPosition = ball.Position - ball.Direction * (float)gameTime.ElapsedGameTime.TotalSeconds; Vector2 newDirection = new Vector2(ball.Direction.X, ball.Direction.Y * -1); ball.Position = newPosition; ball.Direction = newDirection; }