Snake Game Project in Python language

Snake Game Project – Program written in Python Language with Easy to understand Comments in Each Step.

Pic: Snake Game

import pygame
import random

# Initialize Pygame
pygame.init()

# Set up display dimensions
WIDTH, HEIGHT = 800, 600
GRID_SIZE = 20
GRID_WIDTH = WIDTH // GRID_SIZE
GRID_HEIGHT = HEIGHT // GRID_SIZE

# Set up colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# Set up directions
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)

# Snake class
class Snake:
def __init__(self):
self.body = [((WIDTH // 2), (HEIGHT // 2))] # Initialize snake’s body with starting position
self.direction = random.choice([UP, DOWN, LEFT, RIGHT]) # Randomly choose initial direction
self.score = 0 # Initialize score

def move(self):
current_head = self.body[0] # Get current head position
x, y = self.direction
# Calculate new head position based on current direction
new_head = (((current_head[0] + (x * GRID_SIZE)) % WIDTH),
((current_head[1] + (y * GRID_SIZE)) % HEIGHT))
# Check for collision with itself
if new_head in self.body[1:]:
self.score = 0 # Reset score
self.body = [((WIDTH // 2), (HEIGHT // 2))] # Reset snake’s body to initial position
else:
self.body.insert(0, new_head) # Insert new head position at the beginning of the body
if len(self.body) > self.score + 1:
self.body.pop() # Remove the last segment if snake hasn’t eaten an apple

def change_direction(self, direction):
# Prevent snake from reversing direction into itself
if (direction[0] * -1, direction[1] * -1) != self.direction:
self.direction = direction

def draw(self, surface):
# Draw each segment of the snake’s body
for segment in self.body:
pygame.draw.rect(surface, WHITE, (segment[0], segment[1], GRID_SIZE, GRID_SIZE))

# Apple class
class Apple:
def __init__(self):
self.position = (0, 0)
self.randomize_position() # Initialize apple position randomly

def randomize_position(self):
# Randomize apple position within the grid
self.position = (random.randint(0, GRID_WIDTH – 1) * GRID_SIZE,
random.randint(0, GRID_HEIGHT – 1) * GRID_SIZE)

def draw(self, surface):
# Draw the apple
pygame.draw.rect(surface, RED, (self.position[0], self.position[1], GRID_SIZE, GRID_SIZE))

# Main function
def main():
screen = pygame.display.set_mode((WIDTH, HEIGHT)) # Set up game window
pygame.display.set_caption(“Snake Game”) # Set window title
clock = pygame.time.Clock() # Create Pygame clock object
snake = Snake() # Initialize snake object
apple = Apple() # Initialize apple object
running = True

while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Change snake direction based on arrow key input
if event.key == pygame.K_UP:
snake.change_direction(UP)
elif event.key == pygame.K_DOWN:
snake.change_direction(DOWN)
elif event.key == pygame.K_LEFT:
snake.change_direction(LEFT)
elif event.key == pygame.K_RIGHT:
snake.change_direction(RIGHT)

snake.move() # Move the snake
# Check if snake eats an apple
if snake.body[0] == apple.position:
snake.score += 1 # Increment score
apple.randomize_position() # Randomize apple position

screen.fill(BLACK) # Clear the screen
snake.draw(screen) # Draw the snake
apple.draw(screen) # Draw the apple
pygame.display.update() # Update the display
clock.tick(10) # Control the game speed

pygame.quit() # Quit Pygame

if __name__ == “__main__”:
main()

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top