2

I just started learning pygame, and I'm working on a snake game. However, I can't seem to turn the snake and I can't find the issue. I think the problem is in the "move_snake" method inside the snake class, but I really can't find the problem. I'm generally new to python and objected-oriented programming as well, so that may be the reason.

import pygame, sys
import random
pygame.init()

clock = pygame.time.Clock()

screen_width = 600
screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")

bgColor = "grey12"
lightGrey = (200, 200, 200)

random_x = random.randint(0, screen_width - 20)
random_y = random.randint(0, screen_height - 20)

class FOOD:
    def __init__(self):
        self.food_x = random_x
        self.food_y = random_y
        self.food_width = 20
        self.food_height = 20
        self.food_rect = pygame.Rect(self.food_x, self.food_y, self.food_width, self.food_height)
    def draw_food(self):
        pygame.draw.rect(screen, lightGrey, self.food_rect)

food = FOOD()

class SNAKE(FOOD):
    def __init__(self):
        super().__init__()
        self.snake_width = 20
        self.snake_height = 20
        self.snake_x = screen_width/2 - self.snake_width
        self.snake_y = screen_height/2 - self.snake_height
        self.snake_rect = pygame.Rect(self.snake_x, self.snake_y, self.snake_width, self.snake_height)
        self.move = [0, -1]
        self.snake_speed = 5
    def draw_snake(self):
        pygame.draw.rect(screen, lightGrey, self.snake_rect)
    def move_snake(self):
        for e in pygame.event.get():
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_UP:
                    self.move = [0, -1]
                if e.key == pygame.K_DOWN:
                    self.move = [0, 1]
                if e.key == pygame.K_LEFT:
                    self.move = [-1, 0]
                if e.key == pygame.K_RIGHT:
                    self.move = [1, 0]
        self.snake_x += self.move[0] * self.snake_speed
        self.snake_y += self.move[1] * self.snake_speed
        self.snake_rect.topleft = (self.snake_x, self.snake_y)

snake = SNAKE()

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill(bgColor)
    food.draw_food()
    snake.draw_snake()

    snake.move_snake()

    pygame.display.flip()

    clock.tick(60)

1 Answers1

0

The issue are the multiple calls to pygame.event.get().

pygame.event.get() get all the messages and remove them from the queue:

This will 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.

Get the events once and use them in multiple loops or pass the list or events to functions and methods where they are handled:

class SNAKE(FOOD):
    # [...]

    def move_snake(self, event_list):
        for e in event_list:
            if e.type == pygame.KEYDOWN:
                # [...]
while True:

    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # [...]

    snake.move_snake(event_list)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174