当前位置:   article > 正文

Python_Game之开心消消乐_python开心消消乐。

python开心消消乐。

目录

前言

环境要求

项目演示

源码分享


前言

        老少皆宜的儿童8+游戏,历经两年半的沉淀,终于让我在鸽鸽手上薅到了,特此马不停蹄来水一篇博客。(如有侵权,请私信解决!)

环境要求

【软件】

        python3.7

        pycharm专业版

【模块】

        pip install pygame

项目演示

源码分享

Sounds.p

  1. # -*- encoding: utf-8 -*-
  2. '''Game sounds.'''
  3. from enum import Enum
  4. from pygame.mixer import Sound
  5. class Sounds(Enum):
  6. '''Enum for the game's sounds.'''
  7. GAME_BGM = 'sound/GameSceneBGM.ogg'
  8. WORLD_BGM = 'sound/WorldSceneBGM.ogg'
  9. ELIMINATE_FORMAT = 'sound/eliminate/%d.ogg'
  10. SCORE_LEVEL = ('sound/good.ogg', 'sound/great.ogg', 'sound/amazing.ogg', 'sound/excellent.ogg',\
  11. 'sound/unbelievable.ogg')
  12. CLICK = 'sound/click.bubble.ogg'
  13. BOARD_SOUND = 'sound/board.ogg'
  14. CLICK_BUTTON = 'sound/click_common_button.ogg'
  15. MONEY = 'sound/money.ogg'
  16. ICE_BREAKING = 'sound/ice_break.ogg'
  17. @staticmethod
  18. def eliminate(idx):
  19. '''Plays the eliminate sound with given index.'''
  20. Sound(Sounds.ELIMINATE_FORMAT.value%idx).play()
  21. @staticmethod
  22. def score_level(idx):
  23. '''Plays the score level sound with given index.'''
  24. Sound(Sounds.SCORE_LEVEL.value[idx]).play()
  25. def play_sound(sound: Enum):
  26. '''Play sound with given number of loops.'''
  27. Sound(sound.value).play()

