Facebook
From asdasd, 9 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 170
  1. #pgzero
  2. import random
  3.  
  4. # Ventana de juego
  5. cell = Actor('border')
  6. cell1 = Actor('floor')
  7. cell2 = Actor("crack")
  8. cell3 = Actor("bones")
  9. size_w = 9 # Anchura del campo en celdas
  10. size_h = 10 # Altura del campo en celdas
  11. WIDTH = cell.width * size_w
  12. HEIGHT = cell.height * size_h
  13.  
  14. mode = "game"
  15. win = 0
  16.  
  17. TITLE = "Mazmorras" # Título de la ventana de juego
  18. FPS = 30 # Número de fotogramas por segundo
  19. my_map = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  20.           [0, 1, 1, 1, 1, 1, 1, 1, 0],
  21.           [0, 1, 1, 2, 1, 3, 1, 1, 0],
  22.           [0, 1, 1, 1, 2, 1, 1, 1, 0],
  23.           [0, 1, 3, 2, 1, 1, 3, 1, 0],
  24.           [0, 1, 1, 1, 1, 3, 1, 1, 0],
  25.           [0, 1, 1, 3, 1, 1, 2, 1, 0],
  26.           [0, 1, 1, 1, 1, 1, 1, 1, 0],
  27.           [0, 0, 0, 0, 0, 0, 0, 0, 0],
  28.           [-1, -1, -1, -1, -1, -1, -1, -1, -1]] # Fila de poder de ataque y salud
  29.  
  30. # Protagonista
  31. char = Actor('stand')
  32. char.top = cell.height
  33. char.left = cell.width
  34. char.health = 100
  35. char.attack = 5
  36.  
  37. # Generando enemigos
  38. enemies = []
  39. for i in range(5):
  40.     x = random.randint(1, 7) * cell.width
  41.     y = random.randint(1, 7) * cell.height
  42.     enemy = Actor("enemy", topleft = (x, y))
  43.     enemy.health = random.randint(10, 20)
  44.     enemy.attack = random.randint(5, 10)
  45.     enemy.bonus = random.randint(0, 2)
  46.     enemies.append(enemy)
  47.  
  48. # Bonificaciones
  49. hearts = []
  50. swords = []
  51.  
  52. def map_draw():
  53.     for i in range(len(my_map)):
  54.         for j in range(len(my_map[0])):
  55.             if my_map[i][j] == 0:
  56.                 cell.left = cell.width*j
  57.                 cell.top = cell.height*i
  58.                 cell.draw()
  59.             elif my_map[i][j] == 1:
  60.                 cell1.left = cell.width*j
  61.                 cell1.top = cell.height*i
  62.                 cell1.draw()
  63.             elif my_map[i][j] == 2:
  64.                 cell2.left = cell.width*j
  65.                 cell2.top = cell.height*i
  66.                 cell2.draw()  
  67.             elif my_map[i][j] == 3:
  68.                 cell3.left = cell.width*j
  69.                 cell3.top = cell.height*i
  70.                 cell3.draw()
  71.  
  72. def draw():
  73.     if mode == "game":
  74.         screen.fill("#2f3542")
  75.         map_draw()
  76.         char.draw()
  77.         screen.draw.text("HP:", center=(25, 475), color = 'white', fontsize = 20)
  78.         screen.draw.text(char.health, center=(75, 475), color = 'white', fontsize = 20)
  79.         screen.draw.text("AP:", center=(375, 475), color = 'white', fontsize = 20)
  80.         screen.draw.text(char.attack, center=(425, 475), color = 'white', fontsize = 20)
  81.        
  82.         for i in range(len(enemies)):
  83.             enemies[i].draw()
  84.            
  85.         for i in range(len(hearts)):
  86.             hearts[i].draw()
  87.            
  88.         for i in range(len(swords)):
  89.             swords[i].draw()
  90.            
  91.     elif mode == "end":
  92.         screen.fill("#2f3542")
  93.         if win == 1:
  94.             screen.draw.text("¡Ganaste!", center=(WIDTH/2, HEIGHT/2), color = 'white', fontsize = 46)
  95.         else:
  96.             screen.draw.text("¡Perdiste!", center=(WIDTH/2, HEIGHT/2), color = 'white', fontsize = 46)
  97.    
  98. def on_key_down(key):
  99.     old_x = char.x
  100.     old_y = char.y
  101.     if keyboard.right and char.x + cell.width < WIDTH - cell.width:
  102.         char.x += cell.width
  103.         char.image = 'stand'
  104.     elif keyboard.left and char.x - cell.width > cell.width:
  105.         char.x -= cell.width
  106.         char.image = 'left'
  107.     elif keyboard.down and char.y + cell.height < HEIGHT - cell.height*2:
  108.         char.y += cell.height
  109.     elif keyboard.up and char.y - cell.height > cell.height:
  110.         char.y -= cell.height
  111.    
  112.     enemy_index = char.collidelist(enemies)
  113.    
  114.     if enemy_index != -1:
  115.         char.x = old_x
  116.         char.y = old_y
  117.        
  118.         enemy = enemies[enemy_index]
  119.         enemy.health -= char.attack
  120.        
  121.         char.health -= enemy.attack
  122.        
  123.         if enemy.health <= 0:
  124.              if enemy.b 1:
  125.                 heart = Actor('heart')
  126.                 heart.pos = enemy.pos
  127.                 hearts.append(heart)
  128.                
  129.              elif enemy.b 2:
  130.                 sword = Actor('sword')
  131.                 sword.pos = enemy.pos
  132.                 swords.append(sword)
  133.                
  134.             enemies.pop(enemy_index)
  135.  
  136. def victory():
  137.     global mode, win
  138.     if enemies == [] and char.health > 0:
  139.         mode = "end"
  140.         win = 1
  141.     elif char.health <= 0:
  142.         mode = "end"
  143.         win = -1
  144.        
  145. def update(dt):
  146.     victory()
  147.     for i in range(len(hearts)):
  148.         if char.colliderect(hearts[i]):
  149.             char.health += 5
  150.             hearts.pop(i)
  151.             break
  152.        
  153.     for i in range(len(swords)):
  154.         if char.colliderect(swords[i]):
  155.             char.attack += 5
  156.             swords.pop(i)
  157.             break