create snake game in python

Here's a basic implementation of the classic Snake game using the Pygame module in Python:

main.py
import pygame
import random

pygame.init()

# Setting up window size
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Snake Game")

# Setting up the colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

# Setting up the snake properties
snake_x = 250
snake_y = 250
snake_width = 10
snake_height = 10
snake_speed = 10
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_y)

# Setting up the food properties
food_x = round(random.randrange(0, win_width - snake_width) / 10.0) * 10.0
food_y = round(random.randrange(0, win_height - snake_height) / 10.0) * 10.0
food_width = 10
food_height = 10

# Setting up game clock and font
clock = pygame.time.Clock()
font_style = pygame.font.SysFont(None, 30)

# Function to display score
def show_score(score):
    score_text = font_style.render("Score: " + str(score), True, white)
    win.blit(score_text, [0, 0])

# Main game loop
def gameLoop():
    game_over = False
    game_close = False
    x1_change = 0
    y1_change = 0
    score = 0

    # Keep track of the snake's body as a list of points
    snake_list = []
    Length_of_snake = 1

    # Generate the snake at the start of the game
    while Length_of_snake <= 3:
        x = snake_x + Length_of_snake * 10
        snake_list.append([x, snake_y])
        Length_of_snake += 1

    while not game_over:

        while game_close == True:
            # Game over screen
            win.fill(black)
            game_over_text = font_style.render("Game Over!", True, red)
            win.blit(game_over_text, [win_width/3, win_height/3])
            show_score(score)
            pygame.display.update()

            # Event handling in game over screen
            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_r:
                        gameLoop()

        # Handling events within the game
        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_speed
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_speed
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_speed
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_speed
                    x1_change = 0

        # Check if snake hits the borders of the screen
        if snake_head[0] >= win_width or snake_head[0] < 0 or snake_head[1] >= win_height or snake_head[1] < 0:
            game_close = True

        # Update the coordinates of the snake head
        snake_head[0] += x1_change
        snake_head[1] += y1_change

        win.fill(black)

        # Draw the food block
        pygame.draw.rect(win, red, [food_x, food_y, food_width, food_height])

        # Draw the snake body
        for segment in snake_list:
            pygame.draw.rect(win, white, [segment[0], segment[1], snake_width, snake_height])

        # Add the current position of the head to the snake body
        snake_segments = []
        snake_segments.append(snake_head[0])
        snake_segments.append(snake_head[1])
        snake_list.append(snake_segments)

        # Remove the oldest coordinate if snake is greater than its length
        if len(snake_list) > Length_of_snake:
            del snake_list[0]

        # Check if snake hits the food block
        if snake_head[0] == food_x and snake_head[1] == food_y:
            food_x = round(random.randrange(0, win_width - snake_width) / 10.0) * 10.0
            food_y = round(random.randrange(0, win_height - snake_height) / 10.0) * 10.0
            Length_of_snake += 1
            score += 10

        # Refreshes the score
        show_score(score)
        pygame.display.update()

        # Check if snake hits its body
        for segment in snake_list[:-1]:
            if segment == snake_head:
                game_close = True

        # Refreshes the game
        pygame.display.update()
        clock.tick(30)

    pygame.quit()
    quit()

gameLoop()
4473 chars
148 lines

This is a basic algorithm, you can add extra features to it like adding sound or increasing difficulty levels for the player. If you want to improve or modify the code, feel free to do so.

gistlibby LogSnag