I'm trying to make a 'snake game' in pygame. I created an array that I planned on using to store a vector that contains the snake's current position every time the update function is ran (I will slice the array at the end of the loop to hold only the last few positions).
I wanted to use the snake's last position as a parameter for when the snake eventually eats food and the tail blocks can be initialized and continue to follow the snake according to its last position. However when I printed the array to the log, it only held the same vector being repeated over and over again. I thought my logic was sound but clearly I went wrong somewhere. Any help would be appreciated,
Thanks!
class Snake:
def __init__(self,game):
pygame.init()
self.game = game
self.image = pygame.Surface((20,20))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.speed = 20
self.velocity = vec(0,0)
self.position = vec(0,0)
self.move_log = []
self.right,self.left,self.down,self.up = False,False,False,False
self.tail = []
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_RIGHT] and not self.left:
self.right = True
self.left,self.down,self.up = False,False,False
self.velocity.x = self.speed
elif key[pygame.K_LEFT] and not self.right:
self.left = True
self.right,self.down,self.up = False,False,False
self.velocity.x = -self.speed
elif key[pygame.K_DOWN] and not self.up:
self.down = True
self.left,self.right,self.up = False,False,False
self.velocity.y = self.speed
elif key[pygame.K_UP] and not self.down:
self.up = True
self.left,self.down,self.right = False,False,False
self.velocity.y = -self.speed
if self.right or self.left:
self.velocity.y = 0
elif self.down or self.up:
self.velocity.x = 0
self.eat()
current_position = self.position
self.position += self.velocity
self.move_log.append(current_position)
self.rect.x,self.rect.y = self.position