GamingWithWolfy 1223
GamingWithWolfy 1223

Reputation: 21

How to run a python file through another python file and then close the first one

I know this sounds confusing, but basically I want to run another python file (a.py) through my main.py file when the user presses the key "p" and then close main.py while a.py is still running. I have tried multiple different ways to force main.py to stop running, but it always closes only after a.py exits.

Here's my code for this part (I'm using pygame):

  import pygame
  import os

  gameOver = False

  while not gameOver:

       for event in pygame.event.get(): 

            if event.type == pygame.KEYDOWN:

                  if event.key == pygame.K_p:
                       os.system("a.py")
                       gameOver = True
   #the rest of the game

Thanks for your help!

Upvotes: 1

Views: 87

Answers (1)

Yacine Mahdid
Yacine Mahdid

Reputation: 731

Usually a program lives and die with the main. Instead of trying to kill the main use the main to manage your game. Have the main.py initialize the state of your game and then have it cal other functions depending on the input. This is more sane and will make sense when you try it out. Try to follow this tutorial, it will teach you best practices: PyGame tutorial

Upvotes: 1

Related Questions