1

I was working on the basic game "Snake" in Python using "pygame". When I move around at the start and I am alive, my arrow keys are working fine. When I die, then I can't use any key and I can't close the window by pressing the X button on the top right of the window. The only way to terminate it is by pressing Ctrl-x in the console, and that way it doesn't close.

When I debugged it, my console says that my q and c values are 59, 248 accordingly but the pygame. K_q and pygame.K_c have the values 113, 99. Does anybody knows the reason? My code when I die is the following:

            while game_close == True:
            self.dis.fill(colors("blue"))
            self.message("You Lost! Press C-Play Again or Q-Quit", colors("red"))
            self.Your_score(Length_of_snake - 1)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    elif event.key == pygame.K_c:
                        game_close = False
                        self.gameLoop()
           

If anybody has any clue why this happened it will be useful. It is my first time writing any quest so sorry for not having the best description or there is any duplicate( I have searched and found nothing that works for me)

edit: This is the whole script:

    game_over = False
    game_close = False
    x1 = self.dis_width / 2
    y1 = self.dis_height / 2

    x1_change = 0
    y1_change = 0

    snake_List = []
    Length_of_snake = 1

    foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
    foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
    
    flagex=True
    flagey=True

    while not game_over:

        while game_close :
            self.dis.fill(colors.blue())
            self.message("You Lost! Press C-Play Again or Q-Quit", colors.red())
            self.Your_score(Length_of_snake - 1)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    game_close = True
                    game_over = True
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    elif event.key == pygame.K_c:
                         game_close = False
                         self.gameLoop()
                    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and flagey==True:
                    flagey = False
                    flagex = True
                    x1_change = -self.snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT and flagey==True: 
                    flagey = False
                    flagex = True
                    x1_change = self.snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP and flagex==True:
                    flagey = True
                    flagex = False
                    y1_change = -self.snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN and flagex==True:
                    flagey = True
                    flagex = False
                    y1_change = self.snake_block
                    x1_change = 0

        if x1 >= self.dis_width or x1 < 0 or y1 >= self.dis_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        self.dis.fill(colors.blue())
        pygame.draw.rect(self.dis, colors.green(), [foodx, foody, self.snake_block, self.snake_block])
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
        if len(snake_List) > Length_of_snake:
            del snake_List[0]

        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True

        self.our_snake(self.snake_block, snake_List)
        self.Your_score(Length_of_snake - 1,)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
            foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
            Length_of_snake += 1

        self.clock.tick(self.snake_speed)

    pygame.quit()
    quit()  
Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105
  • 1
    How many event loops do you have? See [Faster version of pygame.event.get()](https://stackoverflow.com/questions/58086113/faster-version-of-pygame-event-get/58087070#58087070). – Rabbid76 Oct 27 '20 at 19:03
  • 1
    The implementation seems to be correct. You have to show more code. I'm very sure that you've implemented more than 1 event loop. [`pygame.event.get()`](https://www.pygame.org/docs/ref/event.html#pygame.event.get) get all the messages and remove them from the queue. If `pygame.event.get ()` is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed. – Rabbid76 Oct 27 '20 at 19:23

3 Answers3

1

I won't be able to answer your question completely because it would be nice to see the whole code but I would definietly change it to this:

        while game_close:
            self.dis.fill(colors("blue"))
            self.message("You Lost! Press C-Play Again or Q-Quit", colors("red"))
            self.Your_score(Length_of_snake - 1)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    game_close = True
                    game_over = True
                    # exit()
            keys = pygame.key.get_pressed()
            if keys[pygame.K_q]:
                game_over = True
                game_close = False
            if keys[pygame.K_c]:
                game_close = False
                self.gameLoop()
Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • 1
    Thank you very much. This was the solution but with the change that for some reason keys[pygam.K_q] did not work but it works with keys[pygame.K_q]==False and the same goes for keys[pygame.K_c] – Akis Lionis Oct 27 '20 at 19:17
  • 1
    @AkisLionis This answer is wrong and hides the real bug in your application. The bug is caused by the fact that you have implemented multiple event loops. – Rabbid76 Oct 27 '20 at 19:18
  • @Rabbid76 my bad did not notice but it would be great if the whole script was included – Matiiss Oct 27 '20 at 19:21
  • @AkisLionis I said it hides the bug. But it will cause enormous issue later. – Rabbid76 Oct 27 '20 at 19:21
0

Try changing it to this

           for event in pygame.event.get():
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    elif event.key == pygame.K_c:
                        game_close = False
                        self.gameLoop()
Oliver Hnat
  • 797
  • 4
  • 19
  • Sorry, it did not work. I still take the same values. Thanks for trying. – Akis Lionis Oct 27 '20 at 18:59
  • 1
    Have you tried changing the specific keys? For example changing them to ```pygame.K_SPACE``` or something like that? – Oliver Hnat Oct 27 '20 at 19:04
  • For some reason, pygame.K_SPACE and the arrow keys are working fine. Any other key is not working. Is it because I am working at Ubuntu and not Windows? – Akis Lionis Oct 27 '20 at 19:07
0

The whole code is not included, a class is missing and some methods are not defined. But first I would suggest changing all the key presses to this:

keys = pygame.key.get_pressed()
if keys[pygame.K_yourkey]:
    # do something
if keys[pygame.K_yourotherkey]:
    # do something
# and so on

and dont put it in event for loop

Matiiss
  • 5,970
  • 2
  • 12
  • 29