[Tutor] Continuous Shooting
Hugo Arts
hugo.yoshi at gmail.com
Thu Jan 12 14:39:20 CET 2012
On Thu, Jan 12, 2012 at 1:39 PM, ANKUR AGGARWAL <coolankur2006 at gmail.com> wrote:
> Hey
> I was making a demo shooting game and problem is that I want
> a continuous stream of bullets. As of now on pressing the space key only one
> bullet comes out of the plane (I want this to be continuous stream). On
> pressing space key again bullet starts from its initial point. My problem in
> the code is that I am able to make a single object of Bullet only (thats why
> it is throwing single bullet) and unable to find the another logic. Please
> help me out. Attaching the files along with this mail.
>
Well, the solution seems like it should be simple. Right now, you
create a single bullet object and associate it with the plane, then
you center the bullet on the plane when you press spacebar. What you
should do instead is create a new bullet object every time the
spacebar is pressed (or, alternatively, create a new one every so
often while the spacebar is held down). I'd suggest you keep a
separate sprite group for all bullets, and put it in a place where all
your classes can access it (at the module level seems like a good
idea).
Oh, and do keep in mind that if you do pygame.image.load() every time
you make a new bullet, you will have the same image in memory multiple
times, which is quite wasteful. Instead, you should load it once and
reuse the same image for all your Bullets.
HTH,
Hugo
More information about the Tutor
mailing list