0

I know the answer is no, but I'm just making sure. Baisically, I've made my first pygame project. I'm pretty proud of it, to be honest. The program starts off by defining a bunch of classes for different sprites. Then it defines three functions. One for the options screen, one for the game screen, and one for the timer. What i want is to be able to run the timer() function and the game() function at the same time. I have tried threads:

t1 = Thread(target=game)
t2 = Thread(target-timer)
while True:
start_screen.draw(window)
clock.tick(30)
events = pygame.event.get()
for event in events:
    if event.type == QUIT:
        exit()
    elif event.type == MOUSEBUTTONDOWN:
        mousepos = pygame.mouse.get_pos()
        if button.rect.collidepoint(mousepos):
            start_screen.remove(button)
            start_screen.remove(button1)
            window.fill(white)
            t1.start()
            t2.start()
            t1.join()
            window.fill(white)
            start_screen.add(button)
            start_screen.add(button1)
        if button1.rect.collidepoint(mousepos):
            options()
            time.sleep(3)
            window.fill(white)
pygame.display.update()

as you can see, the if statement checks whether the game button is pressed, then it runs the timer and the game at the same time. This works, but also causes many glitches and stops the loop so any event handling in game() doesnt work. It also makes the Pygame window lag alot. When i ran the same code with only game() it worked pretty awesomley, with back button, start button, and options button all working, but the timer stayed at 0.

So after the long, boring explanation, my question is: is there a way to run 2 functions at the same time without threads. Or how can i make a timer in the same loop as the game() loop without slowing it down( technichally that's possible if it counted in frames but i would prefer seconds please). Thanks all :)

  • Does **Threading.Timer** help at all? – Prune Apr 06 '17 at 19:08
  • Check out Python's async/await. Here is a decent tutorial: http://stackabuse.com/python-async-await-tutorial/ You can make some of your blocking functions async, await them, and then synchronize everything before moving to the next frame. – Remolten Apr 07 '17 at 01:59
  • Thank you very much. It worked!! –  Apr 07 '17 at 07:26

0 Answers0