Start again button"
I want to implement a "play again" button in a game (made with pygame). To do this, I decided that I could use a "loop within a loop". The first cycle is used to make the start button of the game, the second - the main cycle, and starts at the end of the second, i.e. when the player loses. After the start of the 3rd cycle, the "play again" button should appear, after clicking on which, according to my idea, the 2nd cycle should start again, but this does not happen.
run = False
start = True
while start == True:
window.fill((0, 0, 0))
# draw btn
window.blit(start_btn, (0, 0))
# draw borders
pygame.draw.rect(window, (255, 248, 220), [245, 35, 5, 810])
pygame.draw.rect(window, (255, 248, 220), [245, 35, 800, 5])
pygame.draw.rect(window, (255, 248, 220), [245, 840, 800, 5])
pygame.draw.rect(window, (255, 248, 220), [1045, 35, 5, 810])
# get cursor position for click
pos = pygame.mouse.get_pos()
x = pos[0]
y = pos[1]
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
if x > 500 and x < 800 and y > 295 and y < 580:
run = True
pygame.display.flip()
# main cycle
while run:
# fill screen with black color
window.fill((0,0,0))
# draw a part of snake
for x, y in snake:
pygame.draw.rect(window, (124, 252, 0), (x, y, snake_size_x, snake_size_y))
# draw food img
window.blit(egg_img, (egg_spawn_x, egg_spawn_y))
# draw borders
pygame.draw.rect(window, (255, 248, 220), [245, 35, 5, 810])
pygame.draw.rect(window, (255, 248, 220), [245, 35, 800, 5])
pygame.draw.rect(window, (255, 248, 220), [245, 840, 800, 5])
pygame.draw.rect(window, (255, 248, 220), [1045, 35, 5, 810])
# EXIT from game
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
start = False
# fps
fps.tick(10)
# control movement
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
dir_x, dir_y = 0, -1
if keys[pygame.K_a]:
dir_x, dir_y = -1, 0
if keys[pygame.K_s]:
dir_x, dir_y = 0, 1
if keys[pygame.K_d]:
dir_x, dir_y = 1, 0
snake_x += snake_speed * dir_x
snake_y += snake_speed * dir_y
# we append every move to snake-list
snake.append((snake_x, snake_y))
snake = snake[-lenth_snake:]
# What we are doing if food is eaten
if snake_x == egg_spawn_x and snake_y == egg_spawn_y:
# spawn new egg
egg_spawn_x = random.randrange(370, 851, 40)
egg_spawn_y = random.randrange(160, 641, 40)
window.blit(egg_img, (egg_spawn_x,egg_spawn_y))
# update snake-lenth
lenth_snake += 1
# What we are doing if we have collision
if snake_x > 1050 or snake_x < 210 or snake_y > 840 or snake_y < 0:
# run = False
# start = False
finish = True
while finish:
window.fill((0, 0, 0))
# draw btn
window.blit(gameOver_btn, (0, 0))
# draw borders
pygame.draw.rect(window, (255, 248, 220), [245, 35, 5, 810])
pygame.draw.rect(window, (255, 248, 220), [245, 35, 800, 5])
pygame.draw.rect(window, (255, 248, 220), [245, 840, 800, 5])
pygame.draw.rect(window, (255, 248, 220), [1045, 35, 5, 810])
# get cursor position for click
pos = pygame.mouse.get_pos()
x = pos[0]
y = pos[1]
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
start = False
finish = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if x > 565 and x < 740 and y > 420 and y < 595:
points = 0
finish = False
# save all
pygame.display.flip()
# save all
pygame.display.flip()
pygame.quit()
0