So in my snake game using pygame, I have set the snake to be able to eat the apple and become longer but I want it to instead make a trail following the snake head instead of there being a long line behind the head.
import pygame
from time import sleep
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (159, 218, 64)
RED = (199, 55, 47)
screen_width = 800
screen_height = 608
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Best Snake game in existance")
font = pygame.font.Font(None, 36)
input_font = pygame.font.Font(None, 24)
RandomX = random.randint(0,screen_width-32)
RandomY = random.randint(0,screen_height-32)
#Checking to make sure apple does not spawn on top of player
while RandomX == 0 and RandomY == 0:
RandomX = random.randint(0,screen_width-32)
RandomY = random.randint(0,screen_height-32)
while RandomX % 32 != 0:
RandomX = random.randint(0,screen_width-32)
while RandomY % 32 != 0:
RandomY = random.randint(0,screen_height-32)
print(RandomX,RandomY)
Snake_Player = pygame.Rect(0, 0, 32, 32)
global Apple
Apple = pygame.Rect(RandomX,RandomY,32,32)
StartingX = 0
StartingY = 0
Movement_Speed = 32
global Apple_Count
Apple_Count = 0
global Snake_Alive
Snake_Alive = True
global running
running = True
current_direction = ""
clock = pygame.time.Clock()
global Apple_Eaten_Check
Apple_Eaten_Check = False
def Make_Snake_Longer():
for x in range(0,Apple_Count):
if current_direction == 'RIGHT':
Last_X = Snake_Player[0]-(32*x)
Last_Movement = pygame.Rect(Last_X,Snake_Player[1],32,32)
pygame.draw.rect(screen,BLACK,Last_Movement)
elif current_direction == 'LEFT':
Last_X = Snake_Player[0]+(32*x)
Last_Movement = pygame.Rect(Last_X,Snake_Player[1],32,32)
pygame.draw.rect(screen,BLACK,Last_Movement)
elif current_direction == 'DOWN':
Last_Y = Snake_Player[1]-(32*x)
Last_Movement = pygame.Rect(Snake_Player[0],Last_Y,32,32)
pygame.draw.rect(screen,BLACK,Last_Movement)
elif current_direction == 'UP':
Last_Y = Snake_Player[1]+(32*x)
Last_Movement = pygame.Rect(Snake_Player[0],Last_Y,32,32)
pygame.draw.rect(screen,BLACK,Last_Movement)
def New_Apple_Coordinates():
global Apple
RandomX = random.randint(0,screen_width)
RandomY = random.randint(0,screen_height)
while RandomX == 0 and RandomY == 0:
RandomX = random.randint(0,screen_width-32)
RandomY = random.randint(0,screen_height-32)
while RandomX % 32 != 0:
RandomX = random.randint(0,screen_width-32)
while RandomY % 32 != 0:
RandomY = random.randint(0,screen_height-32)
Apple = pygame.Rect(RandomX,RandomY,32,32)
Draw_Apple()
def Draw_Apple():
pygame.draw.rect(screen,RED,Apple)
pygame.display.update()
def Apple_Eaten():
global Apple_Count, Apple_Eaten_Check,Apple
Apple_Eaten_Check = True
Apple_Count += 1
New_Apple_Coordinates()
def Check_Boundries():
global StartingX, StartingY,Snake_Alive
if StartingX < 0:
StartingX = 0
Snake_Loss()
elif StartingY < 0:
StartingY = 0
Snake_Loss()
elif StartingX > screen_width - Movement_Speed:
StartingX = screen_width - Movement_Speed
Snake_Loss()
elif StartingY > screen_height - Movement_Speed:
StartingY = screen_height - Movement_Speed
Snake_Loss()
def Snake_Loss():
global running,Snake_Alive
Snake_Alive = False
LossScreen = pygame.Rect(200,150,400,300)
pygame.draw.rect(screen, BLACK, LossScreen)
pygame.display.update()
def move_snake():
global Snake_Player, StartingX, StartingY, current_direction,Snake_Alive,Apple_Count
if Snake_Alive:
if current_direction == "UP":
StartingY -= Movement_Speed
Check_Boundries()
if Snake_Player.colliderect(Apple):
Apple_Eaten()
elif current_direction == "DOWN":
StartingY += Movement_Speed
Check_Boundries()
if Snake_Player.colliderect(Apple):
Apple_Eaten()
elif current_direction == "LEFT":
StartingX -= Movement_Speed
Check_Boundries()
if Snake_Player.colliderect(Apple):
Apple_Eaten()
elif current_direction == "RIGHT":
StartingX += Movement_Speed
Check_Boundries()
if Snake_Player.colliderect(Apple):
Apple_Eaten()
Snake_Player = pygame.Rect(StartingX, StartingY, 32, 32)
def handle_events():
global current_direction,Snake_Alive
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if Snake_Alive:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w and current_direction != "DOWN":
current_direction = "UP"
elif event.key == pygame.K_s and current_direction != "UP":
current_direction = "DOWN"
elif event.key == pygame.K_a and current_direction != "RIGHT":
current_direction = "LEFT"
elif event.key == pygame.K_d and current_direction != "LEFT":
current_direction = "RIGHT"
while running:
if Snake_Alive:
screen.fill(GREEN)
pygame.display.update()
handle_events()
move_snake()
Draw_Apple()
if Apple_Count == 0:
pygame.draw.rect(screen,RED,Apple)
pygame.display.update()
if Apple_Eaten_Check:
pygame.draw.rect(screen,RED,Apple)
pygame.display.update()
Apple_Eaten_Check = False
pygame.display.update()
pygame.draw.rect(screen, BLACK, Snake_Player)
Make_Snake_Longer()
pygame.display.update()
sleep(0.15)
clock.tick(60)
pygame.quit()
My only idea to fix this was to add a sleep for when the blocks behind the snakes moves but that did not work