1

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!

4
  • Soundle like an XY problem. What are you trying to achieve? Commented Dec 28, 2019 at 14:54
  • well I am basically trying to restart the whole game from the beginning (a.py is a copy of main) if the player wants to. Since I have set a timer to calculate every second played, if I dont restart the whole program correcting the timer will be more trouble than it's worth. I know im not making sense, but I just can't really explain what I am trying to achieve. I am new to programming (started 1 month ago), so it is still a relatively new concept for me Commented Dec 28, 2019 at 15:35
  • 2
    Resetting a game by restarting the entire application is very, very, very bad programming. You should either destroy and reinit a (game) class or call some init routine instead. Commented Dec 28, 2019 at 15:37
  • @agtoever is there a way to restart time without restarting the program? Commented Dec 28, 2019 at 15:40

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

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.