Skip to content

Instantly share code, notes, and snippets.

@sublimator
Created February 27, 2024 11:46
Show Gist options
  • Save sublimator/063b582168b0eea79a30a98ad3249731 to your computer and use it in GitHub Desktop.
Save sublimator/063b582168b0eea79a30a98ad3249731 to your computer and use it in GitHub Desktop.
import pygame
import random
# Initialize Pygame
pygame.init()
# Set display dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Define snake properties
SNAKE_SIZE = 20
SNAKE_SPEED = 10
# Define food properties
FOOD_SIZE = 20
# Function to draw the snake
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block, snake_block])
# Function to draw the food
def draw_food(foodx, foody):
pygame.draw.rect(screen, RED, [foodx, foody, FOOD_SIZE, FOOD_SIZE])
# Function to display game over message
def game_over():
font_style = pygame.font.SysFont(None, 30)
message = font_style.render("Game Over! Press C to play again or Q to quit.", True, RED)
screen.blit(message, [WIDTH / 6, HEIGHT / 3])
# Main game loop
def game_loop():
global SNAKE_SPEED
x1 = WIDTH / 2
y1 = HEIGHT / 2
x1_change = 0
y1_change = 0
snake_list = []
snake_length = 1
foodx = round(random.randrange(0, WIDTH - FOOD_SIZE) / 10.0) * 10.0
foody = round(random.randrange(0, HEIGHT - FOOD_SIZE) / 10.0) * 10.0
game_over = False
clock = pygame.time.Clock()
while not game_over:
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 and x1_change != SNAKE_SIZE:
x1_change = -SNAKE_SIZE
y1_change = 0
elif event.key == pygame.K_RIGHT and x1_change != -SNAKE_SIZE:
x1_change = SNAKE_SIZE
y1_change = 0
elif event.key == pygame.K_UP and y1_change != SNAKE_SIZE:
y1_change = -SNAKE_SIZE
x1_change = 0
elif event.key == pygame.K_DOWN and y1_change != -SNAKE_SIZE:
y1_change = SNAKE_SIZE
x1_change = 0
# Check for game over conditions
if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
game_over = True
for x in snake_list[:-1]:
if x == [x1, y1]:
game_over = True
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
draw_food(foodx, foody)
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
draw_snake(SNAKE_SIZE, snake_list)
# Check if snake eats food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, WIDTH - FOOD_SIZE) / 10.0) * 10.0
foody = round(random.randrange(0, HEIGHT - FOOD_SIZE) / 10.0) * 10.0
snake_length += 1
SNAKE_SPEED += 1
pygame.display.update()
clock.tick(SNAKE_SPEED)
# Game over screen
while game_over:
screen.fill(BLACK)
game_over()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
game_loop()
if event.key == pygame.K_q:
pygame.quit()
quit()
game_loop()
pygame.quit()
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment