Facebook
From asdasd, 7 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 176
  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.  
  35. char.health = 100
  36. char.attack = 5
  37.  
  38. char.apodo = "memo"
  39.  
  40. # Generando enemigos
  41. enemies = []
  42. for i in range(5):
  43.     x = random.randint(1, 7) * cell.width
  44.     y = random.randint(1, 7) * cell.height
  45.     enemy = Actor("enemy", topleft = (x, y))
  46.    
  47.     enemy.health = random.randint(10, 20)
  48.     enemy.attack = random.randint(5, 10)
  49.     enemy.bonus = random.randint(0, 2)
  50.    
  51.     enemies.append(enemy)
  52.  
  53. # Bonificaciones
  54. hearts = []
  55. swords = []
  56.  
  57. def map_draw():
  58.     for i in range(len(my_map)):
  59.         for j in range(len(my_map[0])):
  60.             if my_map[i][j] == 0:
  61.                 cell.left = cell.width*j
  62.                 cell.top = cell.height*i
  63.                 cell.draw()
  64.             elif my_map[i][j] == 1:
  65.                 cell1.left = cell.width*j
  66.                 cell1.top = cell.height*i
  67.                 cell1.draw()
  68.             elif my_map[i][j] == 2:
  69.                 cell2.left = cell.width*j
  70.                 cell2.top = cell.height*i
  71.                 cell2.draw()  
  72.             elif my_map[i][j] == 3:
  73.                 cell3.left = cell.width*j
  74.                 cell3.top = cell.height*i
  75.                 cell3.draw()
  76.  
  77. def draw():
  78.     if mode == "game":
  79.         screen.fill("#2f3542")
  80.         map_draw()
  81.         char.draw()
  82.         screen.draw.text("HP:", center=(25, 475), color = 'white', fontsize = 20)
  83.         screen.draw.text(char.health, center=(75, 475), color = 'white', fontsize = 20)
  84.         screen.draw.text("AP:", center=(375, 475), color = 'white', fontsize = 20)
  85.         screen.draw.text(char.attack, center=(425, 475), color = 'white', fontsize = 20)
  86.        
  87.         for i in range(len(enemies)):
  88.             enemies[i].draw()
  89.            
  90.         for i in range(len(hearts)):
  91.             hearts[i].draw()
  92.            
  93.         for i in range(len(swords)):
  94.             swords[i].draw()
  95.            
  96.     elif mode == "end":
  97.         screen.fill("#2f3542")
  98.         if win == 1:
  99.             screen.draw.text("¡Ganaste!", center=(WIDTH/2, HEIGHT/2), color = 'white', fontsize = 46)
  100.         else:
  101.             screen.draw.text("¡Perdiste!", center=(WIDTH/2, HEIGHT/2), color = 'white', fontsize = 46)
  102.    
  103. def on_key_down(key):
  104.    
  105.     old_x = char.x
  106.     old_y = char.y
  107.    
  108.    
  109.     if keyboard.right and char.x + cell.width < WIDTH - cell.width:
  110.         char.x += cell.width
  111.         char.image = 'stand'
  112.     elif keyboard.left and char.x - cell.width > cell.width:
  113.         char.x -= cell.width
  114.         char.image = 'left'
  115.     elif keyboard.down and char.y + cell.height < HEIGHT - cell.height*2:
  116.         char.y += cell.height
  117.     elif keyboard.up and char.y - cell.height > cell.height:
  118.         char.y -= cell.height
  119.    
  120.     enemy_index = char.collidelist(enemies)
  121.    
  122.     if enemy_index != -1:
  123.         char.x = old_x
  124.         char.y = old_y
  125.        
  126.    
  127.         enemy = enemies[enemy_index]
  128.        
  129.         enemy.health -= char.attack
  130.         char.health -= enemy.attack
  131.        
  132.         if enemy.health <= 0:
  133.             enemies.pop(enemy_index)
  134.            
  135.              if enemy.b 1:
  136.                 heart = Actor('heart')
  137.                 heart.pos = enemy.pos
  138.                 hearts.append(heart)
  139.                
  140.              elif enemy.b 2:
  141.                 sword = Actor('sword')
  142.                 sword.pos = enemy.pos
  143.                 swords.append(sword)
  144.                
  145.  
  146. def victory():
  147.     global mode, win
  148.     if enemies == [] and char.health > 0:
  149.         mode = "end"
  150.         win = 1
  151.     elif char.health <= 0:
  152.         mode = "end"
  153.         win = -1
  154.        
  155. def update(dt):
  156.     victory()
  157.     for i in range(len(hearts)):
  158.         if char.colliderect(hearts[i]):
  159.             char.health += 5
  160.             hearts.pop(i)
  161.             break
  162.        
  163.     for i in range(len(swords)):
  164.         if char.colliderect(swords[i]):
  165.             char.attack += 5
  166.             swords.pop(i)
  167.             break