0% found this document useful (0 votes)
20 views5 pages

Python Snake Game with Pygame

python snake game

Uploaded by

hi2539
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Python Snake Game with Pygame

python snake game

Uploaded by

hi2539
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import pygame

import random

import time

# Initialize pygame

[Link]()

# Colors

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

DARK_GREEN = (0, 155, 0)

# Screen dimensions

WIDTH, HEIGHT = 600, 600

GRID_SIZE = 20

GRID_WIDTH = WIDTH // GRID_SIZE

GRID_HEIGHT = HEIGHT // GRID_SIZE

# Set up the display

screen = [Link].set_mode((WIDTH, HEIGHT))

[Link].set_caption('Snake Game - By Grok')

clock = [Link]()

font = [Link]("verdana", 35)

score_font = [Link]("comicsansms", 25)


def draw_grid():

for x in range(0, WIDTH, GRID_SIZE):

for y in range(0, HEIGHT, GRID_SIZE):

rect = [Link](x, y, GRID_SIZE, GRID_SIZE)

[Link](screen, (40, 40, 40), rect, 1)

def game_over_text():

game_over_surface = [Link]('Game Over!', True, RED)

score_surface = [Link](f'Score: {len(snake) - 1}', True, WHITE)

restart_surface = score_font.render('Press SPACE to Restart or Q to Quit', True, WHITE)

[Link](game_over_surface, (WIDTH//2 - game_over_surface.get_width()//2, 200))

[Link](score_surface, (WIDTH//2 - score_surface.get_width()//2, 280))

[Link](restart_surface, (WIDTH//2 - restart_surface.get_width()//2, 350))

# Snake initial position (center)

snake = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]

snake_direction = (1, 0) # Start moving right

snake_speed = 10 # FPS

# Food

food = ([Link](0, GRID_WIDTH - 1), [Link](0, GRID_HEIGHT - 1))

# Game loop

running = True

game_over = False

while running:
for event in [Link]():

if [Link] == [Link]:

running = False

if [Link] == [Link]:

if game_over:

if [Link] == pygame.K_SPACE:

# Restart game

snake = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]

snake_direction = (1, 0)

food = ([Link](0, GRID_WIDTH - 1), [Link](0, GRID_HEIGHT -


1))

game_over = False

if [Link] == pygame.K_q:

running = False

else:

if [Link] == pygame.K_UP and snake_direction != (0, 1):

snake_direction = (0, -1)

if [Link] == pygame.K_DOWN and snake_direction != (0, -1):

snake_direction = (0, 1)

if [Link] == pygame.K_LEFT and snake_direction != (1, 0):

snake_direction = (-1, 0)

if [Link] == pygame.K_RIGHT and snake_direction != (-1, 0):

snake_direction = (1, 0)

if game_over:

screen.fill(BLACK)

game_over_text()
[Link]()

continue

# Move snake

head_x = snake[0][0] + snake_direction[0]

head_y = snake[0][1] + snake_direction[1]

new_head = (head_x, head_y)

# Check collisions with walls

if (head_x < 0 or head_x >= GRID_WIDTH or

head_y < 0 or head_y >= GRID_HEIGHT or

new_head in snake):

game_over = True

continue

[Link](0, new_head)

# Check if food eaten

if new_head == food:

food = ([Link](0, GRID_WIDTH - 1), [Link](0, GRID_HEIGHT - 1))

# Increase speed slightly every 5 points

if len(snake) % 5 == 0:

snake_speed += 2

else:

[Link]() # Remove tail

# Draw everything

screen.fill(BLACK)
draw_grid()

# Draw food

food_rect = [Link](food[0] * GRID_SIZE, food[1] * GRID_SIZE, GRID_SIZE,


GRID_SIZE)

[Link](screen, RED, food_rect)

# Draw snake

for i, segment in enumerate(snake):

segment_rect = [Link](segment[0] * GRID_SIZE, segment[1] * GRID_SIZE,


GRID_SIZE, GRID_SIZE)

if i == 0:

[Link](screen, GREEN, segment_rect) # Head

[Link](screen, DARK_GREEN, segment_rect, 3)

else:

[Link](screen, (0, 200, 0), segment_rect)

[Link](screen, DARK_GREEN, segment_rect, 2)

# Display score

score_text = score_font.render(f"Score: {len(snake) - 1} Length: {len(snake)}", True,


WHITE)

[Link](score_text, (10, 10))

[Link]()

[Link](snake_speed)

[Link]()

print(f"Final Score: {len(snake) - 1}")

You might also like