Certainly! Here's an example of a simple snake game implemented in Python using the Pygame library:
import pygame
import time
# Initialize Pygame
pygame.init()
# Define colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Set the width and height of the game window
window_width = 800
window_height = 600
game_window = pygame.display.set_mode((window_width, window_height))
# Set the title of the game window
pygame.display.set_caption("Snake Game")
# Set the clock object to control the frame rate
clock = pygame.time.Clock()
# Set the font object
font_style = pygame.font.SysFont(None, 50)
def display_score(score):
score_text = font_style.render("Score: " + str(score), True, BLACK)
game_window.blit(score_text, (10, 10))
def game_loop():
# Initial position and size of the snake
x = window_width / 2
y = window_height / 2
snake_size = 20
# Initial speed of the snake
x_speed = 0
y_speed = 0
# Initial position of the food
food_x = round((window_width / 2) / 10.0) * 10.0
food_y = round((window_height / 2) / 10.0) * 10.0
# Initial score
score = 0
# Game over flag
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# Exit the game when the close button is clicked
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -snake_size
y_speed = 0
elif event.key == pygame.K_RIGHT:
x_speed = snake_size
y_speed = 0
elif event.key == pygame.K_UP:
x_speed = 0
y_speed = -snake_size
elif event.key == pygame.K_DOWN:
x_speed = 0
y_speed = snake_size
# Move the snake
x += x_speed
y += y_speed
# Draw the snake and food on the game window
game_window.fill(GREEN)
pygame.draw.rect(game_window, BLACK, [food_x, food_y, snake_size, snake_size])
pygame.draw.rect(game_window, BLACK, [x, y, snake_size, snake_size])
# Check if the snake has collided with the food
if x == food_x and y == food_y:
# Increase the score and generate new food
score += 1
food_x = round((window_width / 2) / 10.0) * 10.0
food_y = round((window_height / 2) / 10.0) * 10.0
# Update the score
display_score(score)
# Update the game window
pygame.display.update()
# Set the frame rate
clock.tick(10)
# Display "Game Over" for 2 seconds
message = font_style.render("Game Over", True, BLACK)
game_window.blit(message, (window_width / 2 - 100, window_height / 2))
pygame.display.update()
time.sleep(2)
# Restart the game loop
game_loop()
# Start the game loop
game_loop()
To run the game, you will need to have the Pygame library installed. You can install it using the following command:
pip install pygame
The game window will open, and you can control the snake using the arrow keys. The objective of the game is to eat the food, which increases your score. When the snake collides with the boundaries of the game window or with itself, it's game over. You can restart the game by closing the "Game Over" message window.
This is just a basic implementation of the snake game, and you can enhance it further by adding features like levels, obstacles, or a high score system.