Advertisement
OtsoSilver

Untitled

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