I am completely stumped. I surrounded the crux of the problem with hashtags. Any help is much appreciated!
import pygame
import time
import random
pygame.init()
display_width = 600
display_height = 600
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
fps = 10
white = (255, 255, 255)
red = (200, 0, 0)
green = (0, 200, 0)
blue = (0, 0, 200)
black = (0, 0, 0)
def extend_snake(snake):
x = snake[len(snake) - 1][0]
y = snake[len(snake) - 1][1]
## for i in snake:
## print(i)
return ([x, y])
def spawn_fruit():
x = random.randrange(fruit_rad, display_width - fruit_rad, 2 * fruit_rad)
y = random.randrange(fruit_rad, display_height - fruit_rad, 2 * fruit_rad)
return ((x, y))
def pause():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
return
score = 0
snake = []
snake_size = 10
snake.append([display_width / 2, display_height / 2])
snake_dx = 0
snake_dy = 0
fruit = []
fruit_count = 5
fruit_rad = 5
for a in range(fruit_count):
fruit.append(spawn_fruit())
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
pause()
elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
snake_dx = -snake_size
snake_dy = 0
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
snake_dx = snake_size
snake_dy = 0
elif event.key == pygame.K_w or event.key == pygame.K_UP:
snake_dy = -snake_size
snake_dx = 0
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
snake_dy = snake_size
snake_dx = 0
print(snake)
for coord in fruit:
if snake[0][0] + fruit_rad == coord[0] and snake[0][1] + fruit_rad == coord[1]:
fruit.remove(coord)
fruit.append(spawn_fruit())
snake.append(extend_snake(snake))
score += 1
fps += 1
for i in range(len(snake) - 1, 0, -1):
snake[i] = snake[i - 1]
################################################
try:
print('1', snake[1])
except:
pass
snake[0][0] += snake_dx
snake[0][1] += snake_dy
try:
print('2', snake[1])
except:
pass
#########################################################
display.fill(white)
for coord in fruit:
pygame.draw.circle(display, red, coord, fruit_rad)
for coord in snake:
pygame.draw.rect(display, black, [coord[0], coord[1], snake_size, snake_size])
pygame.display.update()
clock.tick(fps)
This will return for example:
1 (100, 100)
2 (110, 100)
No matter where I print(snake), every list is the same within it. Could somebody please explain why this is happening and what I can do to prevent it?
Setting snake[i] = snake[i-1] makes them the same object and thus explaining why they were both being edited even though the code only edited one of them.