Manager.py

  1. # -*- encoding: utf-8 -*-
  2. '''Game manager module.'''
  3. # pylint: disable=fixme, line-too-long, invalid-name, undefined-variable
  4. # pylint: disable=too-many-branches, too-many-statements, too-many-arguments
  5. from random import randint
  6. import pygame
  7. from pygame.locals import * # pylint: disable=wildcard-import, unused-wildcard-import
  8. from pygame.time import delay
  9. from sprites import Tree, Board, Element
  10. from sounds import Sounds, play_sound
  11. class TreeManager:
  12. '''Tree manager.'''
  13. __screen_size = (900, 600)
  14. screen = pygame.display.set_mode(__screen_size, DOUBLEBUF, 32)
  15. fruit_list = []
  16. fruit_image = pygame.image.load(Tree.fruit).convert_alpha()
  17. fruit_width = fruit_image.get_width()
  18. fruit_height = fruit_image.get_height()
  19. type = 0 # 0 Tree, 1 Energy
  20. energy_full = False # Energy full mark
  21. money_empty = False # Not any money left?
  22. def display_text(self, text, position, txt_size=25, txt_color=(255, 255, 255)):
  23. '''Display text with given position, size and color.'''
  24. my_font = pygame.font.SysFont(None, txt_size)
  25. text_screen = my_font.render(text, True, txt_color)
  26. self.screen.blit(text_screen, position)
  27. def draw_tree(self, energy_num, money_num):
  28. '''Draws the game tree.'''
  29. Tree(Tree.tree, (0, 600)).draw(self.screen) # Draw tree
  30. Tree(Tree.energy_num, Tree.energy_num_position).draw(self.screen) # Draw energy num
  31. if energy_num > 30:
  32. self.display_text(str(30) + '/30', (22, 55), 21)
  33. else:
  34. self.display_text(str(energy_num)+'/30', (22, 55), 21)
  35. Tree(Tree.money, (15, 135)).draw(self.screen) # Draw money
  36. self.display_text(str(money_num), (32, 124), 21)
  37. for i in range(0, 10): # Draw fruits
  38. Tree(Tree.fruit, Tree.position[i]).draw(self.screen)
  39. self.display_text(str(i+1), (Tree.position[i][0]+15, Tree.position[i][1]-47))
  40. if self.type == 1:
  41. Tree(Tree.energy_buy, Tree.energy_buy_position).draw(self.screen)
  42. if self.energy_full:
  43. self.display_text('energy is full!', (430, 310), 30, (255, 0, 0))
  44. pygame.display.flip()
  45. delay(500)
  46. self.energy_full = False
  47. if self.money_empty:
  48. self.display_text('money is not enough!', (410, 310), 30, (255, 0, 0))
  49. pygame.display.flip()
  50. delay(500)
  51. self.money_empty = False
  52. def mouse_select(self, mgr, mousex, mousey, level, energy_num, money_num):
  53. '''Handle mouse event.'''
  54. if self.type == 0: # Tree Scene
  55. for i in range(0, 10):
  56. if Tree.position[i][0] < mousex < Tree.position[i][0] + self.fruit_width \
  57. and Tree.position[i][1] - self.fruit_height < mousey < Tree.position[i][1]:
  58. if energy_num <= 0:
  59. self.type = 1
  60. else:
  61. level = i + 1
  62. if Tree.energy_num_position[0] < mousex < Tree.energy_num_position[0] + 60 \
  63. and Tree.energy_num_position[1] - 60 < mousey < Tree.energy_num_position[1]: # 精力60*60
  64. play_sound(Sounds.CLICK)
  65. self.type = 1
  66. else: # Energy Scene
  67. if 408 < mousex < 600 and 263 < mousey < 313: # "Buy Energy" button clicked
  68. play_sound(Sounds.CLICK_BUTTON)
  69. if money_num < 50:
  70. self.money_empty = True
  71. if energy_num >= 30:
  72. self.energy_full = True
  73. elif energy_num < 30 and money_num >= 50:
  74. energy_num += 5
  75. money_num -= 50
  76. elif 619 < mousex < 638 and 158 < mousey < 177: # "X" clicked
  77. self.type = 0
  78. mgr.level, mgr.energy_num, mgr.money = level, energy_num, money_num
  79. # pylint: disable=too-many-public-methods, too-many-instance-attributes, too-many-nested-blocks
  80. class Manager:
  81. '''Game manager.'''
  82. __screen_size = (900, 600)
  83. screen = pygame.display.set_mode(__screen_size, DOUBLEBUF, 32)
  84. __brick_size = 50
  85. __bg = pygame.image.load('img/bg.png').convert()
  86. stop_width = 63
  87. selected = [-1, -1] # Current selected [row, col]
  88. swap_sign = -1 # Swap sign
  89. last_sel = [-1, -1] # Last selected [row, col]
  90. value_swapped = False # Swapped?
  91. death_sign = True # Death map sign
  92. boom_sel = [-1, -1] # Eliminate 4: [row, col]
  93. level = 0 # Current level, 0 for tree
  94. money = 100 # Money
  95. energy_num = 30 # Energy num
  96. num_sign = True
  97. type = 2 # (0) Playing, (1) Passed, (-1) Failed, (2) Tree
  98. reset_mode = True # Reset layout?
  99. init_step = 15 # Initial steps for each level
  100. step = init_step # Steps left of the game
  101. score = 0 # Score
  102. min = 20 # Medium score 1
  103. max = 50 # Medium score 2
  104. animal_num = [0, 0, 0, 0, 0, 0] # Number of eliminated animals
  105. ice_num = 0 # Number left of required ice
  106. success_board = Board(Board.success, [200, 0]) # Success board
  107. fail_board = Board(Board.fail, [200, 0]) # Failure board
  108. height, width = 9, 9
  109. row, col = 5, 5
  110. ice_list = [[-1 for _ in range(21)] for _ in range(21)] # (-1) None, (1) Ice
  111. animal = [[-1 for _ in range(21)] for _ in range(21)] # (-2) Elimated, (-1) None, (0-4) Animal
  112. list_x, list_y = (__screen_size[0] - 11 * __brick_size) / 2, (__screen_size[1] - 11 * __brick_size) / 2 # Position of the blocks
  113. def __init__(self, width, height):
  114. self.height = height
  115. self.width = width
  116. self.list_x = (Manager.__screen_size[0] - self.width * Manager.__brick_size) / 2
  117. self.list_y = (Manager.__screen_size[1] - self.height * Manager.__brick_size) / 2
  118. self.row, self.col = Manager.xy_rc(self.list_x, self.list_y)
  119. self.list_x, self.list_y = Manager.rc_xy(self.row, self.col)
  120. self.ice_list = [[-1 for _ in range(21)] for _ in range(21)]
  121. self.animal = [[-1 for _ in range(21)] for _ in range(21)]
  122. self.reset_animals()
  123. def reset_animals(self):
  124. '''Reset board with random animals.'''
  125. for row in range(self.row, self.row + self.height):
  126. for col in range(self.col, self.col + self.width):
  127. self.animal[row][col] = randint(0, 5)
  128. @staticmethod
  129. def rc_xy(row, col):
  130. '''(row, col) -> (x, y)'''
  131. return int(Manager.list_x + (col-Manager.col)*Manager.__brick_size), int\
  132. (Manager.list_y+(row-Manager.row)*Manager.__brick_size)
  133. @staticmethod
  134. def xy_rc(x, y):
  135. '''(x, y) -> (row, col)'''
  136. return int((y-Manager.list_y)/Manager.__brick_size+Manager.row), int\
  137. ((x-Manager.list_x)/Manager.__brick_size+Manager.col)
  138. @staticmethod
  139. def draw_brick(x, y):
  140. '''Draw a brick at (x, y).'''
  141. brick = Element(Element.brick, (x, y))
  142. Manager.screen.blit(brick.image, brick.rect)
  143. def draw_task(self, task_animal_num, which_animal, \
  144. board_position=(400, 90), animal_position=(430, 35), txt_position=(455, 60)):
  145. '''Draw task board'''
  146. txt_size = 24
  147. txt_color = (0, 0, 0)
  148. Board(Board.task_board, board_position).draw(self.screen)
  149. if which_animal == 6:
  150. task_animal = Element(Element.ice, animal_position)
  151. else:
  152. task_animal = Element(Element.animals[which_animal], animal_position)
  153. task_animal.image = pygame.transform.smoothscale(task_animal.image, (40, 40))
  154. task_animal.draw(self.screen)
  155. if which_animal == 6:
  156. if task_animal_num-self.ice_num <= 0:
  157. Board(Board.ok, (txt_position[0], txt_position[1]+15)).draw(self.screen)
  158. else:
  159. self.load_text(str(task_animal_num-self.ice_num), txt_position, txt_size, txt_color)
  160. else:
  161. if task_animal_num - self.animal_num[which_animal] <= 0:
  162. Board(Board.ok, (txt_position[0], txt_position[1]+15)).draw(self.screen)
  163. else:
  164. self.load_text(str(task_animal_num - self.animal_num[which_animal]), txt_position, txt_size, txt_color)
  165. def draw(self):
  166. '''Draw background, animals, and so on.'''
  167. # Draw background
  168. self.screen.blit(Manager.__bg, (0, 0))
  169. # Display steps left
  170. Board(Board.step_board, (0, 142)).draw(self.screen)
  171. tens, single = divmod(self.step, 10)
  172. if tens == 0:
  173. Board(Board.num_format%single, (790, 110)).draw(self.screen)
  174. else:
  175. Board(Board.num_format%tens, (775, 110)).draw(self.screen)
  176. Board(Board.num_format%single, (805, 110)).draw(self.screen)
  177. # Display level & pause button
  178. Board(Board.level_format%self.level, (30, 105)).draw(self.screen)
  179. Element(Element.stop, Element.stop_position).draw(self.screen)
  180. # Draw bricks, ice and animals
  181. brick_group = pygame.sprite.Group()
  182. animal_group = pygame.sprite.Group()
  183. ice_group = pygame.sprite.Group()
  184. for i in range(0, 21):
  185. for j in range(0, 21):
  186. x, y = Manager.rc_xy(i, j)
  187. if self.animal[i][j] != -1:
  188. brick_group.add(Element(Element.brick, (x, y)))
  189. animal_group.add(Element(Element.animals[self.animal[i][j]], (x, y)))
  190. if self.ice_list[i][j] != -1:
  191. ice_group.add(Element(Element.ice, (x, y)))
  192. brick_group.draw(self.screen)
  193. ice_group.draw(self.screen)
  194. for animallist in animal_group:
  195. self.screen.blit(animallist.image, animallist.rect)
  196. if self.level == 1:
  197. self.draw_task(10, 4)
  198. elif self.level == 2:
  199. self.draw_task(21, 1)
  200. elif self.level == 3:
  201. self.draw_task(16, 4, (300, 90), (330, 35), (360, 60))
  202. self.draw_task(16, 5, (500, 90), (530, 35), (560, 60))
  203. elif self.level == 4:
  204. self.draw_task(18, 5, (300, 90), (330, 35), (360, 60))
  205. self.draw_task(18, 2, (500, 90), (530, 35), (560, 60))
  206. elif self.level == 5:
  207. self.draw_task(28, 2, (300, 90), (330, 35), (360, 60))
  208. self.draw_task(28, 0, (500, 90), (530, 35), (560, 60))
  209. elif self.level == 6:
  210. self.draw_task(70, 4)
  211. elif self.level == 7:
  212. self.draw_task(36, 1)
  213. self.draw_task(36, 2, (300, 90), (330, 35), (360, 60))
  214. self.draw_task(36, 0, (500, 90), (530, 35), (560, 60))
  215. elif self.level == 8:
  216. self.draw_task(15, 6)
  217. elif self.level == 9:
  218. self.draw_task(49, 6)
  219. else:
  220. self.draw_task(39, 6)
  221. # Display selected animal
  222. if self.selected != [-1, -1]:
  223. frame_sprite = Element(Element.frame, Manager.rc_xy(self.selected[0], self.selected[1]))
  224. self.screen.blit(frame_sprite.image, frame_sprite.rect)
  225. # Show score
  226. self.load_text('Score:' + str(self.score), (300, 550), 30)
  227. pygame.draw.rect(self.screen, (50, 150, 50, 180), Rect(300, 570, self.score * 2, 25))
  228. pygame.draw.rect(self.screen, (100, 200, 100, 180), Rect(300, 570, 200, 25), 2)
  229. return animal_group
  230. def mouse_image(self):
  231. '''Replace the mouse image with img/mouse.png'''
  232. mouse_cursor = pygame.image.load('img/mouse.png').convert_alpha()
  233. mouse_x, mouse_y = pygame.mouse.get_pos()
  234. # Find the topleft position of the mouse
  235. mouse_x -= mouse_cursor.get_width() / 2
  236. mouse_y -= mouse_cursor.get_height() / 2
  237. self.screen.blit(mouse_cursor, (mouse_x, mouse_y))
  238. def mouse_select(self, mousex, mousey):
  239. '''Handle mouse click event.'''
  240. if self.type == 1: # Passed
  241. if Board.button_position[0][0] < mousex < Board.button_position[0][0]+100 \
  242. and Board.button_position[0][1] - 50 < mousey < Board.button_position[0][1]: # Clicked replay button
  243. if self.energy_num < 5:
  244. self.level = 0
  245. self.reset_mode = True
  246. elif Board.button_position[1][0] < mousex < Board.button_position[1][0]+100 \
  247. and Board.button_position[1][1]-50 < mousey < Board.button_position[1][1]: # Clicked next level button
  248. if self.level < 10:
  249. if self.energy_num < 5:
  250. self.level = 0
  251. else:
  252. self.level += 1
  253. self.reset_mode = True
  254. elif 610 < mousex < 610 + 55 and 205 - 55 < mousey < 205: # x
  255. self.level = 0
  256. self.reset_mode = True
  257. elif self.type == -1: # Failed
  258. if Board.button_position[1][0] < mousex < Board.button_position[1][0]+100 \
  259. and Board.button_position[1][1]-50 < mousey < Board.button_position[1][1]: # Clicked replay button
  260. if self.energy_num < 5:
  261. self.level = 0
  262. self.reset_mode = True
  263. elif Board.button_position[0][0] < mousex < Board.button_position[0][0]+100 \
  264. and Board.button_position[0][1]-50 < mousey < Board.button_position[0][1]: # Clicked 5 more steps button
  265. if self.money < 5:
  266. self.level = 0
  267. else:
  268. self.money -= 5
  269. self.step += 5
  270. self.type = 0 # Playing game
  271. self.fail_board = Board(Board.fail, [200, 0])
  272. elif 610 < mousex < 610 + 55 and 205 - 55 < mousey < 205:
  273. self.level = 0
  274. self.reset_mode = True
  275. elif self.type == 0:
  276. if self.list_x < mousex < self.list_x + Manager.__brick_size * self.width \
  277. and self.list_y < mousey < self.list_y + Manager.__brick_size * self.height:
  278. mouse_selected = Manager.xy_rc(mousex, mousey)
  279. if self.animal[mouse_selected[0]][mouse_selected[1]] != -1:
  280. play_sound(Sounds.CLICK)
  281. self.selected = mouse_selected
  282. if (self.last_sel[0] == self.selected[0] and abs(self.last_sel[1] - self.selected[1]) == 1) \
  283. or (self.last_sel[1] == self.selected[1] and abs(self.last_sel[0] - self.selected[0]) == 1):
  284. self.swap_sign = 1 # Valid move, swap
  285. elif Element.stop_position[0] < mousex < Element.stop_position[0]+self.stop_width\
  286. and Element.stop_position[1] < mousey < Element.stop_position[1]+self.stop_width: # Exit button clicked
  287. play_sound(Sounds.CLICK_BUTTON)
  288. self.level = 0
  289. self.reset_mode = True
  290. else:
  291. self.selected = [-1, -1]
  292. def swap(self, spritegroup):
  293. '''Swap two selected animals on the board.'''
  294. if self.swap_sign == -1: # Not swapped
  295. self.last_sel = self.selected
  296. if self.swap_sign == 1:
  297. last_x, last_y = Manager.rc_xy(self.last_sel[0], self.last_sel[1])
  298. sel_x, sel_y = Manager.rc_xy(self.selected[0], self.selected[1])
  299. if self.last_sel[0] == self.selected[0]: # Swap vertically
  300. for animallist in spritegroup:
  301. if animallist.rect.topleft == (last_x, last_y):
  302. last_sprite = animallist
  303. last_sprite.speed = [self.selected[1]-self.last_sel[1], 0]
  304. elif animallist.rect.topleft == (sel_x, sel_y):
  305. selected_sprite = animallist
  306. selected_sprite.speed = [self.last_sel[1]-self.selected[1], 0]
  307. else: # Swap horizontally
  308. for animallist in spritegroup:
  309. if animallist.rect.topleft == (last_x, last_y):
  310. last_sprite = animallist
  311. last_sprite.speed = [0, self.selected[0]-self.last_sel[0]]
  312. elif animallist.rect.topleft == (sel_x, sel_y):
  313. selected_sprite = animallist
  314. selected_sprite.speed = [0, self.last_sel[0]-self.selected[0]]
  315. while last_sprite.speed != [0, 0]:
  316. delay(5)
  317. self.draw_brick(last_x, last_y)
  318. self.draw_brick(sel_x, sel_y)
  319. last_sprite.move(last_sprite.speed)
  320. selected_sprite.move(selected_sprite.speed)
  321. self.screen.blit(last_sprite.image, last_sprite.rect)
  322. self.screen.blit(selected_sprite.image, selected_sprite.rect)
  323. pygame.display.flip()
  324. self.swap_values()
  325. if self.eliminate_animals():
  326. self.step -= 1
  327. else:
  328. self.swap_values()
  329. self.value_swapped = False
  330. self.boom_sel = self.selected
  331. self.swap_sign = -1
  332. self.selected = [-1, -1]
  333. def swap_values(self):
  334. '''Swap values.'''
  335. (xl, yl), (xc, yc) = self.last_sel, self.selected
  336. self.animal[xl][yl], self.animal[xc][yc] = self.animal[xc][yc], self.animal[xl][yl]
  337. def load_text(self, text, position, txt_size, txt_color=(255, 255, 255)):
  338. '''Display text with given position, size and color.'''
  339. my_font = pygame.font.SysFont(None, txt_size)
  340. text_screen = my_font.render(text, True, txt_color)
  341. self.screen.blit(text_screen, position)
  342. def death_map(self):
  343. '''Checks if there is not a valid move.'''
  344. for i in range(self.row, self.row + self.height):
  345. for j in range(self.col, self.col + self.width):
  346. if self.animal[i][j] != -1:
  347. if self.animal[i][j] == self.animal[i][j+1]:
  348. if (self.animal[i][j] in [self.animal[i-1][j-1], self.animal[i+1][j-1]] \
  349. and self.animal[i][j-1] != -1) or \
  350. (self.animal[i][j] in [self.animal[i-1][j+2], self.animal[i+1][j+2]] \
  351. and self.animal[i][j+2] != -1):
  352. # a b
  353. # a a
  354. # c d
  355. self.death_sign = False
  356. break
  357. if self.animal[i][j] == self.animal[i+1][j]:
  358. if (self.animal[i][j] in [self.animal[i-1][j-1], self.animal[i-1][j+1]] \
  359. and self.animal[i-1][j] != -1) or \
  360. (self.animal[i][j] in [self.animal[i+2][j - 1], self.animal[i+2][j + 1]] \
  361. and self.animal[i+2][j] != -1):
  362. # a b
  363. # a
  364. # a
  365. # c d
  366. self.death_sign = False
  367. break
  368. else:
  369. if self.animal[i-1][j-1] == self.animal[i][j]:
  370. if (self.animal[i][j] == self.animal[i-1][j+1] and self.animal[i-1][j] != -1)\
  371. or (self.animal[i][j] == self.animal[i+1][j-1] and self.animal[i][j-1] != -1):
  372. # a a a b
  373. # a a
  374. # c a
  375. self.death_sign = False
  376. break
  377. if self.animal[i][j] == self.animal[i+1][j+1]:
  378. if (self.animal[i][j] == self.animal[i-1][j+1] and self.animal[i][j+1] != -1)\
  379. or (self.animal[i][j] == self.animal[i+1][j-1] and self.animal[i+1][j] != -1):
  380. # a b
  381. # a a
  382. # b a a a
  383. self.death_sign = False
  384. break
  385. if self.death_sign:
  386. delay(500)
  387. Element(Element.none_animal, (230, 150)).draw(self.screen)
  388. pygame.display.flip()
  389. delay(500)
  390. temp = [self.step, self.score, self.animal_num, self.ice_num, self.energy_num]
  391. self.reset_mode = True
  392. self.set_level_mode(self.level)
  393. self.step = temp[0]
  394. self.score = temp[1]
  395. self.animal_num = temp[2]
  396. self.ice_num = temp[3]
  397. self.energy_num = temp[4]
  398. else:
  399. self.death_sign = True
  400. # TODO: Merge 4 functions below
  401. def exists_left(self, i, j, num):
  402. '''Checks there are at least {num} continous same animals on the left side of (i, j).'''
  403. for t in range(0, num):
  404. if self.animal[i][j] != self.animal[i][j - t] or self.animal[i][j] < 0:
  405. return False
  406. return True
  407. def exists_right(self, i, j, num):
  408. '''Checks there are at least {num} continous same animals on the right side of (i, j).'''
  409. for t in range(0, num):
  410. if self.animal[i][j] != self.animal[i][j + t] or self.animal[i][j] < 0:
  411. return False
  412. return True
  413. def exists_up(self, i, j, num):
  414. '''Checks there are at least {num} continous same animals above (i, j).'''
  415. for t in range(0, num):
  416. if self.animal[i][j] != self.animal[i - t][j] or self.animal[i][j] < 0:
  417. return False
  418. return True
  419. def exists_down(self, i, j, num):
  420. '''Checks there are at least {num} continous same animals below (i, j).'''
  421. for t in range(0, num):
  422. if self.animal[i][j] != self.animal[i + t][j] or self.animal[i][j] < 0:
  423. return False
  424. return True
  425. # TODO: Merge 4 functions below
  426. def change_left(self, i, j, num):
  427. '''Change the left side of the animal.'''
  428. self.value_swapped = True
  429. self.score += num
  430. for k in range(0, int(num)):
  431. self.animal[i][j - k] = -2
  432. def change_right(self, i, j, num):
  433. '''Change the right side of the animal.'''
  434. self.value_swapped = True
  435. self.score += num
  436. for k in range(0, num):
  437. self.animal[i][j + k] = -2
  438. def change_up(self, i, j, num):
  439. '''Change above the animal.'''
  440. self.value_swapped = True
  441. self.score += num
  442. for k in range(0, num):
  443. self.animal[i-k][j] = -2
  444. def change_down(self, i, j, num):
  445. '''Change below the animal.'''
  446. self.value_swapped = True
  447. self.score += num
  448. for k in range(0, num):
  449. self.animal[i+k][j] = -2
  450. def eliminate_animals(self):
  451. '''Eliminate the animals.'''
  452. score_level = self.score
  453. self.value_swapped = False
  454. for i in range(self.row, self.row + self.height):
  455. for j in range(self.col, self.col + self.width):
  456. # TODO: Simplify the if statement below
  457. if self.exists_right(i, j, 5):
  458. self.value_swapped = True
  459. if self.exists_down(i, j+2, 3):
  460. self.animal_num[self.animal[i][j]] += 7
  461. Sounds.eliminate(5) # Elimination sound 5
  462. self.change_right(i, j, 5)
  463. self.change_down(i, j+2, 3)
  464. else:
  465. self.animal_num[self.animal[i][j]] += 5
  466. Sounds.eliminate(3) # Elimination sound 3
  467. self.change_right(i, j, 5)
  468. elif self.exists_right(i, j, 4):
  469. self.value_swapped = True
  470. if self.exists_down(i, j+1, 3):
  471. self.animal_num[self.animal[i][j]] += 6
  472. Sounds.eliminate(4) # Elimination sound 4
  473. self.change_right(i, j, 4)
  474. self.change_down(i, j+1, 3)
  475. elif self.exists_down(i, j+2, 3):
  476. self.animal_num[self.animal[i][j]] += 6
  477. Sounds.eliminate(4) # Elimination sound 4
  478. self.change_right(i, j, 4)
  479. self.change_down(i, j+2, 3)
  480. else:
  481. self.animal_num[self.animal[i][j]] += 4
  482. Sounds.eliminate(2) # Elimination sound 2
  483. self.change_right(i, j, 4)
  484. elif self.exists_right(i, j, 3):
  485. self.value_swapped = True
  486. if self.exists_down(i, j, 3):
  487. self.animal_num[self.animal[i][j]] += 5
  488. Sounds.eliminate(3) # Elimination sound 3
  489. self.change_right(i, j, 3)
  490. self.change_down(i, j, 3)
  491. elif self.exists_down(i, j+1, 3):
  492. self.animal_num[self.animal[i][j]] += 5
  493. Sounds.eliminate(3) # Elimination sound 3
  494. self.change_right(i, j, 3)
  495. self.change_down(i, j+1, 3)
  496. elif self.exists_down(i, j+2, 3):
  497. self.animal_num[self.animal[i][j]] += 5
  498. Sounds.eliminate(3) # Elimination sound 3
  499. self.change_right(i, j, 3)
  500. self.change_down(i, j + 2, 3)
  501. else:
  502. self.animal_num[self.animal[i][j]] += 3
  503. Sounds.eliminate(1) # Elimination sound 1
  504. self.change_right(i, j, 3)
  505. elif self.exists_down(i, j, 5):
  506. self.value_swapped = True
  507. if self.exists_right(i+2, j, 3):
  508. self.animal_num[self.animal[i][j]] += 7
  509. Sounds.eliminate(5) # Elimination sound 5
  510. self.change_down(i, j, 5)
  511. self.change_right(i+2, j, 3)
  512. elif self.exists_left(i+2, j, 3):
  513. self.animal_num[self.animal[i][j]] += 7
  514. Sounds.eliminate(5) # Elimination sound 5
  515. self.change_down(i, j, 5)
  516. self.change_left(i+2, j, 3)
  517. else:
  518. self.animal_num[self.animal[i][j]] += 5
  519. Sounds.eliminate(3) # Elimination sound 3
  520. self.change_down(i, j, 5)
  521. elif self.exists_down(i, j, 4):
  522. self.value_swapped = True
  523. if self.exists_left(i+1, j, 3):
  524. self.animal_num[self.animal[i][j]] += 6
  525. Sounds.eliminate(4) # Elimination sound 4
  526. self.change_down(i, j, 4)
  527. self.change_left(i+1, j, 3)
  528. elif self.exists_right(i+1, j, 3):
  529. self.animal_num[self.animal[i][j]] += 6
  530. Sounds.eliminate(4) # Elimination sound 4
  531. self.change_down(i, j, 4)
  532. self.change_right(i+1, j, 3)
  533. elif self.exists_left(i+2, j, 3):
  534. self.animal_num[self.animal[i][j]] += 6
  535. Sounds.eliminate(4) # Elimination sound 4
  536. self.change_down(i, j, 4)
  537. self.change_left(i+2, j, 3)
  538. elif self.exists_right(i+2, j, 3):
  539. self.animal_num[self.animal[i][j]] += 6
  540. Sounds.eliminate(4) # Elimination sound 4
  541. self.change_down(i, j, 4)
  542. self.change_right(i+2, j, 3)
  543. else:
  544. self.animal_num[self.animal[i][j]] += 4
  545. Sounds.eliminate(2) # Elimination sound 2
  546. self.change_down(i, j, 4)
  547. elif self.exists_down(i, j, 3):
  548. self.value_swapped = True
  549. if self.exists_left(i+1, j, 3):
  550. self.animal_num[self.animal[i][j]] += 5
  551. Sounds.eliminate(3) # Elimination sound 3
  552. self.change_down(i, j, 3)
  553. self.change_left(i+1, j, 3)
  554. elif self.exists_right(i+1, j, 3):
  555. self.animal_num[self.animal[i][j]] += 5
  556. Sounds.eliminate(3) # Elimination sound 3
  557. self.change_down(i, j, 3)
  558. self.change_right(i+1, j, 3)
  559. elif self.exists_left(i+2, j, 3):
  560. self.animal_num[self.animal[i][j]] += 5
  561. Sounds.eliminate(3) # Elimination sound 3
  562. self.change_down(i, j, 3)
  563. self.change_left(i+2, j, 3)
  564. elif self.exists_right(i+2, j, 3):
  565. self.animal_num[self.animal[i][j]] += 5
  566. Sounds.eliminate(3) # Elimination sound 3
  567. self.change_down(i, j, 3)
  568. self.change_right(i+2, j, 3)
  569. elif self.exists_left(i+2, j, 2) and self.exists_right(i+2, j, 2):
  570. self.animal_num[self.animal[i][j]] += 5
  571. Sounds.eliminate(3) # Elimination sound 3
  572. self.change_down(i, j, 3)
  573. self.change_left(i+2, j, 2)
  574. self.change_right(i+2, j, 2)
  575. elif self.exists_left(i+2, j, 2) and self.exists_right(i+2, j, 3):
  576. self.animal_num[self.animal[i][j]] += 6
  577. Sounds.eliminate(4) # Elimination sound 4
  578. self.change_down(i, j, 3)
  579. self.change_left(i+2, j, 2)
  580. self.change_right(i+2, j, 3)
  581. elif self.exists_left(i+2, j, 3) and self.exists_right(i+2, j, 2):
  582. self.animal_num[self.animal[i][j]] += 6
  583. Sounds.eliminate(4) # Elimination sound 4
  584. self.change_down(i, j, 3)
  585. self.change_left(i+2, j, 3)
  586. self.change_right(i+2, j, 2)
  587. elif self.exists_left(i+2, j, 3) and self.exists_right(i+2, j, 3):
  588. self.animal_num[self.animal[i][j]] += 7
  589. Sounds.eliminate(5) # Elimination sound 5
  590. self.change_down(i, j, 3)
  591. self.change_left(i+2, j, 3)
  592. self.change_right(i+2, j, 3)
  593. else:
  594. self.animal_num[self.animal[i][j]] += 3
  595. Sounds.eliminate(1) # Elimination sound 1
  596. self.change_down(i, j, 3)
  597. self.fall_animal()
  598. score_level = self.score - score_level # Score level
  599. # Display & speak: good, great, amazing, excellent, unbelievable
  600. if score_level < 5:
  601. return self.value_swapped
  602. if score_level < 8: # 5 good
  603. Sounds.score_level(0)
  604. Element(Element.score_level[0], (350, 250)).draw(self.screen)
  605. pygame.display.flip()
  606. delay(500)
  607. elif score_level < 10: # 8 great
  608. Sounds.score_level(1)
  609. Element(Element.score_level[1], (350, 250)).draw(self.screen)
  610. pygame.display.flip()
  611. delay(500)
  612. elif score_level < 15: # 10 amazing
  613. Sounds.score_level(2)
  614. Element(Element.score_level[2], (350, 250)).draw(self.screen)
  615. pygame.display.flip()
  616. delay(500)
  617. elif score_level < 20: # 15 excellent
  618. Sounds.score_level(3)
  619. Element(Element.score_level[3], (350, 250)).draw(self.screen)
  620. pygame.display.flip()
  621. delay(500)
  622. elif score_level >= 20: # 20 unbelievable
  623. Sounds.score_level(4)
  624. Element(Element.score_level[4], (350, 250)).draw(self.screen)
  625. pygame.display.flip()
  626. delay(500)
  627. return self.value_swapped # Return the swap value sign
  628. def fall_animal(self): # pylint: disable=too-many-locals
  629. '''Animation of falling animals'''
  630. clock = pygame.time.Clock()
  631. position = []
  632. ice_position = []
  633. for i in range(self.row, self.row + self.height):
  634. for j in range(self.col, self.col + self.width):
  635. if self.animal[i][j] == -2:
  636. x, y = self.rc_xy(i, j)
  637. position.append((x, y))
  638. if self.ice_list[i][j] == 1:
  639. ice_position.append((x, y))
  640. if position:
  641. for index in range(0, 9):
  642. clock.tick(20)
  643. for pos in position:
  644. self.draw_brick(pos[0], pos[1])
  645. if pos in ice_position:
  646. Element(Element.ice_format%index, (pos[0], pos[1])).draw(self.screen)
  647. Element(Element.bling_format%index, (pos[0], pos[1])).draw(self.screen)
  648. pygame.display.flip()
  649. for i in range(self.row, self.row + self.height):
  650. brick_position = []
  651. fall_animal_list = []
  652. speed = [0, 1]
  653. for j in range(self.col, self.col + self.width):
  654. if self.animal[i][j] == -2:
  655. x, y = self.rc_xy(i, j)
  656. if self.ice_list[i][j] == 1:
  657. play_sound(Sounds.ICE_BREAKING)
  658. self.ice_num += 1
  659. self.ice_list[i][j] = -1
  660. brick_position.append((x, y))
  661. for m in range(i, self.row - 1, -1):
  662. if self.animal[m - 1][j] != -1:
  663. x, y = self.rc_xy(m - 1, j)
  664. brick_position.append((x, y))
  665. animal = Element(Element.animals[self.animal[m - 1][j]], (x, y))
  666. fall_animal_list.append(animal)
  667. self.animal[m][j] = self.animal[m - 1][j]
  668. else:
  669. self.animal[m][j] = randint(0, 5)
  670. break
  671. while speed != [0, 0] and fall_animal_list:
  672. for position in brick_position:
  673. self.draw_brick(position[0], position[1])
  674. for animal_sprite in fall_animal_list:
  675. animal_sprite.move(speed)
  676. animal_sprite.draw(self.screen)
  677. speed = animal_sprite.speed
  678. pygame.display.flip()
  679. def judge_next(self, tp, score):
  680. '''Check whether the next level is reached or not'''
  681. if tp == 1: # Passed
  682. self.load_fns_window(score)
  683. elif tp == -1: # Failed
  684. self.load_fail_window()
  685. def load_fail_window(self):
  686. '''Display the failure board and buttons'''
  687. sound_sign = 0
  688. step_add = Board(Board.step_add, Board.button_position[0]) # L: 5 more steps
  689. retry = Board(Board.replay, Board.button_position[1]) # R: Replay
  690. self.screen.blit(self.fail_board.image, self.fail_board.rect) # Failure board
  691. self.screen.blit(step_add.image, step_add.rect)
  692. self.screen.blit(retry.image, retry.rect)
  693. while self.fail_board.speed != [0, 0]:
  694. self.draw()
  695. self.screen.blit(self.fail_board.image, self.fail_board.rect)
  696. self.fail_board.move()
  697. pygame.display.flip()
  698. if sound_sign == 0:
  699. play_sound(Sounds.BOARD_SOUND)
  700. sound_sign = 1
  701. def load_fns_window(self, score):
  702. '''Display the success board, score and buttons'''
  703. sound_sign = 0
  704. replay = Board(Board.replay, Board.button_position[0]) # L: Replay
  705. self.screen.blit(self.success_board.image, self.success_board.rect) # Successful board
  706. if self.level < 10: # If not the last level
  707. next_level = Board(Board.next, Board.button_position[1]) # R: Next level
  708. self.screen.blit(next_level.image, next_level.rect)
  709. self.screen.blit(replay.image, replay.rect)
  710. while self.success_board.speed != [0, 0]:
  711. self.draw()
  712. self.screen.blit(self.success_board.image, self.success_board.rect)
  713. self.success_board.move()
  714. pygame.display.flip()
  715. if sound_sign == 0:
  716. play_sound(Sounds.BOARD_SOUND)
  717. sound_sign = 1
  718. self.displayStars(score) # Display the stars
  719. # Money
  720. self.load_text(str(self.score*2), (Board.starts_position[0][0]+75, Board.starts_position[0][0]+46), 20, (0, 0, 0))
  721. def displayStars(self, score):
  722. '''Display the stars according to the score.'''
  723. star1 = Board(Board.stars, Board.starts_position[0])
  724. star2 = Board(Board.stars, Board.starts_position[1])
  725. star3 = Board(Board.stars, Board.starts_position[2])
  726. if 0 <= score < self.min:
  727. self.load_text('1', (Board.starts_position[1][0]+48, Board.starts_position[1][1]+35), 20, (0, 0, 0))
  728. self.screen.blit(star1.image, star1.rect)
  729. elif self.min <= score <= self.max:
  730. self.load_text('2', (Board.starts_position[1][0] + 48, Board.starts_position[1][1] + 35), 20, (0, 0, 0))
  731. self.screen.blit(star1.image, star1.rect)
  732. self.screen.blit(star2.image, star2.rect)
  733. elif score > self.max:
  734. self.load_text('5', (Board.starts_position[1][0] + 48, Board.starts_position[1][1] + 35), 20, (0, 0, 0))
  735. self.screen.blit(star1.image, star1.rect)
  736. self.screen.blit(star2.image, star2.rect)
  737. self.screen.blit(star3.image, star3.rect)
  738. pygame.display.flip()
  739. def set_level_mode(self, level):
  740. '''Set the level mode and its steps.'''
  741. self.level = level
  742. if self.reset_mode: # If it is required to reset the mode
  743. self.num_sign = True
  744. if level == 1:
  745. self.__init__(7, 7)
  746. self.animal[7][9] = self.animal[7][10] = self.animal[7][11] = self.animal[8][10] = self.animal[11][7] = \
  747. self.animal[11][13] = self.animal[12][7] = self.animal[12][8] = self.animal[12][12] = self.animal[12][13] = \
  748. self.animal[13][7] = self.animal[13][8] = self.animal[13][9] = self.animal[13][11] = self.animal[13][12] = \
  749. self.animal[13][13] = -1
  750. self.init_step = 17 # 17 initial steps
  751. elif level == 2:
  752. self.__init__(4, 8)
  753. self.init_step = 16 # 16 initial steps
  754. elif level == 3:
  755. self.__init__(7, 7)
  756. self.init_step = 18 # 18 initial steps
  757. elif level == 4:
  758. self.__init__(9, 7)
  759. row, col = self.row, self.col
  760. self.animal[row][col] = self.animal[row][col+7] = self.animal[row][col+8] = self.animal[row+1][col+8] = \
  761. self.animal[row+5][col] = self.animal[row+6][col] = self.animal[row+6][col+1] = self.animal[row+6][col+8] = -1
  762. self.init_step = 20
  763. elif level == 5:
  764. self.__init__(8, 9)
  765. row, col = self.row, self.col
  766. self.animal[row][col+7] = self.animal[row+2][col] = self.animal[row+5][col] = self.animal[row+3][col+7] = \
  767. self.animal[row+6][col+7] = self.animal[row+8][col] = -1
  768. self.init_step = 20
  769. elif level == 6:
  770. self.__init__(9, 9)
  771. row, col = self.row, self.col
  772. self.animal[row][col] = self.animal[row][col+8] = self.animal[row+2][col+4] = self.animal[row+3][col+2] = \
  773. self.animal[row+3][col+6] = self.animal[row+8][col] = self.animal[row+8][col+8] = -1
  774. for i in range(row+4, row+6):
  775. for j in range(col+3, col+6):
  776. self.animal[i][j] = -1
  777. self.init_step = 28
  778. elif level == 7:
  779. self.__init__(9, 9)
  780. row, col = self.row, self.col
  781. for i in range(row, row + 9):
  782. self.animal[i][col+4] = -1
  783. for j in range(col, col+4):
  784. self.animal[row+3][j] = -1
  785. for j in range(col+5, col+9):
  786. self.animal[row+5][j] = -1
  787. self.init_step = 25
  788. elif level == 8:
  789. self.__init__(7, 8)
  790. row, col = self.row, self.col
  791. for i in range(row+2, row+5):
  792. for j in range(col+1, col+6):
  793. self.ice_list[i][j] = 1
  794. self.init_step = 21
  795. elif level == 9:
  796. self.__init__(9, 9)
  797. row, col = self.row, self.col
  798. self.animal[row][col+4] = self.animal[row+4][col] = self.animal[row+4][col+8] = self.animal[row+8][col+4] = -1
  799. for i in range(row+1, row+8):
  800. for j in range(col+1, col+8):
  801. self.ice_list[i][j] = 1
  802. self.init_step = 35
  803. else:
  804. self.__init__(9, 9)
  805. row, col = self.row, self.col
  806. for i in range(row, row+2):
  807. for j in range(col, col+9):
  808. self.animal[i][j] = -1
  809. self.animal[row][col+4] = randint(0, 5)
  810. self.animal[row+1][col+2] = randint(0, 5)
  811. self.animal[row+1][col+4] = randint(0, 5)
  812. self.animal[row+1][col+6] = randint(0, 5)
  813. self.animal[row+2][col+1] = self.animal[row+3][col+1] = self.animal[row+2][col+3] = self.animal[row+3][col+3] =\
  814. self.animal[row+2][col+5] = self.animal[row+3][col+5] = self.animal[row+2][col+7] = \
  815. self.animal[row+3][col+7] = self.animal[row+8][col] = self.animal[row+8][col+8] = -1
  816. for i in range(row+4, row+8):
  817. for j in range(col, col+9):
  818. self.ice_list[i][j] = 1
  819. self.ice_list[row+2][col+4] = self.ice_list[row+3][col+2] = self.ice_list[row+3][col+4] = \
  820. self.ice_list[row+3][col+6] = 1
  821. self.init_step = 40
  822. self.type = 0
  823. self.energy_num -= 5
  824. self.success_board = Board(Board.success, [200, 0]) # Success board
  825. self.fail_board = Board(Board.fail, [200, 0]) # Failure board
  826. self.step = self.init_step
  827. self.score = 0
  828. self.animal_num = [0, 0, 0, 0, 0, 0]
  829. self.ice_num = 0
  830. self.reset_mode = False
  831. def num_add(self):
  832. '''Add to score'''
  833. if self.num_sign:
  834. self.money += self.score * 2
  835. if self.score < self.min:
  836. self.energy_num += 1
  837. elif self.score < self.max:
  838. self.energy_num += 2
  839. else:
  840. self.energy_num += 5
  841. self.num_sign = False
  842. def judge_level(self):
  843. '''Check whether the level was passed'''
  844. if self.step <= 0:
  845. self.type = -1 # Game over
  846. if self.level == 1:
  847. if self.animal_num[4] >= 10: # L1: 10 frogs
  848. self.type = 1 # Level 1 passed
  849. self.num_add()
  850. elif self.level == 2:
  851. if self.animal_num[1] >= 21: # L2: 21 bears
  852. self.type = 1 # Level 2 passed
  853. self.num_add()
  854. elif self.level == 3:
  855. if self.animal_num[4] >= 16 and self.animal_num[5] >= 16: # L3: 16 frogs and 16 cows
  856. self.type = 1 # Level 3 passed
  857. self.num_add()
  858. elif self.level == 4:
  859. if self.animal_num[5] >= 18 and self.animal_num[2] >= 18: # L4: 18 cows and 18 chicks
  860. self.type = 1 # Level 4 passed
  861. self.num_add()
  862. elif self.level == 5:
  863. if self.animal_num[2] >= 28 and self.animal_num[0] >= 28: # L5: 28 chicks and 28 foxes
  864. self.type = 1 # Level 5 passed
  865. self.num_add()
  866. elif self.level == 6:
  867. if self.animal_num[4] >= 70: # L6: 70 frogs
  868. self.type = 1 # Level 6 passed
  869. self.num_add()
  870. elif self.level == 7:
  871. if self.animal_num[2] >= 36 and self.animal_num[1] >= 36 and self.animal_num[0] >= 36: # L7: 36 chickens, 36 bears and 36 foxes
  872. self.type = 1 # Level 7 passed
  873. self.num_add()
  874. elif self.level == 8:
  875. if self.ice_num >= 15: # L8: 15 ice
  876. self.type = 1 # Level 8 passed
  877. self.num_add()
  878. elif self.level == 9:
  879. if self.ice_num >= 49: # L9: 49 ice
  880. self.type = 1 # Level 9 passed
  881. self.num_add()
  882. else:
  883. if self.ice_num >= 39: # L10: 39 ice
  884. self.type = 1 # Level 10 passed
  885. self.num_add()
  886. self.judge_next(self.type, self.score)

