My current code makes any added length from eating apples just stay in a straight line behind the head of a snake. How do I make it so it is a trail like normal snake games? If someone could help me approach the problem rather than pasting could that would be nice.
here is my full code (it uses png files so it might not run if you try it)
import pygame, random, time
pygame.init()
screenWidth = 500
screenHeight = 500
window = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Snake")
x = random.randint(30, 440)
y = random.randint(30, 44)
snakex = x-30
snakey = y
previousx = snakex
previousy = snakey
run = True
appleNeeded = True
direction = "right"
lengthNeeded = False
length1 = True
lengthAmount = 3
check = True
snake_body = []
#Create images (Snake head, apple, and snake tail)
#class SnakeHead(pygame.sprite.Sprite):
# def __init__(self, ):
snakeHeadUp = pygame.image.load("snakeHeadUp.png")
snakeHeadUp = pygame.transform.scale(snakeHeadUp, (30, 30))
snakeHeadLeft = pygame.image.load("snakeHeadLeft.png")
snakeHeadLeft = pygame.transform.scale(snakeHeadLeft, (30, 30))
snakeHeadRight = pygame.image.load("snakeHeadRight.png")
snakeHeadRight = pygame.transform.scale(snakeHeadRight, (30, 30))
snakeHeadDown = pygame.image.load("snakeHeadDown.png")
snakeHeadDown = pygame.transform.scale(snakeHeadDown, (30, 30))
apple = pygame.image.load("apple.png")
apple = pygame.transform.scale(apple, (30, 30))
snakeHead = snakeHeadRight
while run:
if check == True:
snake_body.append((x, y))
check = False
#Create screen with boundaries
window.fill((0, 0, 255))
#Create snake head (sprite)
#Create snake
#Create apple randomly (either rect or sprite) and if needed
if appleNeeded == True:
randomx = random.randint(30, 440)
randomy = random.randint(30, 440)
if randomx > 5:
randomx = randomx - (randomx % 5)
elif randomx < 5:
randomx = randomx + (5 % randomx)
if randomy > 5:
randomy = randomy - (randomy % 5)
elif randomy < 5:
randomy = randomy + (5 % randomy)
appleNeeded = False
window.blit(apple, (randomx, randomy))
#Determine if snake head has touched apple
if x-15 >= randomx-30 and x-15 < randomx and y-15 >= randomy-30 and y-15 < randomy:
appleNeeded = True
lengthAmount += 1
snake_body.append("1")
#Check for key updates
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
direction = "left"
snakeHead = snakeHeadLeft
if keys[pygame.K_RIGHT]:
direction = "right"
snakeHead = snakeHeadRight
if keys[pygame.K_UP]:
direction = "up"
snakeHead = snakeHeadUp
if keys[pygame.K_DOWN]:
direction = "down"
snakeHead = snakeHeadDown
if direction == "left":
x -= 5
snakex = x+30
snakey = y
elif direction == "right":
x += 5
snakex = x-30
snakey = y
elif direction == "up":
y -= 5
snakex = x
snakey = y+30
elif direction == "down":
y += 5
snakex = x
snakey = y-30
#Update snake length
window.blit(snakeHead, (x, y))
for i in range(lengthAmount):
if i == 0:
previousx = snakex - (30 * i)
previousy = snakey
if direction == "left":
previousx = snakex + (30 * i)
previousy = snakey
elif direction == "right":
previousx = snakex - (30 * i)
previousy = snakey
elif direction == "up":
previousx = snakex
previousy = snakey + (30 * i)
elif direction == "down":
previousx = snakex
previousy = snakey - (30 * i)
for body in snake_body:
pygame.draw.rect(window, (0, 175, 0), (previousx, previousy, 30, 30))
#Don't allow snake to leave the window (game over if True)
if x >= 470 or x <= 0 or y <= 0 or y >= 470:
run = False
#Don't allow snake to eat itself (game over if True)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()