Sure, I'll add console logging to the sample code to help with troubleshooting potential errors or understanding the flow of the game. Please note that in a typical application, you would potentially use a logging library, but for simplicity, I'll just use print statements here.
import pygame
import time
import random
import sys
# Initialize the pygame modules to get everything started.
pygame.init()
# The dimensions of the game window.
window_width = 800
window_height = 600
# Initialize the game window.
game_window = pygame.display.set_mode((window_width, window_height))
# Define some colors in RGB format.
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)
# Set the FPS (frames per second) controller.
clock = pygame.time.Clock()
# Define the snake's default position.
snake_pos = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
snake_speed = 15
# Define the direction the snake moves in.
# It could be up, down, left, or right.
change_to = 'RIGHT'
direction = 'RIGHT'
# Initialize the score
score = 0
# Define how to exit the game
def game_close():
pygame.quit()
sys.exit()
# Define the snake function.
def snake(snake_block, snake_body):
for pos in snake_body:
# Draw each block of the snake's body
pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], snake_block, snake_block))
# Define the function to create food for the snake.
def create_food():
return [random.randrange(1, (window_width // snake_block)) * snake_block,
random.randrange(1, (window_height // snake_block)) * snake_block]
# Initialize food position
food_pos = create_food()
# Main function for the game.
def game():
global snake_pos, snake_body, direction, change_to, food_pos, score
running = True
while running:
# Handling key events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != 'DOWN':
change_to = 'UP'
elif event.key == pygame.K_DOWN and direction != 'UP':
change_to = 'DOWN'
elif event.key == pygame.K_LEFT and direction != 'RIGHT':
change_to = 'LEFT'
elif event.key == pygame.K_RIGHT and direction != 'LEFT':
change_to = 'RIGHT'
elif event.type == pygame.QUIT:
running = False
# Update the direction of the snake
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
elif change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
elif change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
elif change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# Update the position of the snake's head.
if direction == 'UP':
snake_pos[1] -= snake_block
elif direction == 'DOWN':
snake_pos[1] += snake_block
elif direction == 'LEFT':
snake_pos[0] -= snake_block
elif direction == 'RIGHT':
snake_pos[0] += snake_block
# Growing the snake whenever it eats food.
snake_body.insert(0, list(snake_pos))
if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
score += 1
print('Yummy! Score: ', score)
food_pos = create_food()
else:
snake_body.pop()
# Game Over conditions
if snake_pos[0] < 0 or snake_pos[0] > window_width - snake_block:
print('Snake hit the wall! Game over!')
running = False
if snake_pos[1] < 0 or snake_pos[1] > window_height - snake_block:
print('Snake hit the wall! Game over!')
running = False
for block in snake_body[1:]:
if snake_pos == block:
print('Snake hit itself! Game over!')
running = False
# Update the display and fill the game window with the white color.
game_window.fill(white)
# Draw the snake and the food
snake(snake_block, snake_body)
pygame.draw.rect(game_window, red, pygame.Rect(food_pos[0], food_pos[1], snake_block, snake_block))
# Update the full display Surface to the screen
pygame.display.flip()
# Set the speed of the snake.
clock.tick(snake_speed)
# Exiting the game as it's over
game_close()
# Start the game.
game()
This code represents a simple snake game where console logging helps to track the snake's score, as well as when and why the game ends. Note that for a real project, you'd likely want to replace these print statements with proper logging, and might want to handle the end-of-game scenario more gracefully than just closing the window abruptly. This example is for educational purposes and designed to be a starting point.
Please remember to run this code in a file, preferably in an IDE or environment where Pygame is correctly setup to open a window. Running this kind of script in a Jupyter Notebook or other type of REPL interface may not work as expected, as those environments might not support creating game windows or handling real-time user input correctly.