Загрузка данных
import os
import random
import pygame
import pgzrun
from pgzero.builtins import Actor
WIDTH = 800
HEIGHT = 600
GRAVITY = 0.8
JUMP_STRENGTH = -12
PLAYER_SPEED = 5
def create_dummy_images():
os.makedirs("images", exist_ok=True)
player_path = os.path.join("images", "player.png")
platform_path = os.path.join("images", "platform.png")
if not os.path.exists(player_path):
surf_player = pygame.Surface((40, 40), pygame.SRCALPHA)
pygame.draw.circle(surf_player, (255, 0, 0), (20, 20), 18)
pygame.draw.circle(surf_player, (255, 255, 0), (20, 20), 12)
pygame.image.save(surf_player, player_path)
if not os.path.exists(platform_path):
surf_platform = pygame.Surface((120, 20), pygame.SRCALPHA)
surf_platform.fill((139, 69, 19))
pygame.draw.rect(surf_platform, (101, 67, 33), surf_platform.get_rect(), 2)
pygame.image.save(surf_platform, platform_path)
pygame.init()
create_dummy_images()
def make_platform(x, y):
platform = Actor("platform", (x, y))
platform.scored = False
return platform
def generate_level(level_num):
new_platforms = [make_platform(WIDTH // 2, HEIGHT - 50)]
last_x = WIDTH // 2
for _ in range(8 + level_num):
last_x += random.randint(150, 300)
new_y = HEIGHT - 100 + random.randint(-120, 80)
new_y = max(80, min(HEIGHT - 50, new_y))
new_platforms.append(make_platform(last_x, new_y))
return new_platforms
platforms = generate_level(1)
player = Actor("player", (0, 0))
player.vy = 0
player.on_ground = True
start_platform = platforms[0]
player.midbottom = (start_platform.x, start_platform.top)
score = 0
level = 1
def game_over():
print(f"Игра окончена, счет: {score}")
raise SystemExit
def update():
global score, level, platforms
if keyboard.left:
player.x -= PLAYER_SPEED
if keyboard.right:
player.x += PLAYER_SPEED
if keyboard.space and player.on_ground:
player.vy = JUMP_STRENGTH
player.on_ground = False
player.vy += GRAVITY
player.y += player.vy
player.on_ground = False
for platform in platforms:
if player.colliderect(platform) and player.vy >= 0:
if player.bottom >= platform.top and player.bottom - player.vy <= platform.top + 10:
player.bottom = platform.top
player.vy = 0
player.on_ground = True
if player.y > HEIGHT + 100:
game_over()
threshold = WIDTH * 0.7
if player.x > threshold:
shift = player.x - threshold
player.x -= shift
for platform in platforms:
platform.x -= shift
platforms = [p for p in platforms if p.right > -200]
if not platforms:
platforms = generate_level(level)
while len(platforms) < 10:
last_platform = max(platforms, key=lambda p: p.x)
new_x = last_platform.x + random.randint(200, 400)
new_y = last_platform.y + random.randint(-100, 100)
new_y = max(80, min(HEIGHT - 50, new_y))
platforms.append(make_platform(new_x, new_y))
for platform in platforms:
if platform.right < player.left - 50 and not platform.scored:
score += 10
platform.scored = True
if score >= level * 100:
level += 1
platforms = generate_level(level)
start_platform = platforms[0]
player.midbottom = (start_platform.x, start_platform.top)
player.vy = 0
player.on_ground = True
def draw():
screen.clear()
screen.fill((135, 206, 235))
for platform in platforms:
platform.draw()
player.draw()
screen.draw.text(f"Счет: {score}", (10, 10), fontsize=30, color="white")
screen.draw.text(f"Уровень: {level}", (10, 50), fontsize=30, color="white")
screen.draw.text(
"стрелочки для движения, пробел для прыжка",
(WIDTH // 2 - 150, HEIGHT - 30),
fontsize=20,
color="white"
)
pgzrun.go()