1

I am creating a Snake game for a school project. Something I am trying to implement is a Game Over screen when the player fails. I have the screen created and a button which can be clicked on the screen, but the button doesn't show up and it doesn't restart the game loop. Here is some of my code so far:

def game_loop(self):
        pygame.display.set_caption("Worm on a String")

        pygame.time.set_timer(WIN_UPDATE, 150)

        game_logic.background_music()

        # draw elements
        while game_running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == WIN_UPDATE:
                    game_logic.update()

                    userInput = pygame.key.get_pressed()

                    if userInput[pygame.K_LEFT]:
                        if game_logic.worm.direction.x != 1:
                            game_logic.worm.direction = pygame.math.Vector2(-1, 0)
                    if userInput[pygame.K_RIGHT]:
                        if game_logic.worm.direction.x != -1:
                            game_logic.worm.direction = pygame.math.Vector2(1, 0)
                    if userInput[pygame.K_UP]:
                        if game_logic.worm.direction.y != 1:
                            game_logic.worm.direction = pygame.math.Vector2(0, -1)
                    if userInput[pygame.K_DOWN]:
                        if game_logic.worm.direction.y != -1:
                            game_logic.worm.direction = pygame.math.Vector2(0, 1)
                
            win.fill(pygame.Color('green'))
            game_logic.draw_elements()

            pygame.display.update()
            fps.tick(60)
def failure(self):
        if not 0 <= self.worm.body[0].x <= cell_number - 1:
            self.game_over()
        if not 0 <= self.worm.body[0].y <= cell_number - 1:
            self.game_over()
        for block in self.worm.body[1:]:
            if block == self.worm.body[0]:
                self.game_over()

    def game_over(self):
        global game_running
        while game_running:
            game_running = False
            over_screen = True
            while over_screen:
                if game_running == False:
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            pygame.quit()
                            quit()
                    win.fill('black')
                    title_text = "GAME OVER"
                    title_surface = title_font.render(title_text, True, 'red')  
                    title_rect = title_surface.get_rect(center = ((winX / 2), (winY - 400)))  
                    win.blit(title_surface, title_rect)      
                    
                    mouse = pygame.mouse.get_pos()
                    click = pygame.mouse.get_pressed()

                    if self.button.collidepoint(mouse):
                        if click[0] == 1:
                            print("click")
                            game_logic.game_loop() 

                    pygame.display.update()
                    fps.tick(15)

Any help would be appreciated!

  • *"What am I doing wrong ... ?"* - You call `game_loop` recursively. – Rabbid76 Dec 03 '21 at 21:08
  • @Rabbid76 How would I go about fixing this in this regard? Not super well versed – kristenmarcinek Dec 04 '21 at 14:36
  • Implement only one application loop. Use a game state variable that indicates the state of the game. e.g.: [`Reset and restart pygame program doesn't work`](https://stackoverflow.com/questions/64715107/reset-and-restart-pygame-program-doesnt-work/64715310#64715310) or [Pygame level/menu states](https://stackoverflow.com/questions/14700889/pygame-level-menu-states/14727074#14727074). – Rabbid76 Dec 04 '21 at 14:39

0 Answers0