/// <summary> /// method allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // set the background's initial position _backgroundPosition = new Rectangle(0, 0, 640, 512); // create a ball object string spriteName = "Ball"; Vector2 position = new Vector2(300, 200); Vector2 velocity = new Vector2(2, 2); _ball = new Ball(Content, spriteName, position, velocity); // _ball = new Ball(Content, "Ball", new Vector2(300, 200), new Vector2( 2, 2)); // make the ball active _ball.Active = true; base.Initialize(); }
/// <summary> /// method to move the ball forward and bounce off the sites of the screen /// </summary> /// <param name="_ball">ball object</param> private void UpdateBallMovement(Ball _ball) { if (_ball.Active) { BounceOffWalls(); _ball.Position += _ball.Velocity; } }
/// <summary> /// spawn the ball at a random location on the screen /// </summary> /// <param name="ball">ball object</param> private void Spawn(Ball ball) { // find a valid location to spawn the ball int ballXPosition = _randomNumbers.Next(WINDOW_WIDTH - CELL_WIDTH); int ballYPosition = _randomNumbers.Next(WINDOW_HEIGHT - CELL_HEIGHT); // set ball's new position and reverse direction ball.Position = new Vector2(ballXPosition, ballYPosition); ball.Velocity = -ball.Velocity; }
/// <summary> /// method allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // set the background's initial position _backgroundPosition = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // create a ball object string spriteName = "Ball"; Vector2 position = new Vector2(300, 200); Vector2 velocity = new Vector2(2, 2); _ball = new Ball(Content, spriteName, position, velocity); // _ball = new Ball(Content, "Ball", new Vector2(300, 200), new Vector2( 2, 2)); // make the ball active _ball.Active = true; // make mouse visible on game this.IsMouseVisible = true; base.Initialize(); }
/// <summary> /// method to determine if the mouse is on the ball /// </summary> /// <returns></returns> private bool MouseOnBall(Ball ball) { bool mouseClickedOnBall = false; // get the current state of the mouse MouseState mouseState = Mouse.GetState(); // mouse over ball if ((_mouseNewState.X > ball.Position.X) && (_mouseNewState.X < (ball.Position.X + 64)) && (_mouseNewState.Y > ball.Position.Y) && (_mouseNewState.Y < (ball.Position.Y + 64))) { mouseClickedOnBall = true; } return mouseClickedOnBall; }
/// <summary> /// method allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // set the background's initial position _backgroundPosition = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // create a list of Ball objects _balls = new List<Ball>(); // add balls to the list Ball ball01 = new Ball(Content, "Ball", 32, new Vector2(300, 200), new Vector2(4, 4)); _balls.Add(ball01); Ball ball02 = new Ball(Content, "Ball", 32, new Vector2(100, 400), new Vector2(-2, -2)); _balls.Add(ball02); Ball ball03 = new Ball(Content, "small_ball", 16, new Vector2(400, 400), new Vector2(4, -4)); _balls.Add(ball03); // make the balls active ball01.Active = true; ball02.Active = true; ball03.Active = true; // make mouse visible on game this.IsMouseVisible = true; base.Initialize(); }
/// <summary> /// method to calculate the distance between the centers of two balls /// </summary> /// <param name="ball1">first ball</param> /// <param name="ball2">second ball</param> /// <returns></returns> public double DistanceBetweenBalls(Ball ball1, Ball ball2) { double distance; distance = Vector2.Distance(ball1.Center, ball2.Center); return distance; }
/// <summary> /// method to bounce ball of walls /// </summary> public void BounceOffWalls(Ball ball) { // ball is at the top or bottom of the window, change the Y direction if ((ball.Position.Y > WINDOW_HEIGHT - ball.Radius * 2) || (ball.Position.Y < 0)) { ball.Velocity = new Vector2(ball.Velocity.X, -ball.Velocity.Y); } // ball is at the left or right of the window, change the X direction else if ((ball.Position.X > WINDOW_WIDTH - ball.Radius * 2) || (ball.Position.X < 0)) { ball.Velocity = new Vector2(-ball.Velocity.X, ball.Velocity.Y); } }
/// <summary> /// method to change the velocity of the balls when they collide /// </summary> /// <param name="balls">list of all balls</param> /// <param name="checkBall">ball being check</param> public void BounceOffBalls(List<Ball> balls, Ball checkBall) { foreach (Ball ball in balls) { if (ball != checkBall) { if (DistanceBetweenBalls(checkBall, ball) < checkBall.Radius + ball.Radius) { checkBall.Velocity = -checkBall.Velocity; } } } }
/// <summary> /// method to bounce ball of walls /// </summary> //public void BounceOffWalls(Ball ball) //{ // // ball is at the top or bottom of the window, change the Y direction // if ((ball.Position.Y > (WINDOW_HEIGHT - CELL_HEIGHT) - ball.Radius * 2) || (ball.Position.Y < CELL_HEIGHT)) // { // ball.Velocity = new Vector2(ball.Velocity.X, -ball.Velocity.Y); // } // // ball is at the left or right of the window, change the X direction // else if ((ball.Position.X > (WINDOW_WIDTH - CELL_WIDTH) - ball.Radius * 2) || (ball.Position.X < CELL_HEIGHT)) // { // ball.Velocity = new Vector2(-ball.Velocity.X, ball.Velocity.Y); // } //} public void BounceOffWalls(Ball ball) { Rectangle ballRectangle = new Rectangle((int)ball.Position.X, (int)ball.Position.Y, (int)(ball.Radius * 2), (int)(ball.Radius * 2)); foreach (Wall wall in _walls) { Rectangle wallRectangle = new Rectangle((int)wall.Position.X, (int)wall.Position.Y, CELL_WIDTH, CELL_HEIGHT); if (Rectangle.Intersect(ballRectangle, wallRectangle) != null) { MessageBox(new IntPtr(0), "I hit a wall!!", "Debug Message", 0); } } //// ball is at the top or bottom of the window, change the Y direction //if ((ball.Position.Y > (WINDOW_HEIGHT - CELL_HEIGHT) - ball.Radius * 2) || (ball.Position.Y < CELL_HEIGHT)) //{ // ball.Velocity = new Vector2(ball.Velocity.X, -ball.Velocity.Y); //} //// ball is at the left or right of the window, change the X direction //else if ((ball.Position.X > (WINDOW_WIDTH - CELL_WIDTH) - ball.Radius * 2) || (ball.Position.X < CELL_HEIGHT)) //{ // ball.Velocity = new Vector2(-ball.Velocity.X, ball.Velocity.Y); //} }