Apologies for the oversight. The issue in the code is that the food_pos variable is defined inside the game() function but is being referenced before assignment.
To fix this, you can move the food_pos variable definition outside the game() function and make it a global variable. Here's the modified code:
import pygame
import random
# Initialize Pygame
pygame.init()
# Window dimensions
width, height = 640, 480
window = pygame.display.set_mode((width, height))
pygame.display.set_caption('Gluttonous Snake')
# Colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# Snake initial position and size
snake_pos = [[100, 50], [90, 50], [80, 50]]
snake_size = 15
# Food initial position and size
food_pos = [random.randrange(1, width // 10) * 10,
random.randrange(1, height // 10) * 10]
food_size = 10
# Initial direction
direction = 'RIGHT'
change_to = direction
# Score tracking
score = 0
# Game over
game_over = False
# Velocity
vel = 10
# FPS (frames per second) controller
fps = pygame.time.Clock()
# Function to display the score
def show_score(choice, color, font, size):
score_font = pygame.font.SysFont(font, size)
score_surface = score_font.render('Score : ' + str(score), True, color)
score_rect = score_surface.get_rect()
if choice == 1:
score_rect.midtop = (width // 10, 15)
else:
score_rect.midtop = (width // 2, height / 1.25)
window.blit(score_surface, score_rect)
# Main game function
def game():
global game_over, score, change_to, direction, food_pos
while not game_over:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == ord('w'):
change_to = 'UP'
if event.key == pygame.K_DOWN or event.key == ord('s'):
change_to = 'DOWN'
if event.key == pygame.K_LEFT or event.key == ord('a'):
change_to = 'LEFT'
if event.key == pygame.K_RIGHT or event.key == ord('d'):
change_to = 'RIGHT'
# Snake direction validation
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# Update snake position
if direction == 'UP':
snake_pos[0][1] -= vel
if direction == 'DOWN':
snake_pos[0][1] += vel
if direction == 'LEFT':
snake_pos[0][0] -= vel
if direction == 'RIGHT':
snake_pos[0][0] += vel
# Check for boundaries or self-collision
if snake_pos[0][0] < 0 or snake_pos[0][0] > width - snake_size or snake_pos[0][1] < 0 or snake_pos[0][1] > height - snake_size:
game_over = True
for block in snake_pos[1:]:
if snake_pos[0][0] == block[0] and snake_pos[0][1] == block[1]:
game_over = True
# Check if snake eats the food
if snake_pos[0][0] == food_pos[0] and snake_pos[0][1] == food_pos[1]:
score += 1
food_pos = [random.randrange(1, width // 10) * 10,
random.randrange(1, height // 10) * 10]
else:
snake_pos.pop()
# Draw game window
window.fill(black)
for pos in snake_pos:
pygame.draw.rect(window, green, pygame.Rect(
pos[0], pos[1], snake_size, snake_size))
pygame.draw.rect(window, blue, pygame.Rect(
food_pos[0], food_pos[1], food_size, food_size))
# Update the score
show_score(1, white, 'Arial', 20)
# Refresh the game screen
pygame.display.update()
# FPS controller
fps.tick(30)
# Run the game
game()
# Quit the game
pygame.quit()
With this modification, the code should run without any errors, and you can play the Gluttonous Snake game.