Shooting Mechanism Demo
As a follow up to the background scrolling example, I’ve modified the 1945 code again to emphasize how the shooting mechanism works.

Basically, the player is given two options for shooting:
- Every distinct click of the left mouse button guarantees one shot.
- Holding the left mouse button will make shots appear at regular intervals (rapid fire).
In a way, this gives the player the incentive to click if he wants a more rapid rate of fire, since you can effectively increase the intervals at which rapid fire gives off shots.
Feel free to ask questions!
shooting mechanism source code
License
This work is published under a Creative Commons Attribution 2.5 License.


January 23rd, 2007 at 10:57 pm
hello,
it’s real funny because i went searching for some help on a solution to my “rate of fire” problem and it came up with this blog. the funny part is im making a similar game with the same sprite sheet (unknowingly of course).
anyway, i was looking for some help on making a standard rate of fire. i am having the problem that when the space bar (the key that activates the bullets), all the bullets fire at one time.
im new to game creation and i am using it as a way to learn how to program with c++.
i was wondering if you had any ideas for me or could help.
thanks,
nick
January 30th, 2007 at 7:12 pm
Hi Nick. I think that making a game is a great way to learn C++, or programming in general. I hope you succeed in this!
Regarding your question, I think that I could help you better if you would send me or post a snippet of the code which deals with firing.
In the meantime, my guess is that you coded your game to fire bullets when the spacebar is held down. However, this check is done around 20 to 60 times per second (depending on the way you constructed your game loop). When you press the spacebar, it’s not actually held down for just 1/60th of a second; it could be half a second for instance, and in such a case your plane would fire 30 shots in a half a second, which is probably what you’re experiencing.
To remedy this, I suggest that you keep a copy of the previous state of your spacebar. And instead of checking whether the spacebar is held down or not, do a check on whether the state of the spacebar changed - just compare the previous state and the current state. If the state has changed, that’s when you check whether or not the spacebar is held down. If both conditions are fulfilled, then make a bullet fire.
I hope this all made sense - I will try to elaborate further in another post, hopefully with source code.
Good luck with your game!