Reputation: 141
how can I restart the game with user input? i searched all over the place and i wasnt able to discover how i can restart my game, i just want to press ESC and my game restart, after knowing how to do that i will implement a button, but how can I restart my game? this is my main loop:
while True:
pygame.time.Clock().tick(fps)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
#input
elif event.type == pygame.KEYDOWN:
#snake
if event.key == ord('w'):
change_to = 'UP'
if event.key == ord('s'):
change_to = 'DOWN'
if event.key == ord('a'):
change_to = 'LEFT'
if event.key == ord('d'):
change_to = 'RIGHT'
#snake2
if event.key == pygame.K_UP:
change2_to = 'UP'
if event.key == pygame.K_DOWN:
change2_to = 'DOWN'
if event.key == pygame.K_LEFT:
change2_to = 'LEFT'
if event.key == pygame.K_RIGHT:
change2_to = 'RIGHT'
#quit game
if event.key == pygame.K_ESCAPE:
#here is where was supposed to restart the game
i cut the major part to not be so long but i dont know how to restart my game
Upvotes: 1
Views: 243
Reputation: 76
I think it should be something like this
# imports
restart = False
while running:
if restart:
# create instance of all objects used
# score = 0
# Default values should be initialised
# Keyboard events go here
# code for restarting
if event.key == pygame.K_ESCAPE:
restart = True
Upvotes: 4