Загрузка данных


# pgzero
import pgzrun
import random

# Настройки игрового окна
cell = Actor('border')     # Клетка-стена
cell1 = Actor('floor')     # Пол
cell2 = Actor("crack")     # Трещина
cell3 = Actor("bones")     # Кости

size_w = 9
size_h = 10
WIDTH = cell.width * size_w
HEIGHT = cell.height * size_h
TITLE = "The Wellkid Explorer"

# Карта уровня
my_map = [
    [0,0,0,0,0,0,0,0,0],
    [0,1,1,1,1,1,1,1,0],
    [0,1,2,1,3,1,2,1,0],
    [0,1,1,3,1,1,1,1,0],
    [0,3,1,1,1,2,1,3,0],
    [0,1,1,1,3,1,1,1,0],
    [0,1,2,1,1,1,3,1,0],
    [0,1,1,1,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0],
    [-1,-1,-1,-1,-1,-1,-1,-1,-1]
]

# Персонаж
char = Actor('stand')
char.left = cell.width
char.top = cell.height
char.health = 100
char.attack = 5

# Враги
enemies = []
for i in range(5):
    x = random.randint(1, 7) * cell.width
    y = random.randint(1, 7) * cell.height
    enemy = Actor("enemy", topleft=(x, y))
    enemy.health = random.randint(10, 20)
    enemy.attack = random.randint(5, 10)
    enemies.append(enemy)

# Режим игры и результат
mode = "game"  # "game" или "end"
win = 0        # 1 — победа, -1 — поражение

# Отрисовка карты
def map_draw():
    for i in range(len(my_map)):
        for j in range(len(my_map[0])):
            x = cell.width * j
            y = cell.height * i
            if my_map[i][j] == 0:
                cell.left, cell.top = x, y
                cell.draw()
            elif my_map[i][j] == 1:
                cell1.left, cell1.top = x, y
                cell1.draw()
            elif my_map[i][j] == 2:
                cell2.left, cell2.top = x, y
                cell2.draw()
            elif my_map[i][j] == 3:
                cell3.left, cell3.top = x, y
                cell3.draw()

# Отрисовка экрана
def draw():
    if mode == "game":
        screen.fill("#2f3542")
        map_draw()
        char.draw()
        for enemy in enemies:
            enemy.draw()
        screen.draw.text(f"HP: {char.health}", (10, 10), fontsize=24, color="white")
        screen.draw.text(f"AP: {char.attack}", (10, 40), fontsize=24, color="white")
    elif mode == "end":
        screen.fill("black")
        if win == 1:
            screen.draw.text("Победа!", center=(WIDTH/2, HEIGHT/2), fontsize=60, color="green")
        else:
            screen.draw.text("Поражение!", center=(WIDTH/2, HEIGHT/2), fontsize=60, color="red")

# Управление
def on_key_down(key):
    if mode != "game":
        return

    old_x = char.x
    old_y = char.y

    if keyboard.right and char.x + cell.width < WIDTH - cell.width:
        char.x += cell.width
        char.image = 'stand'
    elif keyboard.left and char.x - cell.width > cell.width:
        char.x -= cell.width
        char.image = 'left'
    elif keyboard.down and char.y + cell.height < HEIGHT - cell.height * 2:
        char.y += cell.height
    elif keyboard.up and char.y - cell.height > cell.height:
        char.y -= cell.height

    # ⚔Столкновение
    enemy_index = char.collidelist(enemies)
    if enemy_index != -1:
        char.x = old_x
        char.y = old_y
        enemy = enemies[enemy_index]
        enemy.health -= char.attack
        char.health -= enemy.attack
        if enemy.health <= 0:
            enemies.pop(enemy_index)

# Победа / Поражение
def victory():
    global mode, win
    if char.health <= 0:
        mode = "end"
        win = -1
    elif len(enemies) == 0:
        mode = "end"
        win = 1

# Обновление состояния
def update():
    if mode == "game":
        victory()

# Запуск
pgzrun.go()