Make a game of snake

2025-05-12by@ismailchairi2004-QtgXf&@python.Model: gpt-3.5
ismailchairi2004-QtgXf

You are a Python Coder Expert,I have something to ask you.

Avatar
ismailchairi2004-QtgXf

Make a game of snake

Avatar
Avatar

Sure! Here is a simple implementation of the classic Snake game using Python and the Pygame library:

import pygame
import time
import random

pygame.init()

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
GRID_SIZE = 20
SNAKE_SPEED = 15

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Directions
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)

# Snake class
class Snake:
    def __init__(self):
        self.positions = [(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)]
        self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
        self.color = GREEN

    def move(self):
        head_x, head_y = self.positions[0]
        dir_x, dir_y = self.direction
        new_head = (head_x + dir_x * GRID_SIZE, head_y + dir_y * GRID_SIZE)
        self.positions = [new_head] + self.positions[:-1]

    def grow(self):
        tail_x, tail_y = self.positions[-1]
        dir_x, dir_y = self.direction
        new_tail = (tail_x - dir_x * GRID_SIZE, tail_y - dir_y * GRID_SIZE)
        self.positions.append(new_tail)

    def change_direction(self, direction):
        if (direction[0] * -1, direction[1] * -1) != self.direction:
            self.direction = direction

    def draw(self, surface):
        for pos in self.positions:
            rect = pygame.Rect(pos[0], pos[1], GRID_SIZE, GRID_SIZE)
            pygame.draw.rect(surface, self.color, rect)

    def check_collision(self):
        head_x, head_y = self.positions[0]
        if head_x < 0 or head_x >= SCREEN_WIDTH or head_y < 0 or head_y >= SCREEN_HEIGHT:
            return True
        if len(self.positions) != len(set(self.positions)):
            return True
        return False

    def check_food_collision(self, food_pos):
        return self.positions[0] == food_pos

# Food class
class Food:
    def __init__(self):
        self.position = (random.randint(0, SCREEN_WIDTH // GRID_SIZE - 1) * GRID_SIZE, random.randint(0, SCREEN_HEIGHT // GRID_SIZE - 1) * GRID_SIZE)
        self.color = RED

    def draw(self, surface):
        rect = pygame.Rect(self.position[0], self.position[1], GRID_SIZE, GRID_SIZE)
        pygame.draw.rect(surface, self.color, rect)

# Game setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Snake Game')

snake = Snake()
food = Food()

clock = pygame.time.Clock()

# Main game loop
running = True
while running:
    screen.fill(BLACK)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            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()
    if snake.check_collision():
        running = False

    if snake.check_food_collision(food.position):
        snake.grow()
        food = Food()

    snake.draw(screen)
    food.draw(screen)

    pygame.display.flip()
    clock.tick(SNAKE_SPEED)

pygame.quit()

You can run this code in your Python environment with the Pygame library installed to play the Snake game. Feel free to modify and enhance the game as you like!