Vildan
Vildan

Reputation: 131

How to create a score counter where scores add for every second?

I'd like to add special score counter to my game can finish when Player get, for example 1000 scores, and Player will win, however, I absolutely don't know how to realize it. What do I have to do? Probably, I have to use pygame.USEREVENT + 1;

import time
total_score = 1000
scores = 0
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        pygame.sprite.Sprite.__init__(self)
        #[...]
        self.score = scores
        self.plus = 1

    def update(self):
        #[...]
        self.score += self.plus
def game():
    global health 
    Game = True
    while Game:
        #[...]
        print_text('Total score  ' + str(scores), 725, 25)

Upvotes: 1

Views: 877

Answers (1)

Rabbid76
Rabbid76

Reputation: 211219

Use a timer event. In pygame exists a timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. The time has to be set in milliseconds. e.g.:

timer_interval = 1000 # 1 seconds
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, timer_interval)

Each timer event needs a unique id. The ids for the user events have to be between pygame.USEREVENT (24) and pygame.NUMEVENTS (32). In this case pygame.USEREVENT+1 is the event id for the timer event.

Increment the score when the timer event occurs:

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

         elif event.type == timer_event:
             player.score += player.plus

Upvotes: 3

Related Questions