Created
December 23, 2023 12:50
-
-
Save kianurivzzz/fbad9c4b3765f20e15de807c95546169 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
import sys | |
import pygame | |
pygame.init() | |
WHITE = (255, 255, 255) | |
YELLOW = (255, 255, 102) | |
BLACK = (0, 0, 0) | |
RED = (213, 50, 80) | |
GREEN = (0, 255, 0) | |
BLUE = (50, 153, 213) | |
display_width = 800 | |
display_height = 600 | |
snake_block = 10 | |
snake_speed = 15 | |
window = pygame.display.set_mode((display_width, display_height)) | |
pygame.display.set_caption('Змейка от EasyCode') | |
clock = pygame.time.Clock() | |
font_style = pygame.font.SysFont('bahnschrift', 25) | |
score_font = pygame.font.SysFont('comicsansms', 35) | |
def get_score(score): | |
value = score_font.render('Ваш счёт: ' + str(score), True, YELLOW) | |
window.blit(value, [0, 0]) | |
def our_snake(snake_block, snake_list): | |
for x in snake_list: | |
pygame.draw.rect(window, BLACK, [x[0], x[1], snake_block, snake_block]) | |
def get_message(msg, color): | |
message = font_style.render(msg, True, color) | |
window.blit(message, [display_width / 6, display_height / 3]) | |
def game_loop(): | |
game_over = False | |
game_close = False | |
x1 = display_width / 2 | |
y1 = display_height / 2 | |
x1_change = 0 | |
y1_change = 0 | |
snake_list = [] | |
length_of_snake = 1 | |
foodx = round(random.randrange(0, display_width - snake_block) / 10.0) * 10.0 | |
foody = round(random.randrange(0, display_height - snake_block) / 10.0) * 10.0 | |
while not game_over: | |
while game_close == True: | |
window.fill(BLUE) | |
get_message('Вы проиграли! Нажмите Q для выхода или C для повторной игры', RED) | |
get_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 >= display_width or x1 < 0 or y1 >= display_height or y1 < 0: | |
game_close = True | |
x1 += x1_change | |
y1 += y1_change | |
window.fill(BLUE) | |
pygame.draw.rect(window, GREEN, [foodx, foody, snake_block, snake_block]) | |
snake_head = [] | |
snake_head.append(x1) | |
snake_head.append(y1) | |
snake_list.append(snake_head) | |
if len(snake_list) > length_of_snake: | |
del snake_list[0] | |
for x in snake_list[:-1]: | |
if x == snake_head: | |
game_close = True | |
our_snake(snake_block, snake_list) | |
get_score(length_of_snake - 1) | |
pygame.display.update() | |
if x1 == foodx and y1 == foody: | |
foodx = round(random.randrange(0, display_width - snake_block) / 10.0) * 10.0 | |
foody = round(random.randrange(0, display_height - snake_block) / 10.0) * 10.0 | |
length_of_snake += 1 | |
clock.tick(snake_speed) | |
sys.exit() | |
game_loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment