I just had a pretty basic question regarding graphics in C++ using the header graphics.h. How would you take a shape or just regular text and randomly move it around the screen like you would see in a screen saver? Just looping it to randomly display it in different areas is simple enough, but I'm trying to go for that constant moving effect. Any help would be greatly apprechiated.

compare the current coordinates of text or picture with screen coordinate...and if it's larger than that...then take suitable action to correct it

I just had a pretty basic question regarding graphics in C++ using the header graphics.h. How would you take a shape or just regular text and randomly move it around the screen like you would see in a screen saver? Just looping it to randomly display it in different areas is simple enough, but I'm trying to go for that constant moving effect. Any help would be greatly apprechiated.

Here are some steps I would take:

1. Setup Coordinate Parameters and Direction Flags

If you are in full screen mode then all you will need to do is obtain the screen size i.e.(800x600).

If you are dealing with a rectangle box, then you will need to set up 4 variables that will determine when the object needs to "bounce off" and change direction.

You will also need to set up 2 flags that will determine the movement along the x and y axis.

Not only will you have to take in consideration of your "borders", but your objects size. This can be done by subtracting the size of your object, which is another rectangle too, from your parameters. This will make sure your object doesn't slide off the screen, since the object's coords will come from the top-left point.

2. Test your object with these parameters

You will need to set up some conditions to test if your object didn't "hit" the parameter you set up.

When the object hits a parameter then you will need to turn off or turn on one of the direction flags.

Depending on what direction you are in you will need to decrement or increment the value of your object's coordiantes.

Moving the object by one pixel will give it the smooth motion effect you want.

3. Draw your object in its new location

You can adjust the speed of the object by how often the trigger goes off on the Draw function, which will probably be in milliseconds.

Sorry I can't give any example code for this. I've only worked with graphics using VB.NET on a version of PONG. So I think the theory can get you rolling further.

This article has been dead for over six months. Start a new discussion instead.