Advertisement
OtsoSilver

Untitled

Sep 25th, 2021
1,481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. #pgzero
  2. import random
  3.  
  4. # Игровое окно
  5. cell = Actor('border')
  6. cell1 = Actor('floor')
  7. cell2 = Actor("crack")
  8. cell3 = Actor("bones")
  9. size_w = 9 # Ширина поля в клетках
  10. size_h = 10 # Высота поля в клетках
  11. WIDTH = cell.width * size_w
  12. HEIGHT = cell.height * size_h
  13.  
  14. TITLE = "Подземелья" # Заголовок окна игры
  15. FPS = 30 # Количество кадров в секунду
  16. my_map = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  17.           [0, 1, 1, 1, 1, 1, 1, 1, 0],
  18.           [0, 1, 1, 2, 1, 3, 1, 1, 0],
  19.           [0, 1, 1, 1, 2, 1, 1, 1, 0],
  20.           [0, 1, 3, 2, 1, 1, 3, 1, 0],
  21.           [0, 1, 1, 1, 1, 3, 1, 1, 0],
  22.           [0, 1, 1, 3, 1, 1, 2, 1, 0],
  23.           [0, 1, 1, 1, 1, 1, 1, 1, 0],
  24.           [0, 0, 0, 0, 0, 0, 0, 0, 0],
  25.           [-1, -1, -1, -1, -1, -1, -1, -1, -1]] # Строка с атакой и здоровьем
  26.  
  27. # Главный герой
  28. char = Actor('stand')
  29. char.top = cell.height
  30. char.left = cell.width
  31. char.health = 100
  32. char.attack = 5
  33.  
  34. # Генерация врагов
  35. enemies = []
  36. for i in range(5):
  37.     x = random.randint(1, 7) * cell.width
  38.     y = random.randint(1, 7) * cell.height
  39.     enemy = Actor("enemy", topleft = (x, y))
  40.     enemy.health = random.randint(10, 20)
  41.     enemy.attack = random.randint(5, 10)
  42.     enemy.bonus = random.randint(0, 2)
  43.     enemies.append(enemy)
  44.  
  45. # Бонусы
  46. hearts = []
  47. swords = []
  48.  
  49. def map_draw():
  50.     for i in range(len(my_map)):
  51.         for j in range(len(my_map[0])):
  52.             if my_map[i][j] == 0:
  53.                 cell.left = cell.width*j
  54.                 cell.top = cell.height*i
  55.                 cell.draw()
  56.             elif my_map[i][j] == 1:
  57.                 cell1.left = cell.width*j
  58.                 cell1.top = cell.height*i
  59.                 cell1.draw()
  60.             elif my_map[i][j] == 2:
  61.                 cell2.left = cell.width*j
  62.                 cell2.top = cell.height*i
  63.                 cell2.draw()  
  64.             elif my_map[i][j] == 3:
  65.                 cell3.left = cell.width*j
  66.                 cell3.top = cell.height*i
  67.                 cell3.draw()
  68.  
  69. def draw():
  70.     screen.fill("#2f3542")
  71.     map_draw()
  72.     char.draw()
  73.     screen.draw.text("HP:", center=(25, 475), color = 'white', fontsize = 20)
  74.     screen.draw.text(char.health, center=(75, 475), color = 'white', fontsize = 20)
  75.     screen.draw.text("AP:", center=(375, 475), color = 'white', fontsize = 20)
  76.     screen.draw.text(char.attack, center=(425, 475), color = 'white', fontsize = 20)
  77.     for i in range(len(enemies)):
  78.         enemies[i].draw()
  79.     for i in range(len(hearts)):
  80.         hearts[i].draw()
  81.     for i in range(len(swords)):
  82.         swords[i].draw()
  83.    
  84. def on_key_down(key):
  85.     old_x = char.x
  86.     old_y = char.y
  87.     if keyboard.right and char.x + cell.width < WIDTH - cell.width:
  88.         char.x += cell.width
  89.         char.image = 'stand'
  90.     elif keyboard.left and char.x - cell.width > cell.width:
  91.         char.x -= cell.width
  92.         char.image = 'left'
  93.     elif keyboard.down and char.y + cell.height < HEIGHT - cell.height*2:
  94.         char.y += cell.height
  95.     elif keyboard.up and char.y - cell.height > cell.height:
  96.         char.y -= cell.height
  97.    
  98.     enemy_index = char.collidelist(enemies)
  99.     if enemy_index != -1:
  100.         char.x = old_x
  101.         char.y = old_y
  102.         enemy = enemies[enemy_index]
  103.         enemy.health -= char.attack
  104.         char.health -= enemy.attack
  105.         if enemy.health <= 0:
  106.             if enemy.bonus == 1:
  107.                 heart = Actor('heart')
  108.                 heart.pos = enemy.pos
  109.                 hearts.append(heart)
  110.             elif enemy.bonus == 2:
  111.                 sword = Actor('sword')
  112.                 sword.pos = enemy.pos
  113.                 swords.append(sword)
  114.             enemies.pop(enemy_index)
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement