So I am currently mid way through coding snake, in an attempt at learning pygame. I am quite new to coding so apologies if I have explained this badly. I have a game loop which is active when running = True, when the snake hits a boundary, running becomes False, ending the game loop and switches to a different while loop that says game over. I will of course add more to this screen later down the line. I am just currently trying to figure out how to make it when I click r, within this second loop, running becomes true again, triggering the game loop once again to effectively restart this game. I would really appreciate any suggestions, thank you!
import pygame
pygame.init()
display_x = 400
display_y = 300
display = pygame.display.set_mode((display_x, display_y))
pygame.display.set_caption("Ned's snake game")
x1 = display_x / 2
y1 = display_y / 2
x1_change = 0
y1_change = 0
clock = pygame.time.Clock()
fps = 45
game_over_font = pygame.font.Font("freesansbold.ttf", 16)
def game_over():
over_text = game_over_font.render("GAME OVER", True, (200, 0, 0))
display.blit(over_text, ((display_x / 2) - 54, (display_y / 2) - 16))
running = True
while running is True:
print("thing")
display.fill((50, 200, 20))
pygame.draw.rect(display, (0, 0, 255), [x1, y1, 10, 10])
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -5
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 5
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -5
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 5
x1_change = 0
elif event.key == pygame.K_ESCAPE:
pygame.quit()
x1 += x1_change
y1 += y1_change
if x1 <= 0 or x1 >= display_x or y1 <= 0 or y1 >= display_y:
running = False
end_screen = True
clock.tick(fps)
pygame.display.update()
while end_screen is True:
display.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
elif event.key == pygame.K_r:
running = True
end_screen = False
game_over()
pygame.display.update() ```