how to move a image

Started by
9 comments, last by Captain P 15 years, 9 months ago
Hello guyz, I am creating a simple shooting game using C++/allegro/Dev-C++. I have not gone very far. just managed to put an image of a "fighter" plane on the screen and move it. but the problem i face is that when i press the left/right arrow key, it moves too much of a distance for just one key press ie. instead of just moving one unit along "x-axis", it moves around one-fourth of the screen. heres the code for it. void moveOnX(){ xPrev = xCurrent; if (key[KEY_RIGHT] && xCurrent < 540) { xCurrent = xCurrent + 1; draw_sprite(screen, jetSprite, xCurrent, 440); rectfill(screen, xPrev, 440, xCurrent, 400, makecol(0, 0, 0)); } else if (key[KEY_LEFT] && xCurrent > 0) { xCurrent = xCurrent - 1; draw_sprite(screen, jetSprite, xCurrent, 440); rectfill(screen, xCurrent, 440, xPrev, 440, makecol(0, 0, 0)); } } where am i going wrong here? Cheers, danny P.S : Sorry if i posted in the wrong forum
Advertisement
Are you doing any sort of frame rate limiting? If you're not it could just be moving way too fast.
just write SDLDelay(200) at the end of the loop or something and the game will slow down considerably. Could also try a holddown timer for the action but that wouldn't look or feel good.

Video Game Programmer.
5 years in industry.

If your game runs with, lets say, 100 frames per second and you hold down the key for, lets say, 1 second, your movement code will execute 100 times. If you want it to be executed only once per keypress, you need come up with a system that allows you to see your keypresses as events rather than states, where you only have one "KEY_DOWN" event for each keypress.

Something simple could look like (pseudocode):

if(key[KEY_RIGHT])
{
while(key[KEY_RIGHT])
{ check_key_states() }
xCurrent = xCurrent + 1
...
}
}

This is not a very smart way of doing it since it will basically stall your game while you hold down the key, but you get the idea :)

Quote:Original post by vinodh dan
void moveOnX(){
xPrev = xCurrent;
if (key[KEY_RIGHT] && xCurrent < 540)
{
xCurrent = xCurrent + 1;
draw_sprite(screen, jetSprite, xCurrent, 440);
rectfill(screen, xPrev, 440, xCurrent, 400, makecol(0, 0, 0));
}
else if (key[KEY_LEFT] && xCurrent > 0)
{
xCurrent = xCurrent - 1;
draw_sprite(screen, jetSprite, xCurrent, 440);
rectfill(screen, xCurrent, 440, xPrev, 440, makecol(0, 0, 0));
}

}

Two things: first, you're duplicating code. Move the draw_sprite and rectfill lines outside the input check code - just call them once after you've checked for input. You may think you only need to refresh the screen after moving, but once you add enemies, bullets, moving terrain and all that, you'll soon find it's better to update the screen every frame anyway.

Second, you're not taking time into account. Multiply the speed by the time the last frame took and you will get a steady movement. You'll want to use floats rather than ints for positions and movement speeds to make it work correctly.
Create-ivity - a game development blog Mouseover for more information.
thanks a lot for the input guyz...
got it working thru captain-p's idea....
Check out this article for a good tutorial on time-based programming. Whenever there is something that happens through time (like movement), I use the method described in the article to have it run smooth. You might want to give your fighter plane some sort of xSpeed, change that through the user input, and then use that to calculate the new xCurrent in your timestep (s += v * dt works fine as so-called Euler-integration)
hey guyz,

i have another question.i press f to fire. but till the "bullets" move out of the screen (ie till the for loop ends) i am not able to move the plane. how do i solve this issue? i should be able to fire and then move... ie not wait till bullet disappears.

cheers,
vinodh
When you fire a bullet, you should only create a new bullet. Don't write a for loop that moves it, because as long as that loop runs, nothing else will be executed. Put the movement code in the main game loop instead. Keep in mind that movement - and many other activities - should be spread across multiple game cycles.
Create-ivity - a game development blog Mouseover for more information.
I am not sure I got your last point captain. Can you explain it a bit more clearly???

cheers,
vinodh

This topic is closed to new replies.

Advertisement