Yeah.. I applied the counter logic and it worked for me :) Thanks a lot :)

Regards
Ankur Aggarwal

On Fri, Jan 13, 2012 at 12:02 AM, Julian Marchant <[email protected]> wrote:

> First off, you need some sort of time management. As it is now, it'll run
> at variable speeds depending on how fast the processor is. Use
> pygame.time.Clock to limit the frame rate and/or use delta timing.
>
> As for your problem, all you need is a counter variable. Have the counter
> variable start at 0 and decrease it by 1 (or the amount of time passed if
> you're using delta timing) each time the loop happens. When the shoot key
> is pressed, create a bullet and set the counter to the amount of time you
> want to wait for the next bullet. Then, if the shoot key is held down while
> the loop variable is 0, create another bullet and reset the counter to the
> wait time.
>
> --- On *Thu, 1/12/12, ANKUR AGGARWAL <[email protected]>* wrote:
>
>
> From: ANKUR AGGARWAL <[email protected]>
> Subject: [pygame] Continuous Shooting
> To: [email protected], [email protected]
> Date: Thursday, January 12, 2012, 12:39 PM
>
>
> 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.
>
> import pygame
> from pygame.locals import *
> import random
>
> pygame.init()
> screen=pygame.display.set_mode((640,480),0,24)
> pygame.display.set_caption("Hit The Stone")
>
> class Plane(pygame.sprite.Sprite):
>     def __init__(self,bullet):
>         self.bullet=bullet
>         pygame.sprite.Sprite.__init__(self)
>         self.image=pygame.image.load('plane.gif').convert()
>         self.rect=self.image.get_rect()
>         self.rect.centerx=random.randint(0,screen.get_width())
>         self.distancefromcenter=30
>         self.rect.centery=screen.get_height()-self.distancefromcenter
>         self.dx=2
>         self.dy=2
>
>     def update(self):
>         pressed=pygame.key.get_pressed()
>         if pressed[K_DOWN]:
>             self.rect.centery+=self.dy
>         elif pressed[K_UP]:
>             self.rect.centery-=self.dy
>         elif pressed[K_LEFT]:
>             self.rect.centerx-=self.dx
>         elif pressed[K_RIGHT]:
>             self.rect.centerx+=self.dx
>
>
>         if self.rect.bottom>=screen.get_height():
>             self.rect.bottom=screen.get_height()
>         elif self.rect.top<=0:
>             self.rect.top=0
>
>         if self.rect.centerx>=screen.get_width()-self.distancefromcenter:
>             self.rect.centerx=screen.get_width()-self.distancefromcenter
>         elif self.rect.centerx<=self.distancefromcenter:
>             self.rect.centerx=self.distancefromcenter
>
>         if pressed[K_SPACE]:
>             self.bullet.x=self.rect.centerx
>             self.bullet.y=self.rect.centery
>
>
>
> class Bullet(pygame.sprite.Sprite):
>     def __init__(self):
>         pygame.sprite.Sprite.__init__(self)
>         self.image=pygame.image.load('geometrybullet.png').convert_alpha()
>         self.rect=self.image.get_rect()
>         self.rect.center=(-100,-100)
>         self.x=-100
>          self.y=-100
>         self.dy=5
>
>
>     def update(self):
>         self.y-=self.dy
>         self.rect.center=(self.x,self.y)
>         if self.rect.top<0:
>             self.x=-100
>             self.y=-100
>
>
> def main():
>     background=pygame.Surface(screen.get_size())
>     background=background.convert()
>     screen.blit(background,(0,0))
>     bullet=Bullet()
>     plane=Plane(bullet)
>     allSprites=pygame.sprite.Group(plane,bullet)
>
>     while 1:
>         for i in pygame.event.get():
>             quitPressed=pygame.key.get_pressed()
>             if i.type==QUIT or quitPressed[K_q]:
>                 exit()
>
>         allSprites.clear(screen,background)
>          allSprites.update()
>         allSprites.draw(screen)
>         pygame.display.flip()
>
>
> if __name__=='__main__':
>     main()
>
> Thanks in advance :)
>
> Regards
> Ankur Aggarwal
>
>
>
>

Reply via email to