Make ball move automatically

So my problem is I need to make the ball move around the screen automatically. So when it reached one of the screen it needs to go the other way. If I could also get help on how I would use trig if the ball hits a corner to make it bounce a certain way that would be amazing too as I am going to do that later!

At the moment my ball will just keep travelling right.
And yes I have a variable for my screen width and screen height as well as for my ball width and ball height.


int ballx = 0;
int bally = 240;

do
{
ClearScreen();

MoveSprite( iPlayerSprite, ballx, bally );

if ( ( x + ballw ) >= 0 )
{
x = x + 10;
}
if ( ( x + ballw ) >= screenw)
{
x = x - 10;
}
Last edited on
It's easier to do something like this...

1
2
3
4
5
6
7
8
9
int ballSpeedX = 10;

ballPositionX += ballSpeedX;

if( ballPositionX > screenWidth ||
    ballPositionX < 0 )
{
   ballSpeedX *= -1;
}


Depending on how your ball is positioned, you may need to take the radius/diameter into account when checking it's position against the screen size.
Thank you :)

That helps a lot
Topic archived. No new replies allowed.