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

Popular posts from this blog

Fisayo Fosudo: The YouTuber Who Masters Visual Storytelling

  Fisayo Fosudo is a Nigerian content creator who has made a name for himself on YouTube by mastering the art of visual storytelling. With over 200,000 subscribers and millions of views, Fisayo has become a popular figure in the Nigerian online community. Fisayo's YouTube channel features a variety of content, including tech reviews, travel vlogs, and short films. But what sets him apart is his unique approach to storytelling. He uses a combination of stunning visuals and engaging narration to tell compelling stories that captivate his audience. Fisayo's journey to becoming a successful YouTuber was not without its challenges. He started his channel in 2015 while still in university, but it took him several years to gain traction. He had to learn how to create high-quality videos on a tight budget, and he had to figure out how to promote his content to a wider audience. But Fisayo persevered, and his hard work paid off. Today, he is not only a successful YouTuber but also a sou...