Skip to main content
GameDev.net gamedev.net
🔒 Locked

Allegro moving rectangle help

Started by MrMonkeh Oct 6, 2011 at 1:03 AM 2 replies 2.7k views
Original Post
MrMonkeh
MrMonkeh
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
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:
if (key[KEY_S])
{
y1++;
rest(10);
y2++;
rest(10);
rectfill(screen,x1,y1,x2,y2,15);
}


Sorry if it looks weird, trying to figure out how to put code on here...anyways...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 rectfill(screen,x1,y1,x2,y2,0); 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
Wooh
Wooh
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.
rectfill(screen,x1,y1,x2,y2,0);
Also get rid of the calls to rest here. I don't see why you have them here. You know what rest do, do you? It waits the number of milliseconds. This might explain why you get "flicking black". If you want to call rest 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.
MrMonkeh
MrMonkeh

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.
rectfill(screen,x1,y1,x2,y2,0);
Also get rid of the calls to rest here. I don't see why you have them here. You know what rest do, do you? It waits the number of milliseconds. This might explain why you get "flicking black". If you want to call rest 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.


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.
Wooh
Wooh
Controlling the speed this way will not work if you have many objects moving with different speed. Use rest 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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.