0

I am making a bullet dodging game in pygame and want to have my enemies shoot bullets every 5 seconds. I want to find an efficient answer for this because I came up with a solution that slows my game down. Basically I just made a variable that adds constantly, and when it equals a large number the function occurs. I tried finding answers online sing the threading library but it still blocks my game.

    def shoot(self):
        for i in range(self.bullets):
            self.angle += 360/self.bullets
            self.bullet_group.add(Bullet((self.rect.x, self.rect.y), self.angle))

    def shoot_cycle(self, interval):
        self.sec += 1
        if self.sec == interval * 50:
            self.shoot()
            self.sec = 0

    def update(self):
        self.shoot_cycle(3)
        self.animate()
        self.bullet_group.update()
        self.bullet_group.draw(self.display_surface)

I tried using the threading library from a tutorial online. I might be using this wrong however. When I ran my code, my enemies were shooting constantly with no delay. If someone could answer this that would be great!

import threading
    def shoot(self):
        for i in range(self.bullets):
            self.angle += 360/self.bullets
            self.bullet_group.add(Bullet((self.rect.x, self.rect.y), self.angle))

    def shoot_cycle(self, interval):
        timer = threading.Timer(interval, self.shoot_cycle)
        timer.start()

        self.shoot()

    def update(self):
        self.shoot_cycle(3)
        self.animate()
        self.bullet_group.update()
        self.bullet_group.draw(self.display_surface)

0 Answers0