5

I'm new to python and therefore decided to try to make a simple game in pygame. I want to add a timer/clock that shows how long "you have played/survived", so creating a clock basically.

However, I've searched around and gotten the time.sleep(1) function and it does indeed work as a clock, but it slows down everything else of the game to the point it barely moves.

Is there a simple way to add a clock to the gamescreen?

0

1 Answer 1

5

The number of milliseconds since pygame.init() can be retrieved by pygame.time.get_ticks(). See pygame.time module.


Furthermore in pygame exists a timer event. Use pygame.time.set_timer() to repeatedly create an USEREVENT. e.g.:

time_delay = 500 # 0.5 seconds timer_event = pygame.USEREVENT + 1 pygame.time.set_timer(timer_event , time_delay ) 

Note, in pygame customer events can be defined. Each event needs a unique id. The ids for the user events have to start at pygame.USEREVENT. In this case pygame.USEREVENT+1 is the event id for the timer event.

Receive the event in the event loop:

running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == timer_event: # [...] 

The timer event can be stopped by passing 0 to the time parameter.


See the example:

import pygame pygame.init() window = pygame.display.set_mode((200, 200)) clock = pygame.time.Clock() font = pygame.font.SysFont(None, 100) counter = 0 text = font.render(str(counter), True, (0, 128, 0)) time_delay = 1000 timer_event = pygame.USEREVENT+1 pygame.time.set_timer(timer_event, time_delay) # main application loop run = True while run: clock.tick(60) # event loop for event in pygame.event.get(): if event.type == pygame.QUIT: run = False elif event.type == timer_event: # recreate text counter += 1 text = font.render(str(counter), True, (0, 128, 0)) # clear the display window.fill((255, 255, 255)) # draw the scene text_rect = text.get_rect(center = window.get_rect().center) window.blit(text, text_rect) # update the display pygame.display.flip() 
Sign up to request clarification or add additional context in comments.

4 Comments

@Mokoisa Yes, exactly.
Thank you so much! I didn’t quite understand this. Above the while loop I have: time_delay = 1000 # 1 second timer_event = pygame.USEREVENT + 1 time = pygame.time.set_timer(timer_event, time_delay) def show_time(x, y): time_s = font.render("Time: " + str(time), True, (0, 0, 255)) screen.blit(time_s, (x, y)) and tried to use “show_time” in the while loop as shown on your picture, but I get “None” instead of a timer when running the game. Am I missing a “pygame.time.get_ticks()” ? Do you know where I've messed? Sorry, but I found this difficult. Thanks again! :)
Im also new to these kind of websites, so sorry if my posts are messy af.
@Mokoisa See the example, which I've add to the answer, right now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.