import pygame
import random
# Initialize Pygame
pygame.init()
# Set the screen dimensions
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# Set the title of the game window
pygame.display.set_caption("Snake Game")
# Set the colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# Set the font
font = pygame.font.SysFont(None, 25)
# Set the block size and speed
block_size = 10
speed = 15
# Define the snake function
def snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, green, [x[0], x[1], block_size, block_size])
# Define the message function
def message(msg, color):
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [screen_width/6, screen_height/3])
# Define the game loop
def gameLoop():
game_over = False
game_close = False
# Set the starting position of the snake
x1 = screen_width / 2
y1 = screen_height / 2
# Set the change in position of the snake
x1_change = 0
y1_change = 0
# Set the initial length of the snake
snake_List = []
Length_of_snake = 1
# Set the initial position of the food
foodx = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
# Start the game loop
while not game_over:
# Check for events
while game_close == True:
screen.fill(white)
message("You Lost! Press Q-Quit or C-Play Again", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
# Move the snake
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:
x1_change = -block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = block_size
x1_change = 0
# Check for boundaries
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_close = True
# Update the position of the snake
x1 += x1_change
y1 += y1_change
# Fill the screen with white color
screen.fill(white)
# Draw the food
pygame.draw.rect(screen, red, [foodx, foody
, block_size, block_size])
# Add the position of the head of the snake to the snake list
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
# Remove the last position of the snake if the length of the snake exceeds Length_of_snake
if len(snake_List) > Length_of_snake:
del snake_List[0]
# Check if the snake hits itself
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
# Draw the snake
snake(block_size, snake_List)
# Display the score
pygame.display.update()
# Check if the snake hits the food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
Length_of_snake += 1
# Set the speed of the snake
clock = pygame.time.Clock()
clock.tick(speed)
# Quit Pygame
pygame.quit()
# Quit Python
quit()
This code will create a window for the Snake game, where the player will control a snake that must eat food to grow. The player must avoid hitting the walls or the snake's own body. The game ends when the player hits a wall or the snake's body. The score is displayed on the screen, and the player can choose to play again or quit.
The Best Gaming Blogger Template
إرسال تعليق