Sign in to follow this  
MrMonkeh

Allegro moving rectangle help

Recommended Posts

Hi, I have a simple question, probably super easy too, so I'd appreciate if someone could help me out here!

I'm trying to recreate Pong, but far from that...I'm at the stage of getting things on the screen :P
Basically, I want to move the rectangles on the sides, but can't figure out how in a productive manner.

And before I get any farther, I am running Allegro 4.2, not the new version, 5.

I have this code:
[code]if (key[KEY_S])
{
y1++;
rest(10);
y2++;
rest(10);
rectfill(screen,x1,y1,x2,y2,15);
}[/code]

Sorry if it looks weird, trying to figure out how to put code on here...[i]anyways...[/i]Basically for anyone who has ran Allegro, this simply redraws another rectangle, so it ends up looking like there's a rectangle on the whole left side. I've tried doing [code] rectfill(screen,x1,y1,x2,y2,0);[/code] to sort of "erase" it, but then when moving it just looks horrible (flicking black).

Any help would be appreciated, or point me in the right direction!

Thank you,
MrMonkeh

Share this post


Link to post
Share on other sites
There are many ways to make updates to the screen. One simple way is to redraw everything every frame. This way is very easy. Doing like you are trying to do, drawing the background over the previous location of the paddle, should work fine in this simple case with single coloured background and non overlapping objects.

You should call this before updating y1 and y2.
[code]rectfill(screen,x1,y1,x2,y2,0);[/code]
Also get rid of the calls to [i]rest[/i] here. I don't see why you have them here. You know what [i]rest[/i] do, do you? It waits the number of milliseconds. This might explain why you get "flicking black". If you want to call [i]rest[/i] do that somewhere else once per frame instead.

Your approach is probably faster than redrawing everything every frame but the question is how much? Does it really matter? I have never used Allegro so I don't know.

Share this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1317892687' post='4869719']
There are many ways to make updates to the screen. One simple way is to redraw everything every frame. This way is very easy. Doing like you are trying to do, drawing the background over the previous location of the paddle, should work fine in this simple case with single coloured background and non overlapping objects.

You should call this before updating y1 and y2.
[code]rectfill(screen,x1,y1,x2,y2,0);[/code]
Also get rid of the calls to [i]rest[/i] here. I don't see why you have them here. You know what [i]rest[/i] do, do you? It waits the number of milliseconds. This might explain why you get "flicking black". If you want to call [i]rest[/i] do that somewhere else once per frame instead.

Your approach is probably faster than redrawing everything every frame but the question is how much? Does it really matter? I have never used Allegro so I don't know.
[/quote]

I have the rest to actually control how fast the box moves. When I take the rest out, you see a quick flicker of white, and the box is way gone out of the picture.

Share this post


Link to post
Share on other sites
Controlling the speed this way will not work if you have many objects moving with different speed. Use [i]rest[/i] to regulate the time between frames instead· Update the positions with whatever value to get the rectangle move with the speed you want. You may want to store the positions as floating point numbers to get better precision and be able to move slower than 1 pixel per update.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this