Question

In my pong game, I have a ball and two paddles. I want it so that when ball.ballRect.x < 5 is true, score.greenScore increments like so: score.greenScore++;

And that works well, but I also want it so that the ball moves back to the center of the screen.

So in Game1.cs I did this:

public void ScoreAdder()
    {
        if (ball.ballRect.X < 5)
        {
            score.blueScore++;
            ball.ballRect = new Rectangle((int)400, (int)250, ball.ballTexture.Width, ball.ballTexture.Height);
        }

    }

It goes back to the center and adds the score, but now it won't listen to collisions.

In my Ball.cs I only draw the rectangle, like:

spriteBatch.Draw(ballTexture, ballRect, Color.White);

Because when I use Vector2 position, the ball won't even appear on the screen.

Was it helpful?

Solution

Have you reset the Position when you respawn the ball to the center?

You could ever take advantage of properties with this, and have the Position point to the Rectangle or vice-versa.

public Vector2 BallPosition
{
    get
    {
        return ballPosition;
    }
    set
    {
        ballRectangle.X = value.X;
        ballRectangle.Y = value.Y;
        ballPosition = value;
    }
}
private Vector2 ballPosition

I'm not sure on how you handle collision and everything, but whenever you set the Position it will set the rectangle, you could also try the opposite, where you set the rectangle and it will sync with the position.

OTHER TIPS

I don't know how you're encapsulating the ball logic, but here's how I might try to do it. Using a class such as this guarantees all the internal logic of the ball is in one place so that the resulting drawing rectangle can be predicted based on the position and bounds. No more disappearing balls from using vector2's!

public class Ball
{
private Vector2 _position;
private Vector2 _velocity;
private Point _bounds;

public Vector2 Position { get { return _position; } set { _position = value; } }
public Vector2 Velocity { get { return _velocity; } set { _velocity = value; } }
public int LeftSide { get { return (int)_position.X - (_bounds.X / 2); } }
public int RightSide { get { return (int)_position.X + (_bounds.X / 2); } }
public Rectangle DrawDestination
{
    get
    {
        return new Rectangle((int)_position.X, (int)_position.Y, _bounds.X, _bounds.Y);
    }
}

public Ball(Texture2D ballTexture)
{
    _position = Vector2.Zero;
    _velocity = Vector2.Zero;
    _bounds = new Pointer(ballTexture.Width, ballTexture.Height);
}

public void MoveToCenter()
{
    _position.X = 400.0f;
    _position.Y = 250.0f;
}

public void Update(GameTime gameTime)
{
    _position += _velocity;
}
}

Then in your update/draw code:

class Game
{
void Update(GameTime gameTime)
{
    // ...

    ball.Update(gameTime);

    if(ball.LeftSide < LEFT_BOUNDS)
    {
        score.blueScore++;      
        ball.MoveToCenter();
    }
    if(Ball.RightSide > RIGHT_BOUNDS)
    {
        score.greenScore++;
        ball.MoveToCenter();
    }

    // ...
}

void Draw(GameTime gameTime)
{
    // ...

    _spriteBatch.Draw(ballTexture, ball.DrawDestination, Color.White);

    // ...
}
}

Remember to also modulate velocity of the ball by the elapsed frame time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top