Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. import pygame
  2. import random
  3. from time import sleep
  4.  
  5.  
  6. class Character:
  7. def __init__(self):
  8. self.health = 100
  9. self.health_placeholder = 100
  10. self.func_list = [self.spell_berserk, self.spell_fierce_berserk, self.spell_light_healing]
  11.  
  12. def spell_berserk(self):
  13. self.min_dmg = 18
  14. self.max_dmg = 25
  15.  
  16. return random.randint(self.min_dmg, self.max_dmg)
  17.  
  18. def spell_fierce_berserk(self):
  19. self.min_dmg = 10
  20. self.max_dmg = 35
  21.  
  22. return random.randint(self.min_dmg, self.max_dmg)
  23.  
  24. def spell_light_healing(self):
  25. self.min_heal = 12
  26. self.max_heal = 30
  27.  
  28. return random.randint(self.min_heal, self.max_heal)
  29.  
  30.  
  31. class Bot(Character):
  32. pass
  33.  
  34.  
  35. class Game:
  36. def __init__(self):
  37. pygame.init()
  38. self.stop_game = False
  39. self.bg_color = [211, 211, 211]
  40. self.display_width = 600
  41. self.display_height = 480
  42. self.score = 0
  43. self.fps_number = 15
  44. self.clock = pygame.time.Clock()
  45. self.game_display = pygame.display.set_mode((self.display_width, self.display_height))
  46. pygame.display.set_caption('Pokemon_Battle_Game')
  47. self.character = Character()
  48. self.character.health = 100
  49. self.bot = Bot()
  50. self.health_green = pygame.Color('green')
  51. self.health_yellow = pygame.Color('yellow')
  52. self.health_red = pygame.Color('red')
  53. self.border_black = pygame.Color('black')
  54. self.btn_gray = pygame.Color('gray')
  55. self.btn_dark_gray = pygame.Color('dark gray')
  56. self.btn_blue = pygame.Color('blue')
  57. self.health_color_player = None # https://stackoverflow.com/questions/40750584/this-inspection-detects-instance-attribute-definition-outside-init-method
  58. self.health_color_bot = None
  59. self.spells_button = None
  60.  
  61. def health_bar(self):
  62. # hp bar for Character
  63. if self.character.health > 75:
  64. self.health_color_player = self.health_green
  65. elif self.character.health > 49:
  66. self.health_color_player = self.health_yellow
  67. elif self.character.health <= 0: # not negative values
  68. self.character.health = 0
  69. else:
  70. self.health_color_player = self.health_red
  71.  
  72. # hp bar for Bot
  73. if self.bot.health > 75:
  74. self.health_color_bot = self.health_green
  75. elif self.bot.health > 49:
  76. self.health_color_bot = self.health_yellow
  77. elif self.bot.health <= 0: # not negative values
  78. self.bot.health = 0
  79. else:
  80. self.health_color_bot = self.health_red
  81.  
  82. def health_bar_heal(self):
  83. if self.character.health > 100:
  84. self.character.health = 100
  85.  
  86. if self.bot.health > 100:
  87. self.bot.health = 100
  88.  
  89. pygame.draw.rect(self.game_display, self.health_color_player, [50, 5, self.character.health, 25], 0) # printing health bar
  90. pygame.draw.rect(self.game_display, self.border_black, [50, 5, self.character.health_placeholder, 25], 1) # printing border
  91. pygame.draw.rect(self.game_display, self.health_color_bot, [460, 5, self.bot.health, 25], 0) # printing health for Bot
  92. pygame.draw.rect(self.game_display, self.border_black, [460, 5, self.bot.health_placeholder, 25], 1) # printing border for Bot
  93.  
  94. def display_hp(self):
  95. font = pygame.font.SysFont("arial", 14)
  96. text = font.render(f"{self.character.health} / 100", True, (15, 5, 25))
  97. self.game_display.blit(text, (80, 9))
  98. text = font.render(f"{self.bot.health} / 100", True, (15, 5, 25))
  99. self.game_display.blit(text, (490, 9))
  100.  
  101. def creating_characters(self):
  102. # creating Character image
  103. self.water_priest = pygame.image.load("images/water-mage.png")
  104. self.game_display.blit(self.water_priest, (10, 50))
  105. # creating Bot image character
  106. self.dark_priest = pygame.image.load("images/dark-mage.png")
  107. self.game_display.blit(self.dark_priest, (360, 50))
  108.  
  109. # drawing spell buttons and hover
  110. def spell_buttons(self):
  111. mouse = pygame.mouse.get_pos()
  112. click = pygame.mouse.get_pressed()
  113. # first button
  114. if 190+50 > mouse[0] > 190 and 5+30 > mouse[1] > 5:
  115. pygame.draw.rect(self.game_display, self.btn_dark_gray, (190, 5, 75, 30))
  116. if click[0] == 1:
  117. self.character.health = self.character.health - self.character.spell_berserk()
  118. self.bot.health = self.bot.health - self.character.spell_berserk()
  119. else:
  120. pygame.draw.rect(self.game_display, self.btn_gray, (190, 5, 75, 30))
  121. # second button
  122. if 270+50 > mouse[0] > 270 and 5+30 > mouse[1] > 5:
  123. pygame.draw.rect(self.game_display, self.btn_dark_gray, (270, 5, 75, 30))
  124. if click[0] == 1:
  125. self.character.health = self.character.health - self.character.spell_fierce_berserk()
  126. self.bot.health = self.bot.health - random.choice(self.character.func_list)()
  127. else:
  128. pygame.draw.rect(self.game_display, self.btn_gray, (270, 5, 75, 30))
  129. # third button
  130. if 350+50 > mouse[0] > 350 and 5+30 > mouse[1] > 5:
  131. pygame.draw.rect(self.game_display, self.btn_dark_gray, (350, 5, 75, 30))
  132. if click[0] == 1:
  133. self.character.health = self.character.health + self.character.spell_light_healing()
  134. self.bot.health = self.bot.health + self.character.spell_light_healing()
  135. else:
  136. pygame.draw.rect(self.game_display, self.btn_gray, (350, 5, 75, 30))
  137. # printing txt on spell_buttons
  138. font = pygame.font.SysFont("arial", 13)
  139. text = font.render(f"Berserk", True, (15, 5, 25))
  140. self.game_display.blit(text, (210, 10))
  141.  
  142. font = pygame.font.SysFont("arial", 13)
  143. text = font.render(f"Fierce Berserk", True, (15, 5, 25))
  144. self.game_display.blit(text, (272, 10))
  145.  
  146. font = pygame.font.SysFont("arial", 13)
  147. text = font.render(f"Light Healing", True, (15, 5, 25))
  148. self.game_display.blit(text, (355, 10))
  149.  
  150. def quit_button(self):
  151. click = pygame.mouse.get_pressed()
  152. mouse = pygame.mouse.get_pos()
  153. if 540+55 > mouse[0] > 540 and 445+30 > mouse[1] > 445:
  154. pygame.draw.rect(self.game_display, self.btn_blue, (540, 445, 55, 30))
  155. if click[0] == 1:
  156. pygame.quit()
  157. quit()
  158.  
  159. font = pygame.font.SysFont("arial", 14)
  160. text = font.render(f"Quit", True, (15, 5, 25))
  161. self.game_display.blit(text, (555, 450))
  162.  
  163. def char_names(self):
  164. font = pygame.font.SysFont("arial", 14)
  165. text = font.render(f"YOU", True, (15, 5, 25))
  166. self.game_display.blit(text, (15, 9))
  167.  
  168. font = pygame.font.SysFont("arial", 14)
  169. text = font.render(f"BOT", True, (15, 5, 25))
  170. self.game_display.blit(text, (565, 9))
  171.  
  172. # ESC to quit
  173. def handle_keyboard_input(self):
  174. for event in pygame.event.get():
  175. if event.type == pygame.KEYDOWN:
  176. if event.type == pygame.QUIT:
  177. self.stop_game = True
  178. if event.key == pygame.K_ESCAPE:
  179. self.stop_game = True
  180.  
  181. def draw_game_over(self):
  182. if self.character.health <= 0:
  183. self.game_display.fill(self.bg_color)
  184. font = pygame.font.SysFont('Verdana', 72)
  185. text = font.render("You Lost!", True, (255, 255, 0))
  186. self.game_display.blit(text, (int(self.display_width/2) - text.get_width() // 2, (int(self.display_height/2) - text.get_height() // 2)))
  187. self.quit_button()
  188. pygame.display.update()
  189. elif self.bot.health <= 0:
  190. self.game_display.fill(self.bg_color)
  191. font = pygame.font.SysFont('Verdana', 72)
  192. text = font.render("You win!", True, (255, 255, 0))
  193. self.game_display.blit(text, (int(self.display_width/2) - text.get_width() // 2, (int(self.display_height/2) - text.get_height() // 2)))
  194. self.quit_button()
  195. pygame.display.update()
  196.  
  197. def game_loop(self):
  198. while not self.stop_game:
  199. self.game_display.fill(self.bg_color)
  200. self.clock.tick(self.fps_number)
  201. self.handle_keyboard_input()
  202. self.health_bar()
  203. self.health_bar_heal()
  204. self.display_hp()
  205. self.char_names()
  206. self.creating_characters()
  207. self.spell_buttons()
  208. self.quit_button()
  209. pygame.display.update()
  210. self.draw_game_over()
  211.  
  212.  
  213. Game().game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement