1
\$\begingroup\$

in pygame, we use pygame.time.set_timer() to repeatedly create an event. This function takes 2 arguments : the event type and the delay time after which the event occurs in milliseconds. For my case, that delay time is the reload time. So, i'm looking for a way to display that reload time on the screen. This is what i tried :

...
pygame.time.set_timer(pygame.USEREVENT+1, time)
...
reload_time = myfont.render(time, True, 'black')
myscreen.blit(reload_time, (670, 650))
...
pygame.display.flip()

This resulted in displaying the time delay value forever (it's not decreasing or resetting) So what should i do?

\$\endgroup\$
1
\$\begingroup\$

This is expected as time is passed "by value" (you copy the content of the variable "into the function").

Perhaps you need to change your approach to "timer" as there appears to be no equivalent to "get_timer" to get the time left before the event is fired.

I'm not exactly sure how your whole game is developed, but you probably need to track the time yourself so that you can check the time left.

I'm not fluent in pygame, but, based on your previous question and pygame's documentation, perhaps something like this would work:

...

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 # assuming ms
        # this is new:
        self.bullet_time_left = self.firerate
        self.clock = pygame.time.Clock()
    
    def shoot(self):
        if current_bullet_type == 'single':
            if self.state == 'ready':
                self.x = myplane.x + 71 
                self.y = myplane.y 
            elif self.state == 'fire': # use elif instead of if
                self.speed = -13
                
                
            for event in events:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE and self.state == 'ready':
                        self.clock = pygame.time.Clock()
                        self.bullet_time_left = self.firerate
                        self.state = 'fire'
                        
            delay = self.clock.tick()
            if self.state == 'fire':
                self.bullet_time_left -= delay
                if self.bullet_time_left <= 0:
                    self.bullet_time_left = self.firerate
                    self.state = 'ready'
                else:
                    reload_time = myfont.render(self.bullet_time_left, True, 'black')
                    MyScrollingScreen.blit(reload_time, (670, 650))
                    

            MyScrollingScreen.blit(self.img, (self.x, self.y))
            
            self.y += self.speed
s1 = Singlebullet()
...
while running:
    ...
    s1.shoot()
    ...

That's the gist of it. You start a "Clock" when you fire the bullet; each update, you "update" it via it's tick() method to check how much time has passed since the last "tick()". You remove that time from the "time left"; once that counter gets below 0, you know that you're done and should clean it up. You have access to the time left so you can use it to update your label.

You'll probably want to rearrange some of what I wrote to suit your needs.


As I mentioned, I'm not fluent with pygame, and so maybe someone else has a better approach to propose.

\$\endgroup\$
0

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.