Untitled

 avatar
unknown
plain_text
5 days ago
3.8 kB
2
Indexable
Here's a simple Python implementation of the classic Snake game using the pygame library. If you don't have it installed, you can install it using pip install pygame.

import pygame
import time
import random

# Initialize pygame
pygame.init()

# Set game window dimensions
window_width = 600
window_height = 400
game_window = pygame.display.set_mode((window_width, window_height))

# Set colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# Set clock and font
clock = pygame.time.Clock()
font_style = pygame.font.SysFont("bahnschrift", 25)

# Game over message
def message(msg, color):
    mesg = font_style.render(msg, True, color)
    game_window.blit(mesg, [window_width / 6, window_height / 3])

# Main game function
def gameLoop():
    game_over = False
    game_close = False

    # Snake starting position
    x1 = window_width / 2
    y1 = window_height / 2
    x1_change = 0
    y1_change = 0

    # Snake body
    snake_block = 10
    snake_speed = 15
    snake_body = []

    # Food position
    foodx = round(random.randrange(0, window_width - snake_block) / 10.0) * 10.0
    foody = round(random.randrange(0, window_height - snake_block) / 10.0) * 10.0

    while not game_over:

        while game_close:
            game_window.fill(blue)
            message("You Lost! Press C-Play Again or Q-Quit", 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()

        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 = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0

        if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        game_window.fill(blue)
        pygame.draw.rect(game_window, green, [foodx, foody, snake_block, snake_block])

        # Snake body mechanics
        snake_head = []
        snake_head.append(x1)
        snake_head.append(y1)
        snake_body.append(snake_head)

        if len(snake_body) > 1:
            del snake_body[0]

        for block in snake_body[:-1]:
            if block == snake_head:
                game_close = True

        for segment in snake_body:
            pygame.draw.rect(game_window, black, [segment[0], segment[1], snake_block, snake_block])

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, window_width - snake_block) / 10.0) * 10.0
            foody = round(random.randrange(0, window_height - snake_block) / 10.0) * 10.0

        clock.tick(snake_speed)

    pygame.quit()
    quit()

# Start the game
gameLoop()

Steps to run the game:

1. Install pygame by running pip install pygame.


2. Copy and paste the code into a Python file (e.g., snake_game.py).


3. Run the file, and the game window should open.


4. Use the arrow keys to control the snake.


5. The game ends if the snake runs into the wall or itself.



Let me know if you need any explanations or modifications!

Leave a Comment