0
\$\begingroup\$

i have made this bullet class but i have a problem with the firerate. Sometimes i'm not able to shoot even when the little delay (700ms) between the two shots has ended. I guess it's due to the pygame.time.set_timer(). Anyways i'm looking for a quick solution because i wanna move on to work on another part of this game.

...

class Singlebullet(object):

    def __init__(self):
        self.img = pygame.image.load(r'C:\Users\Salem\Documents\MyGame1\bullet.png')
        self.state = 'ready'
        self.speed = 0
        self.firerate = 700
    
    def shoot(self):
        if current_bullet_type == 'single':
            if self.state == 'ready':
                self.x = myplane.x + 71 
                self.y = myplane.y 
            if self.state == 'fire':
                self.speed = -13
            for event in events:
                if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
                    if event.key == pygame.K_SPACE:
                        self.state = 'fire'
                        pygame.time.set_timer(pygame.USEREVENT+1, self.firerate)
                if event.type == pygame.USEREVENT+1:
                    self.state = 'ready'
            MyScrollingScreen.blit(self.img, (self.x, self.y))
            self.y += self.speed
s1 = Singlebullet()
...
while running:
    ...
    s1.shoot()
    ...
\$\endgroup\$
6
  • \$\begingroup\$ Why do you shoot on space release too? Might want to make sure you're in "ready" state before shooting (starting the timer). \$\endgroup\$ – Vaillancourt May 2 at 0:02
  • \$\begingroup\$ You seem to be misusing the set_timer function. Check the documentation to see how it should be used. \$\endgroup\$ – Vaillancourt May 2 at 0:05
  • \$\begingroup\$ @Vaillancourt Thank you! I removed the KEYUP event and added the condition that the bullet should be in ready state before starting the timer and everything worked ! btw , i see that the documentation doesn't provide enough information about everything and that's why i missuse some functions. \$\endgroup\$ – Salem May 2 at 12:45
  • \$\begingroup\$ Glad your issue is fixed! You can write (and accept) an answer explaining how you fixed it! \$\endgroup\$ – Vaillancourt May 2 at 13:04
  • \$\begingroup\$ Done, but i can't accept it until tomorrow \$\endgroup\$ – Salem May 2 at 13:52
1
\$\begingroup\$

So, i realized that i was missusing the pygame.time.set_timer() function. In fact, this resulted in making me able to shoot the bullet only when the timer resets itself, but i wasn't able to shoot when the countdown timer starts again. Then i added the condition that the bullet should be in ready state before starting/restarting the timer (now, the timer won't be restarted unless the bullet's state is set to 'ready' and i click the space button):

for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and self.state == 'ready':
                    self.state = 'fire'
                    pygame.time.set_timer(pygame.USEREVENT+1, self.firerate)
            if event.type == pygame.USEREVENT+1:
                self.state = 'ready'

And i have also removed the if event.type == pygame.KEYUP as it was absolutely useless.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.