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


import pgzrun
import random
import time


cell0 = Actor("border.png")
cell1 = Actor("floor.png")
cell2 = Actor("crack.png")
cell3 = Actor("bones.png")
cell4 = Actor("lava.png")
cell5 = Actor("poison.png")
cell6 = Actor("thorns.png")
cell6.type = "danger2"
cell4.type = "danger1"
cell5.type = "health1"
cell0.type = ""
cell1.type = ""
cell2.type = ""
cell3.type = ""




size_w = 9
size_h = 9

WIDTH = cell0.width * size_w
HEIGHT = cell0.height * size_h
TITLE = "ROGALIK"

begin = 0

cells = []

game_active = True

level1 = [
    [0,0,0,0,0,0,0,0,0],
    [0,1,1,1,1,5,1,1,0],
    [0,1,2,1,3,1,2,1,0],
    [0,1,5,3,1,1,1,1,0],
    [0,3,1,1,1,2,1,3,0],
    [0,1,1,1,3,1,6,1,0],
    [0,1,2,1,1,1,3,1,0],
    [0,1,4,1,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0],
    [-1,-1,-1,-1,-1,-1,-1,-1,-1]
]

level2 = [
    [0,0,0,0,0,0,0,0,0],
    [0,2,4,1,2,5,2,2,0],
    [0,3,2,1,2,4,2,3,0],
    [0,6,1,3,2,1,2,5,0],
    [0,3,4,2,6,2,1,3,0],
    [0,1,1,2,3,3,1,2,0],
    [0,1,5,2,3,1,3,2,0],
    [0,1,2,2,1,3,5,2,0],
    [0,0,0,0,0,0,0,0,0],
    [-1,-1,-1,-1,-1,-1,-1,-1,-1]
]

level3 = [
    [0,0,0,0,0,0,0,0,0],
    [0,4,1,3,2,5,1,2,0],
    [0,3,2,2,6,1,1,4,0],
    [0,3,2,4,2,3,2,1,0],
    [0,3,5,2,2,5,2,3,0],
    [0,1,1,3,3,2,4,1,0],
    [0,1,3,5,1,1,1,2,0],
    [0,1,4,3,6,3,5,2,0],
    [0,0,0,0,0,0,0,0,0],
    [-1,-1,-1,-1,-1,-1,-1,-1,-1]
]

levels = [level1, level2, level3]
level = 0
my_map = levels[level]


enemies = []
for i in range(5):
    x = random.randint(2, 7) * cell0.width
    y = random.randint(2, 7) * cell0.height
    enemy = Actor("enemy", topleft = (x, y))
    enemy.health = random.randint(10,15)
    enemy.attack = random.randint(3, 7)
    enemies.append(enemy)



char = Actor("stand.png")
char.x = cell0.width + cell0.width // 2
char.y = cell0.height + cell0.height // 2
char.health = 200
char.attack = 5

mode = "game"
win = 0




def reset_game():
    global win, mode , char , enemies, enemy, game_active, my_map

    my_map = levels[level]

    enemies = []
    char.health = 200 + level * 20
    char.attack = 5 + level * 5
    char.x = cell0.width + cell0.width // 2
    char.y = cell0.height + cell0.height // 2

    mode = "game"
    win = 0

    enemies = []
    for i in range(5 + level * 3):
        x = random.randint(2, 7) * cell0.width
        y = random.randint(2, 7) * cell0.height
        enemy = Actor("enemy", topleft=(x, y))
        enemy.health = random.randint(10 + level * 2, 15 + level * 3)
        enemy.attack = random.randint(3 + level * 2, 7 + level * 3)
        enemies.append(enemy)
    game_active = True


if char.health <= 0:
    game_active = False

def on_key_down(key):
    global mode, win, begin
    begin = time.time()
    old_x = char.x
    old_y = char.y
    if keyboard.right and char.x + cell0.width < WIDTH - cell0.width:
        char.x += cell0.width  # перемещаем персонажа вправо на ширину одной клетки
        char.image = "right"  # меняем картинку персонажа на обычную (вправо)
    elif keyboard.left  and char.x - cell0.width > cell0.width:
        char.x -= cell0.width
        char.image = "left"
    elif keyboard.down and char.y + cell0.height < HEIGHT - cell0.height:
        char.y += cell0.height
        char.image = "stand"
    elif keyboard.up  and char.y - cell0.height > cell0.height:
        char.y -= cell0.height
        char.image = "back"

    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)
            if len(enemies) == 0:
                mode = "end"
                win = 1
        if char.health <= 0:
            mode = "end"
            win = 0

def draw():

    if mode == "game":
        for i in range(len(my_map)):
            for j in range(len(my_map[0])):
                x = j * cell0.width
                y = i * cell0.height

                if my_map[i][j] == 0:
                    cell0.topleft = (x, y)
                    cell0.draw()
                    cells.append(cell0)

                elif my_map[i][j] == 1:
                    cell1.topleft = (x, y)
                    cell1.draw()
                    cells.append(cell1)

                elif my_map[i][j] == 2:
                    cell2.topleft = (x, y)
                    cell2.draw()
                    cells.append(cell2)

                elif my_map[i][j] == 3:
                    cell3.topleft = (x, y)
                    cell3.draw()
                    cells.append(cell3)

                elif my_map[i][j] == 4:
                    cell4.topleft = (x, y)
                    cell4.draw()
                    cells.append(cell4)

                elif my_map[i][j] == 5:
                    cell5.topleft = (x, y)
                    cell5.draw()
                    cells.append(cell5)

                elif my_map[i][j] == 6:
                    cell6.topleft = (x, y)
                    cell6.draw()
                    cells.append(cell6)

        char.draw()
        for enemy in enemies:
            enemy.draw()


        screen.draw.text(f"Здоровье: {char.health}", (10, 10), fontsize = 24, color = "green")
        screen.draw.text(f"Атака: {char.attack}", (10, 40), fontsize=24, color="red")
    elif mode == "end":
        screen.fill("black")

        if char.health <= 0 :
            screen.draw.text(
                "GAME OVER",
                center=(WIDTH / 2, HEIGHT / 4),
                fontsize=60,
                color="red"
            )
            screen.draw.text(
                "Press SPACE to restart",
                center=(WIDTH / 2, HEIGHT / 4 + 60),
                fontsize=30,
                color="white"
            )


        if win == 1:
            screen.draw.text(f"Победа ", center = (WIDTH // 2, HEIGHT // 2), fontsize = 60, color = "green")
        if win == 0:
            screen.draw.text(f"Порожение", center = (WIDTH // 2, HEIGHT // 2), fontsize = 60, color = "red")

def update():
    global game_active, mode, win, level, begin, char

    for cell in cells:
        if cell.type == "danger1" and char.colliderect(cell):
            if time.time() - begin > 2:
                char.health -= 3
                begin = time.time()

        if cell.type == "danger2" and char.colliderect(cell):
            if time.time() - begin > 1:
                char.health -= 5
                begin = time.time()

        if cell.type == "health1" and char.colliderect(cell):
            if time.time() - begin > 2:
                char.health += 3
                begin = time.time()


    if char.health <= 0 :
        mode = "end"
        win -= 1
        if keyboard.space:
            reset_game()
    elif len(enemies) == 0:
        level += 1
        if level < len(levels):
            reset_game()
        else:
            mode = "end"
            win = 1





pgzrun.go()