Skip to main content

The Future of Transportation: Self-Driving Cars Revolutionizing the Way We Travel

Snake Game Python Code




 Snake game is a classic game that has been enjoyed by generations. It's a simple game that involves controlling a snake to eat food while avoiding obstacles such as walls and the snake's own body. In this article, we will take a closer look at the Python code that can be used to create a snake game.


The Code


The snake game in Python can be created using the Pygame library, which is a set of Python modules designed for game development. Here's the basic code for creating a snake game in Python:


import pygame

import random


pygame.init()


# Create game window

window_width = 600

window_height = 400

window = pygame.display.set_mode((window_width, window_height))

pygame.display.set_caption("Snake Game")


# Define colors

black = (0, 0, 0)

white = (255, 255, 255)

green = (0, 255, 0)

red = (255, 0, 0)


# Set up snake

snake_block = 10

snake_speed = 15


font_style = pygame.font.SysFont(None, 30)


def message(msg, color):

    message = font_style.render(msg, True, color)

    window.blit(message, [window_width/6, window_height/3])


def gameLoop():

    game_over = False

    game_close = False


    # Set initial snake position

    x1 = window_width / 2

    y1 = window_height / 2

    x1_change = 0

    y1_change = 0


    # Generate food location

    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 == True:

            window.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()


        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


        # Check for boundaries and collisions with snake body

        if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:

            game_close = True

        x1 += x1_change

        y1 += y1_change

        window.fill(black)

        pygame.draw.rect(window, green, [foodx, foody, snake_block, snake_block])

        pygame.draw.rect(window, white, [x1, y1, snake_block, snake_block])

        pygame.display.update()


        # Check if snake has eaten food

        if x1 == foodx and y1 == foody:

            print("Yummy!!")

        pygame.display.update()


        # Set snake speed


Comments