Certainly! Below is a simple implementation of the classic Snake game using Python 3 and Pygame. This example covers the basic gameplay: moving the snake, eating food, growing, and detecting collisions with the walls and itself.
Make sure you have Pygame installed before running the code:
pip install pygame
Snake Game in Python with Pygame
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 600, 400
CELL_SIZE = 20
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
DARK_GREEN = (0, 100, 0)
# Set up display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Set up clock for controlling frame rate
clock = pygame.time.Clock()
FPS = 10
# Font for score
font = pygame.font.SysFont("arial", 25)
def draw_text(text, color, x, y):
img = font.render(text, True, color)
screen.blit(img, (x, y))
def random_food_position():
x = random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE
y = random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE
return (x, y)
def main():
# Initial snake setup
snake = [(WIDTH // 2, HEIGHT // 2)]
direction = (0, 0) # Start stationary
snake_length = 1
food = random_food_position()
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Control the snake with arrow keys or WASD
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != (0, CELL_SIZE):
direction = (0, -CELL_SIZE)
elif event.key == pygame.K_DOWN and direction != (0, -CELL_SIZE):
direction = (0, CELL_SIZE)
elif event.key == pygame.K_LEFT and direction != (CELL_SIZE, 0):
direction = (-CELL_SIZE, 0)
elif event.key == pygame.K_RIGHT and direction != (-CELL_SIZE, 0):
direction = (CELL_SIZE, 0)
if direction != (0, 0):
# Move snake by adding new head based on direction
new_head = (snake[0][0] + direction[0], snake[0][1] + direction[1])
# Check for collisions with walls
if (new_head[0] < 0 or new_head[0] >= WIDTH or
new_head[1] < 0 or new_head[1] >= HEIGHT):
running = False # Game over
# Check for collisions with itself
if new_head in snake:
running = False # Game over
snake.insert(0, new_head)
# Check if snake has eaten the food
if new_head == food:
snake_length += 1
food = random_food_position()
else:
# Remove tail if not growing
if len(snake) > snake_length:
snake.pop()
# Drawing everything
screen.fill(BLACK)
# Draw food
pygame.draw.rect(screen, RED, (*food, CELL_SIZE, CELL_SIZE))
# Draw snake
for segment in snake:
pygame.draw.rect(screen, GREEN, (*segment, CELL_SIZE, CELL_SIZE))
# Draw snake head darker
pygame.draw.rect(screen, DARK_GREEN, (*snake[0], CELL_SIZE, CELL_SIZE))
# Draw score
draw_text(f"Score: {snake_length - 1}", WHITE, 10, 10)
pygame.display.flip()
# Game over message
screen.fill(BLACK)
draw_text("Game Over!", RED, WIDTH // 2 - 70, HEIGHT // 2 - 20)
draw_text(f"Final Score: {snake_length - 1}", WHITE, WIDTH // 2 - 90, HEIGHT // 2 + 20)
pygame.display.flip()
pygame.time.wait(3000)
pygame.quit()
if __name__ == "__main__":
main()
How to Play
- Use arrow keys or WASD to move the snake.
- Eat the red food squares to grow longer.
- Avoid hitting the walls or the snake's own body.
- The game ends when you collide with a wall or yourself.
If you want me to add features like a start screen, pause, or high score saving, just ask!