0

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()
  • can you please show full code? –  Nov 29 '19 at 14:11
  • which parts are important? It has like 900 lines and it all works fine 99% of the time but it freezes in the game code (game code is not mine) – saint_burrito Nov 29 '19 at 14:18
  • you need to initialize pygame.init first, then pygame.mixer –  Nov 29 '19 at 14:27
  • I tried it with init() before mixer, it doesn't help – saint_burrito Nov 29 '19 at 14:28
  • did you try to remove the init? –  Nov 29 '19 at 14:29
  • no no no. did removing the `pygame.init()` line work or not? –  Nov 29 '19 at 14:32
  • without init() it doesn't work – saint_burrito Nov 29 '19 at 14:32
  • can you tell why it's doesn't work? –  Nov 29 '19 at 14:34
  • please update the question with the screenshot, maybe I can help. I only have half hour to help you –  Nov 29 '19 at 14:34
  • Pygame isn't made for multiple windows in the same process. Look [here](https://stackoverflow.com/questions/29811814/pygame-with-multiple-windows). You should spawn new processes instead of new threads. This will make keyboard input etc. more difficult, but I see it as the only option... maybe you should consider not using pygame. – Nearoo Nov 29 '19 at 14:35
  • I was thinking about processes but after run_game this game completely terminates, so it shouldn't be stored in any means in memory. That's why in some lucky cases this loop can run even 700+ times without any problem. – saint_burrito Nov 29 '19 at 14:40
  • @Faran2007 I can make a screenshot but it basically looks like the window with game doesn't show up and second thread terminates normally – saint_burrito Nov 29 '19 at 14:43
  • @saint_burrito I don't understand your objection, stored in memory? You can discard a process completely after it ends. – Nearoo Nov 29 '19 at 14:48
  • Also your threading is completely unsafe, jfyi. Your game could terminate between if(is_game==1) and what comes after. Your whole setup is a bit... spadhetti. Parallel programming is non-trivial. I recommend you try to insulate the test runs from the program that launches the test runs, and let the launcher fork new processes for each launch. And keep the amount of concurrent test runs low, if you have more threads/processes than cpu cores, you're introducing overhead that is potentially worse than running the whole thing in sequence. – Nearoo Nov 29 '19 at 14:53
  • @Nearoo I used a wrong term sorry what I mean is, I run every instance of game as separate program in different time, so in the given time there is only one window and one instance of the game launched – saint_burrito Nov 29 '19 at 14:53
  • Another option: you could initialize only one pygame and one window, and let the programs run invisible "screens" that are just `pygame.Surfaces` unrelated to each other... – Nearoo Nov 29 '19 at 14:55
  • @Nearoo I'm aware of it but every time second thread terminates well – saint_burrito Nov 29 '19 at 14:58
  • @Nearoo but what I need to achieve is complete separation of the game part and the keras part, that's why I don't want to do any coding related to how the game is display in this code, just run and let it terminates – saint_burrito Nov 29 '19 at 15:01

0 Answers0