Skip to content

K1m5o/python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 

Repository files navigation

import pygame import time import random

Initialize Pygame

pygame.init()

Screen Dimensions

WIDTH, HEIGHT = 800, 600

Colors

WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (213, 50, 80) GREEN = (0, 255, 0) BLUE = (50, 153, 213)

Game Variables

SNAKE_BLOCK = 20 SNAKE_SPEED = 15

Initialize screen

screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Snake Game")

Clock

clock = pygame.time.Clock()

Font styles

font_style = pygame.font.SysFont("bahnschrift", 25) score_font = pygame.font.SysFont("comicsansms", 35)

Function to display the score

def your_score(score): value = score_font.render(f"Your Score: {score}", True, WHITE) screen.blit(value, [10, 10])

Function to draw the snake

def our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block, snake_block])

Message to display when the game is over

def message(msg, color): mesg = font_style.render(msg, True, color) screen.blit(mesg, [WIDTH / 6, HEIGHT / 3])

Main Game Loop

def game_loop(): game_over = False game_close = False

x1 = WIDTH / 2
y1 = HEIGHT / 2

x1_change = 0
y1_change = 0

snake_list = []
length_of_snake = 1

# Food position
foodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 20.0) * 20.0
foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 20.0) * 20.0

while not game_over:

    while game_close:
        screen.fill(BLUE)
        message("You Lost! Press Q-Quit or C-Play Again", RED)
        your_score(length_of_snake - 1)
        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:
                    game_loop()

    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 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
        game_close = True

    x1 += x1_change
    y1 += y1_change
    screen.fill(BLACK)

    pygame.draw.rect(screen, RED, [foodx, foody, SNAKE_BLOCK, SNAKE_BLOCK])
    snake_head = [x1, y1]
    snake_list.append(snake_head)

    if len(snake_list) > length_of_snake:
        del snake_list[0]

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

    our_snake(SNAKE_BLOCK, snake_list)
    your_score(length_of_snake - 1)

    pygame.display.update()

    if x1 == foodx and y1 == foody:
        foodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 20.0) * 20.0
        foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 20.0) * 20.0
        length_of_snake += 1

    clock.tick(SNAKE_SPEED)

pygame.quit()
quit()

Run the game

game_loop()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published