0

I have a game where you play against an AI. The main pygame loop looks something like this:

def main():
    running = True
    while running:

        # Some code here.....

        if move_made == True:
            move = ai_make_move(gamestate)

The problem here is that ai_make_move() takes around 10-15 seconds to calculate. During this time the pygame window freezes since the main loop is not updating until ai_make_move() is ready. Is there a way to make calculations in the background and keep the main pygame loop running somehow? So that I can e.g. move the window around during calculation time.

Eli
  • 156
  • 8
  • 1
    Can `ai_make_move()` be executed in a separate thread? – Rabbid76 Nov 22 '20 at 10:24
  • @Rabbid76, Sure, if I know how to do that :) – Eli Nov 22 '20 at 10:26
  • @Eli go find out :) https://stackoverflow.com/questions/2882308/spawning-a-thread-in-python – JeffUK Nov 22 '20 at 10:30
  • Does this answer your question? [Spawning a thread in python](https://stackoverflow.com/questions/2882308/spawning-a-thread-in-python) – JeffUK Nov 22 '20 at 10:30
  • The other option is to optimise your ai_make_move() function so that it doesn't take 10-15 seconds. Maybe you could work out your move progressively, a little bit per loop, rather than doing it all in one go. – JeffUK Nov 22 '20 at 10:31
  • @JeffUK, I don't understand how to do that in my case. The answer provided there don't seem to work, my window is still freezing. Could you be ever so kind and provide an example in an snwer to this question and I will accept it as an answer :) – Eli Nov 22 '20 at 11:00
  • @eli, suggest you post the code that is still locking, after you've tried to debug it yourself. there are a few resources that come up on google for 'pygame multithreading' – JeffUK Nov 22 '20 at 11:15

1 Answers1

0
import pygame
from pygame.locals import *
import threading
#your constants and more there
finished=True
def ai_make_move(args):
    global finished
    # do some stuff there
    finished=True
def main():
    global finished
    running=True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running=False
        #do some stuff here
        if move_made and finished:
            thread=threading.Thread(target=lambda: ai_make_move(gamestate))
            thread.start()
            finished=False
        #also there
        pygame.display.update()

if __name__ == '__main__':
    main()

it allows you to do pygame code in paralel as the ai_make_move function works

I can't promise it works but only that you should change somehow your code( if it does not work ) . I mean the base is here but maybe you should change something in your code to make what you want

hope i helped you!

  • Thank you. How do I obtain the variables that ai_make_move returns? – Eli Nov 24 '20 at 05:08
  • simply write `var_name=ai_make_move()` , but if your function returns more variables, you can unpack them so :: `var1, var,2,var3 (...) = ai_make_move() ` – gfsdfgsfgd Nov 29 '20 at 11:57