-1

I'm working on a snake game using pygame and trying to get the rect objects that make up the snake's body to appear side by side when the game starts but they don't. Instead the rect objects appear to move towards the right overlapping each other until all of them are stacked on top of each other. I need to get them to appear side by side so that the self_eating function I've implemented could work. Here's part of my implementation of the Snake class.

class Snake:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
        self.rect = pygame.rect.Rect([0, 0, tile_size, tile_size])
        self.rect.center = [self.x, self.y]  # set the position of the head
        self.direction = [0, 0]
        self.directions = {pygame.K_w: 1, pygame.K_s: 1, pygame.K_a: 1, pygame.K_d: 1}
        self.time = 0
        self.length = 6

        self.segments = []  # add the head to the segments list

        for i in range(self.length):
            segment_rect = self.rect.copy()
            segment_rect.center = [self.x - (i + 1) * tile_size/2, self.y]
            self.segments.append(segment_rect)

    def draw(self):
        for segment in self.segments:
            pygame.draw.rect(screen, GREEN, (segment[0],segment[1],tile_size, tile_size))
        pygame.display.flip()

    def move_snake(self):
        self.rect.move_ip(self.direction[0] * tile_size, self.direction[1] * tile_size)

        for i in range(len(self.segments)):
            index = len(self.segments) - i - 1
            if index == 0:
                self.segments[index].center = self.rect.center
            else:
                self.segments[index].center = self.segments[index - 1].center

    def control_snake(self, event):
        if event.type == pygame.QUIT:
            quit()
            
        elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a and self.directions[pygame.K_a]:
                    self.direction = [-1, 0]
                    self.directions = {pygame.K_w: 1, pygame.K_s: 1, pygame.K_a: 1, pygame.K_d: 0}
                elif event.key == pygame.K_d and self.directions[pygame.K_d]:
                    self.direction = [1, 0]
                    self.directions = {pygame.K_w: 1, pygame.K_s: 1, pygame.K_a: 0, pygame.K_d: 1}
                elif event.key == pygame.K_w and self.directions[pygame.K_w]:
                    self.direction = [0, -1]
                    self.directions = {pygame.K_w: 1, pygame.K_s: 0, pygame.K_a: 1, pygame.K_d: 1}
                elif event.key == pygame.K_s and self.directions[pygame.K_s]:
                    self.direction = [0, 1]
                    self.directions = {pygame.K_w: 0, pygame.K_s: 1, pygame.K_a: 1, pygame.K_d: 1}
                elif event.key == pygame.K_q:
                    pygame.quit()
                    quit()

And this is how I'm checking my code

tile_size = 25

win_width = 800
win_height = 550

screen = pygame.display.set_mode((win_width, win_height))

snake = Snake(255, 255)

done = False

while not done:
    for event in pygame.event.get():
        snake.control_snake(event)

    snake.move_snake()

    screen.fill(WHITE)
    snake.draw()
    pygame.display.update()

    clock.tick(10)

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
ayy
  • 35
  • 7

0 Answers0