Jump to content
  • Advertisement
Sign in to follow this  
MrMonkeh

Allegro moving rectangle help

This topic is 2449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:
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

Share this post


Link to post
Share on other sites
Advertisement
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.

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.
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.

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 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!