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!