Jump to content
  • Advertisement
Sign in to follow this  

Allegro Question - Drawing During Button Press

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

Hey all, I'm still somewhat new to programming as a whole, but I have a decent grasp on a couple languages. Anyway, I've recently started looking into game programming again (now that I know more about programming) and have started making my own game using Allegro. Its a simple space shooting game kind of like Space Invaders or Galaga. Anyway, getting on to my problem. I tried to make it so the sprite for the bullet is created when you press the "Z" button.
/* bullet Creation and Movement Key Checks */   
              draw_sprite(buffer, bullet, bullet_x_position, bullet_y_position);
              bullet_y_position ++;
However, it doesn't seem to work. Just looking at the code, it seems like it technically should work, but I'm guessing Allegro doesn't allow you to draw sprites during a button press like I'm trying to do. I'm sure there is a way to get this to work and if anyone could help me out I greatly appreciate it. Thank you in advance.

Share this post

Link to post
Share on other sites
On a side note, you do know that if you don't include {} brackets after your if statement, only the next line is "inside" the if statement?

Share this post

Link to post
Share on other sites
Completely forgot the brackets. I added them and I can now at least see that the bullet is being created, but it disappears shortly afterward. Any suggestions, or is there any other part of the code you may need to see?

Edit: I figured out why it disappears. It is only drawing and moving forward during the key press (gah I feel dumb now). However, is there a way around the image only drawing during the button press? I need it to only move forward constantly when the button is pressed just like other old arcade shooters.

Share this post

Link to post
Share on other sites
You probably need to have something, er, more general.

You need a list of objects in your game (or maybe lists (or array, or vector, or whatever way you want to keep track of a bunch of similar things ) and your game loop should look something along the lines of:

do {
check what time it is.
handle getting input from player
move all the objects
draw all the objects
check what time it is.

wait until it's time to start the next frame,
which depends on your desired frame rate, and
how much time has elapsed in doing the above
steps between when you check the clock.

} while (it's not yet time to exit this program)

When the "fire" button is pressed, in your "handle getting input from the player" code, something along these lines should happen:

create a new bullet with an initial velocity
(probably upwards, a la space invaders) and
add it to the list of objects.

The "move all the objects" line in the main loop will advance this new bullet naturally, and this way, you can easily have more than one bullet active at the time.

The move bullet code could look something like:

x += vx;
y += vy;
check to see if I've hit anything, and if so, make it blow up.

This is assuming your bullet is an instance of a class that has fields x,y,vx,vy and this code is adjusting those fields for that bullet (so there's not a global x,y,vx,vy for a global bullet. Probably that's obvious, but then again maybe not.)

(for a game like space invaders you can typically get away with putting the collision detection in the bullet movement code, rather than having to detect if any object collides with any other, which is harder.)

The above is very simplified,

There are lots of complications you can add to the game loop to make it more sophisticated, and probably behave better and smoother in various situations, but the above is sort of the most basic thing that will work ok.

See for Fix your timestep. <-- more useful when you're using a physics engine, not so necessary for the likes of space invaders.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!