Sign in to follow this  

how to move a image

This topic is 3455 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

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

Share this post


Link to post
Share on other sites
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 :)

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
You have a main loop, in which you check for input. You move the player sprite based on this input and then you draw it. These steps are repeated over and over again, until the program is closed.

Now, you added bullet firing code. You probably wrote a for loop that moves the bullets until they're outside the screen, right? Then tell me - why do you expect the player to react to input as long as that loop is running? There's no input checks being done inside that loop. There's no player updating going on in that loop. There's only bullet updating. Basically, that loop blocks the rest of your program.


Which is why I suggest only creating bullets when firing - and updating them inside the main loop. Hey, why don't you post your code so we can make some proper suggestions? (Tip: use the [source] or [code] tags to preserve formatting).

Share this post


Link to post
Share on other sites

This topic is 3455 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.

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