Main.py

  1. # -*- encoding: utf-8 -*-
  2. '''Main module for the application.'''
  3. # pylint: disable=invalid-name, wrong-import-position
  4. # Hide pygame's support prompt.
  5. from os import environ
  6. environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
  7. import sys
  8. import pygame
  9. from pygame.locals import KEYDOWN, QUIT, K_q, K_ESCAPE, MOUSEBUTTONDOWN # pylint: disable=no-name-in-module
  10. from manager import Manager, TreeManager
  11. from sounds import Sounds
  12. # Initialize game
  13. pygame.init() # pylint: disable=no-member
  14. pygame.mixer.init()
  15. pygame.display.set_caption('python开心消消乐')
  16. pygame.mouse.set_visible(False)
  17. tree = TreeManager()
  18. m = Manager(0, 0)
  19. sound_sign = 0
  20. world_bgm = pygame.mixer.Sound(Sounds.WORLD_BGM.value)
  21. game_bgm = pygame.mixer.Sound(Sounds.GAME_BGM.value)
  22. # This improves the performance of the game
  23. get_events, update_window = pygame.event.get, pygame.display.flip
  24. while True:
  25. if m.level == 0:
  26. if sound_sign == 0:
  27. game_bgm.stop()
  28. world_bgm.play(-1)
  29. sound_sign = 1
  30. else:
  31. if sound_sign == 1:
  32. world_bgm.stop()
  33. game_bgm.play(-1)
  34. sound_sign = 0
  35. if m.level == 0:
  36. tree.draw_tree(m.energy_num, m.money)
  37. else:
  38. m.set_level_mode(m.level)
  39. sprite_group = m.draw()
  40. if m.type == 0:
  41. m.eliminate_animals()
  42. m.death_map()
  43. m.swap(sprite_group)
  44. m.judge_level()
  45. for event in get_events():
  46. if event.type == KEYDOWN:
  47. if event.key in (K_q, K_ESCAPE):
  48. sys.exit()
  49. elif event.type == QUIT:
  50. sys.exit()
  51. elif event.type == MOUSEBUTTONDOWN:
  52. mousex, mousey = event.pos
  53. if m.level == 0:
  54. tree.mouse_select(m, mousex, mousey, m.level, m.energy_num, m.money)
  55. m.mouse_select(mousex, mousey)
  56. m.mouse_image()
  57. update_window()

分享下源码地址:

SpiderMedia: 写的一些爬虫脚本 简单好上手icon-default.png?t=N7T8https://gitee.com/wuyan_666/spider-media-s.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/742127
推荐阅读
相关标签
  

闽ICP备14008679号