So my problem is really weird. I first initilaize random neural networks (models) and for every one in loop I start the game run_game
and then keyboard_input
and neural network presses some buttons in loop on keybaord and it "plays" the game . The problem is one once 100 hundred times when I start os.system('./game.pyw ')
program doesn't go further but it freezes on line pygame.init()
in the game.pyw
file and doesn't go further (propably inifnite loop inside) since there is no way to stop one thread from another thread in python I don't know what to do with this
for model in models:
gc.collect()
p1 = threading.Thread(target=run_game,args=())
p2 = threading.Thread(target=keybord_input,args=(model.get_weights(),))
p1.start()
p2.start()
p1.join()
p2.join()
def run_game():
global is_game
is_game = 1
try:
os.system('./game.pyw ')
except:
traceback.print_exc()
is_game = 0
def keybord_input():
global is_game
while is_game == 1:
//neural network give responses and corresponding buttons are pressed
import pygame, os
from pygame.locals import *
import menu, data
def main():
try:
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.mixer.pre_init(44100, -16, 2, 4096)
pygame.init() # problem
pygame.mouse.set_visible(0)
pygame.display.set_icon(pygame.image.load(data.filepath("bowser1.gif")))
pygame.display.set_caption("Super Mario Python")
os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (0,0)
os.environ['SDL_VIDEO_CENTERED'] = '0'
screen = pygame.display.set_mode((640, 480))
menu.Menu(screen)
except:
return 0
okay so I found the solution and it works for 3500 iterations so i guess it's good
if not pygame.get_init():
pygame.init()