Here's a simple implementation of the classic Snake game in Python using the Pygame library:
python
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
width, height = 800, 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")
# Define colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
# Define snake properties
snake_size = 20
snake_speed = 15
# Define font for displaying score
font = pygame.font.SysFont(None, 30)
def draw_snake(snake_body):
for segment in snake_body:
pygame.draw.rect(window, white, [segment[0], segment[1], snake_size, snake_size])
def display_score(score):
score_text = font.render("Score: " + str(score), True, white)
window.blit(score_text, [10, 10])
def game_loop():
game_over = False
close_game = False
# Set initial position of the snake
x = width // 2
y = height // 2
# Set initial movement direction
dx = 0
dy = 0
# Initialize snake body
snake_body = []
snake_length = 1
# Generate initial food position
food_x = round(random.randrange(0, width - snake_size) / snake_size) * snake_size
food_y = round(random.randrange(0, height - snake_size) / snake_size) * snake_size
while not game_over:
while close_game:
window.fill(black)
game_over_text = font.render("Game Over! Press Q to Quit or C to Play Again", True, white)
window.blit(game_over_text, [width // 2 - 200, height // 2])
display_score(snake_length - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
close_game = False
if event.key == pygame.K_c:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dx = -snake_size
dy = 0
elif event.key == pygame.K_RIGHT:
dx = snake_size
dy = 0
elif event.key == pygame.K_UP:
dx = 0
dy = -snake_size
elif event.key == pygame.K_DOWN:
dx = 0
dy = snake_size
# Move the snake
x += dx
y += dy
# Check for collision with boundaries
if x >= width or x < 0 or y >= height or y < 0:
close_game = True
# Update the snake body
snake_head = []
snake_head.append(x)
snake_head.append(y)
snake_body.append(snake_head)
if len(snake_body) > snake_length:
del snake_body[0]
# Check for collision with itself
for segment in snake_body[:-1]:
if segment == snake_head:
close_game = True
# Draw the game objects
window.fill(black)
pygame.draw.rect(window, red, [food_x, food_y, snake_size, snake_size])
draw_snake(snake_body)
display_score(snake_length - 1)
# Check for collision with food
if x == food_x and y == food_y:
food_x = round(random.randrange(0, width - snake_size) / snake_size) * snake_size
food_y = round(random.randrange(0, height - snake_size) / snake_size) * snake_size
snake_length += 1
pygame.display.update()
pygame.time.delay(snake_speed)
pygame.quit()
quit()
game_loop()
This code sets up a basic Snake game using Pygame. Here's how it works:
We initialize Pygame and set up the game window with a specified width and height.
We define colors and snake properties such as size and speed.
We create functions to draw the snake, display the score, and handle the main game loop.
In the game loop:
We handle events such as key presses to control the snake's movement direction.
We update the snake's position based on the current movement direction.
We check for collisions with the boundaries and the snake's own body.
We draw the game objects (snake and food) on the window.
We check for collision with the food and increase the snake's length if it eats the food.
We update the display and control the game speed using pygame.time.delay().
If the game is over (collision with boundaries or itself), we display a "Game Over" message and allow the player to quit or play again.
To run this code, make sure you have Pygame installed (pip install pygame). You can customize the game by modifying the window size, snake size, speed, colors, and adding additional features as desired.