Reputation: 21
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
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