0

I'm trying to create a genetic algorithm that learns how to play Tetris.

But is there a way to creare multiple istance of the game? So that i can create like 200 tetris, wait until all die and do operations instead of creating one instance, wait for the AI to lose, close the game, create a new one and so on?

I have tried with :

import logging
import threading
import time

from game import Game


def thread_function():
    Game()

if __name__ == "__main__":
    x = threading.Thread(target=thread_function, args=())
    x.start()
    y = threading.Thread(target=thread_function, args=())
    y.start()
    z = threading.Thread(target=thread_function, args=())
    z.start()

But the result is not 3 istances of the game, but only 1 with weirds thing going on. If i use just one thread everything works fine.

3
  • No, all the variables that i'm using are attributes of the class Commented Mar 3, 2021 at 16:33
  • Instance attributes Commented Mar 3, 2021 at 16:35
  • See Pygame with Multiple Windows. Commented Mar 3, 2021 at 16:37

2 Answers 2

0

I have actually fixed the problem.

Instead of creating a single process and multithread, you have to create simple multi processes.

I have changed the code like this to get the points for each process created when terminated.

import multiprocessing
from multiprocessing import Process
from game import Game


def thread_function(procnum, return_dict):
    return_dict[procnum] = Game().end_game()

processes = []

if __name__ == "__main__":
    manager = multiprocessing.Manager()
    return_dict = manager.dict()

    for m in range(1, 3):
        p = Process(target=thread_function, args=(m, return_dict,))
        p.start()
        processes.append(p)

    for p in processes:
        p.join()

    print(return_dict.values())
Sign up to request clarification or add additional context in comments.

Comments

0

But is there a way to create multiple instance of the game?

No, there is no way. See pygame.display module:

This module offers control over the pygame display. Pygame has a single display Surface that is either contained in a window or runs full screen. Once you create the display you treat it as a regular Surface. [...]

However, you don't need multiple windows at all. You can run the game logic multiple times concurrently. But display only one of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.