Skip to content

Instantly share code, notes, and snippets.

@Xnuvers007
Created May 19, 2024 11:53
Show Gist options
  • Save Xnuvers007/582442c995bb356dc5312ef9c0e0ebb7 to your computer and use it in GitHub Desktop.
Save Xnuvers007/582442c995bb356dc5312ef9c0e0ebb7 to your computer and use it in GitHub Desktop.
this is script game snake build using python
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the screen
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")
# Set up colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Set up the snake
block_size = 20
snake_speed = 10
snake_block = []
snake_length = 1
snake_x = screen_width / 2
snake_y = screen_height / 2
snake_dx = 0
snake_dy = 0
# Set up the food
food_size = 20
food_x = round(random.randrange(0, screen_width - food_size) / 20) * 20
food_y = round(random.randrange(0, screen_height - food_size) / 20) * 20
# Set up the clock
clock = pygame.time.Clock()
# Set up game variables
score = 0
# Function to draw snake
def draw_snake(snake_block):
for block in snake_block:
pygame.draw.rect(screen, snake_color, [block[0], block[1], block_size, block_size])
# Function to display score
def display_score(score):
font = pygame.font.Font(None, 36)
score_text = font.render("Score: " + str(score), True, WHITE)
screen.blit(score_text, (10, 10))
# Function to display menu
def display_menu():
font = pygame.font.Font(None, 36)
title_text = font.render("Snake Game", True, WHITE)
screen.blit(title_text, (screen_width / 2 - title_text.get_width() / 2, 50))
skin_text = font.render("Choose Snake Skin (1, 2, or 3):", True, WHITE)
screen.blit(skin_text, (screen_width / 2 - skin_text.get_width() / 2, 150))
snake_skin1_text = font.render("1. Green", True, WHITE)
screen.blit(snake_skin1_text, (screen_width / 2 - snake_skin1_text.get_width() / 2, 200))
snake_skin2_text = font.render("2. Blue", True, WHITE)
screen.blit(snake_skin2_text, (screen_width / 2 - snake_skin2_text.get_width() / 2, 250))
snake_skin3_text = font.render("3. Red", True, WHITE)
screen.blit(snake_skin3_text, (screen_width / 2 - snake_skin3_text.get_width() / 2, 300))
# Function to set snake color based on skin choice
def set_snake_color(skin_choice):
global snake_color
if skin_choice in ['1', '2', '3']: # Validate skin choice
if skin_choice == '1':
snake_color = GREEN
elif skin_choice == '2':
snake_color = BLUE
elif skin_choice == '3':
snake_color = RED
else:
print("Invalid skin choice. Please choose 1, 2, or 3.")
pygame.quit()
quit()
# Display menu until the player chooses a skin
skin_choice = None
while not skin_choice:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
skin_choice = '1'
elif event.key == pygame.K_2:
skin_choice = '2'
elif event.key == pygame.K_3:
skin_choice = '3'
screen.fill(BLACK)
display_menu()
pygame.display.update()
clock.tick(15)
# Set snake color based on skin choice
set_snake_color(skin_choice)
# Main game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and snake_dx != block_size:
snake_dx = -block_size
snake_dy = 0
elif event.key == pygame.K_RIGHT and snake_dx != -block_size:
snake_dx = block_size
snake_dy = 0
elif event.key == pygame.K_UP and snake_dy != block_size:
snake_dx = 0
snake_dy = -block_size
elif event.key == pygame.K_DOWN and snake_dy != -block_size:
snake_dx = 0
snake_dy = block_size
elif event.key in [pygame.K_1, pygame.K_2, pygame.K_3]: # Validate skin choice
skin_choice = chr(event.key)
set_snake_color(skin_choice)
# Move the snake
snake_x += snake_dx
snake_y += snake_dy
# Check for collision with the walls
if snake_x >= screen_width:
snake_x = 0
elif snake_x < 0:
snake_x = screen_width - block_size
elif snake_y >= screen_height:
snake_y = 0
elif snake_y < 0:
snake_y = screen_height - block_size
# Check for collision with food
if snake_x == food_x and snake_y == food_y:
score += 10
snake_length += 1
food_x = round(random.randrange(0, screen_width - food_size) / 20) * 20
food_y = round(random.randrange(0, screen_height - food_size) / 20) * 20
# Update the snake block
snake_head = [snake_x, snake_y]
snake_block.append(snake_head)
if len(snake_block) > snake_length:
del snake_block[0]
# Check for collision with own tail
for block in snake_block[:-1]:
if block == snake_head:
running = False
# Clear the screen
screen.fill(BLACK)
# Draw the snake
draw_snake(snake_block)
# Draw the food
pygame.draw.rect(screen, RED, [food_x, food_y, food_size, food_size])
# Display the score
display_score(score)
# Update the display
pygame.display.flip()
# Cap the frame rate
clock.tick(snake_speed)
# Quit Pygame
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment