Пожалуйста, помогите на Python писать игру змейку за 1 час
Здравствуйте! Пожалуйста, помогите на Python писать игру змейку за 1 час.
Только авторизированные пользователи могут оставлять свои ответы
Дата
Популярность
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
import pygame
import time
import random
# Инициализация pygame
pygame.init()
# Задаем размеры окна
width, height = 800, 600
cell_size = 20
# Цвета
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# Создаем окно
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Змейка")
# Часы для контроля FPS
clock = pygame.time.Clock()
fps = 15
# Шрифт для текста
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def your_score(score):
value = score_font.render("Очки: " + str(score), True, blue)
window.blit(value, [0, 0])
def our_snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(window, green, [x[0], x[1], block_size, block_size])
def message(msg, color):
mesg = font_style.render(msg, True, color)
window.blit(mesg, [width / 6, height / 3])
def game_loop():
game_over = False
game_close = False
x1, y1 = width / 2, height / 2
x1_change, y1_change = 0, 0
snake_list = []
length_of_snake = 1
foodx = round(random.randrange(0, width - cell_size) / cell_size) * cell_size
foody = round(random.randrange(0, height - cell_size) / cell_size) * cell_size
while not game_over:
while game_close:
window.fill(black)
message("Вы проиграли! Нажмите Q для выхода или C для продолжения", 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 = -cell_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = cell_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -cell_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = cell_size
x1_change = 0
if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
window.fill(black)
pygame.draw.rect(window, red, [foodx, foody, cell_size, cell_size])
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(cell_size, snake_list)
your_score(length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, width - cell_size) / cell_size) * cell_size
foody = round(random.randrange(0, height - cell_size) / cell_size) * cell_size
length_of_snake += 1
clock.tick(fps)
pygame.quit()
quit()
game_loop()
Забиваешь в инете и копируешь код