0

I am making a snake game in pygame, and when I add body parts, they don't trail behind the snake, they glitch out. Does anyone know why and how I could fix this?

class Snake:
    def draw(self):
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        pygame.draw.rect(screen, BLACK, self.rect)

    def events(self):
        # change direction on key press
        self.keys = pygame.key.get_pressed()

        if self.keys[pygame.K_UP]:
            self.direction = 1
        if self.keys[pygame.K_DOWN]:
            self.direction = 3
        if self.keys[pygame.K_LEFT]:
            self.direction = 4
        if self.keys[pygame.K_RIGHT]:
            self.direction = 2

    def update(self):
        # move
        if self.direction == 1:
            self.y -= self.height
        if self.direction == 2:
            self.x += self.width
        if self.direction == 3:
            self.y += self.height
        if self.direction == 4:
            self.x -= self.width

        if self.collide:
            self.parts.append('Part')
            print(self.parts)
            self.collide = False

        for i in range(len(self.parts)):
            pygame.draw.rect(screen, RED, (self.x, self.y + (self.height * i), self.width, self.height))

Thanks!

  • Hi, welcome to SO! Could you post an example of what you mean (a screenshot or something of the sort) – Joseph Chotard Mar 22 '20 at 16:29
  • Please update your question to include a [mcve]. If you want to increase the chance of a useful answer to your questions, you can read about [How to Ask](https://stackoverflow.com/questions/how-to-ask). – import random Mar 23 '20 at 22:41

0 Answers0