import pygame
import random
pygame.init()
# --- СИСТЕМА ---
# FPS
clock = pygame.time.Clock()
# Прыжок
make_jump = False
# Счетчик прыжков
jump_counter = 30
# Размеры Окна
display_width = 800
display_height = 600
# Создание Окна и изменение его имени
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Run Dino! Run!')
# Установка значка и его импорт
icon = pygame.image.load('Config/Textures/icon.png')
pygame.display.set_icon(icon)
# --- ИГРОК ---
# Размеры Игрока
user_width = 60
user_height = 100
# Координаты Игрока
user_x = display_width // 3
user_y = display_height - user_height - 100
# --- КАКТУС ---
# Размеры Кактуса
cactus_width = 20
cactus_height = 70
# Координаты Кактуса
cactus_x = display_width
cactus_y = display_height - cactus_height - 100
class Cactus:
def __init__(self, x, y, width, height, speed):
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = speed
def move(self):
if self.x >= self.width - 50:
pygame.draw.rect(display, (14, 119, 13), (self.x, self.y, self.width, self.height))
self.x -= self.speed
return True
else:
# self.x = display_width + + random.randrange(-80, 60)
return False
def return_self(self, radius):
self.x = radius
# --- СИСТЕМА x2 ---
# Цикл для игры
def run_game():
global make_jump
game = True
cactus_arr = []
create_cactus_arr(cactus_arr)
while game:
# Закрытие приложения
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# Список нажатых кнопок
keys = pygame.key.get_pressed()
# Проверка нажатия определенных клавиш
if keys[pygame.K_SPACE]:
make_jump = True
if make_jump:
jump()
# Заливка Окна белым цветом (RGB)
display.fill((255, 255, 255))
# Спавн Кактусов
draw_array(cactus_arr)
# Отрисовка "персонажа"
pygame.draw.rect(display, (158, 159, 158), (user_x, user_y, user_width, user_height))
# Обновление Окна
pygame.display.update()
clock.tick(60)
# Функция прыжка
def jump():
global user_y, make_jump, jump_counter
if jump_counter >= -30:
user_y -= jump_counter / 2.5
jump_counter -= 1
else:
jump_counter = 30
make_jump = False
# Функция создавания массива кактусов
def create_cactus_arr(array):
array.append(Cactus(display_width + 50, display_height - 170, 20, 70, 4))
array.append(Cactus(display_width + 300, display_height - 150, 30, 50, 4))
array.append(Cactus(display_width + 600, display_height - 180, 25, 80, 4))
def find_radius(array):
maximum = max(array[0].x, array[1].x, array[2].x)
if maximum < display_width:
radius = display_width
if radius - maximum < 50:
radius += 150
else:
radius = maximum
choice = random.randrange(0, 5)
if choice == 0:
radius += random.randrange(10, 15)
else:
radius += random.randrange(200, 350)
return radius
# Функция спавна кактусов
def draw_array(array):
for cactus in array:
check = cactus.move()
if not check:
radius = find_radius(array)
Cactus.return_self(radius)
# Запуск игры
run_game()