Following is the code of my simple game. When the game is over with the defined conditions if true, I want to break the while loop but NOT terminate (exit) the game.
import cv2
import datetime
#importing other files
#defitinions
# functions
def myfunction1():
try:
...
except:
...
while True: #I also put a gameflag to check ( while gameflag: ...)
#game codes here
if (conditions) :
gameOver = True
if gameOver:
#game is over codes here
#I put a gameflag here to set False to be checked in while loop above
else:
...
key = cv2.waitKey(1) #I put waiting key line both inside and outside of the while loop. But app skips this line and terminates.
if key == ord('r'): #I moved this Line out of the While loop but it doesn't
imgGameOver = cv2.imread("images/gameover.png")
gameflag = True
I calculate the position of the ball and checking the conditions during the while loop. As you can see in the code, if the game is over, I want to keep the window displayed and wait for the key if the player wants to restart the game. But when I put "break;", the app terminates and exits. When I put a bool flag, it doesn't check after breaking the While Loop.
I want to stop While Loop until the player enters a key to restart the game (back into the While loop again, where my game codes are) I want this because the some services keep working within the while loop such as file append text, calling web services etc.
Thank you
You can try adding a nested while loop. The outside while loop will represent the program running and the inside one can represent the game is running. If the game is done then the inner while loop will break and it will loop again if it is restarted.