updatable.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. import pygame
  2. from game.const import game as G
  3. from game.level import level
  4. import json
  5. from json import JSONDecodeError
  6. import logging
  7. from datetime import datetime
  8. # 配置logging基础配置并设置日志级别为INFO
  9. # logging.basicConfig(filename='move.log', level=logging.INFO)
  10. # 游戏初始化 读取配置文件
  11. try:
  12. try:
  13. with open(G.GAME_PATH+"\\config.json", encoding="utf-8") as f:
  14. cfg_json = json.load(f)
  15. except JSONDecodeError:
  16. with open(G.GAME_PATH+"\\config.json", encoding="utf-8_sig") as f:
  17. cfg_json = json.load(f)
  18. except Exception as e:
  19. print(e)
  20. with open("config.json", encoding="utf-8") as f:
  21. cfg_json = json.load(f)
  22. def move_correction_param(x):
  23. if x <= 0:
  24. return 0
  25. if x >= 19:
  26. return 19
  27. else:
  28. return round(x, 3)
  29. # 半身滑墙校正
  30. G.MOVE_CORRECTION = cfg_json.get("move_correction", 8)
  31. move_correction = move_correction_param(G.MOVE_CORRECTION)
  32. def current_grid(x_pos, y_pos):
  33. x = x_pos // G.GAME_SQUARE
  34. y = y_pos // G.GAME_SQUARE
  35. return int(x), int(y)
  36. class Updatable(pygame.sprite.Sprite):
  37. move_correction = move_correction_param(G.MOVE_CORRECTION)
  38. def __init__(self, x, y):
  39. super(Updatable, self).__init__()
  40. self.x = x
  41. self.y = y
  42. self.x_pos = x * G.GAME_SQUARE + G.HALF_GAME_SQUARE # 中心点x坐标
  43. self.y_pos = y * G.GAME_SQUARE + G.HALF_GAME_SQUARE # 中心点y坐标
  44. if hasattr(self, 'is_ghost'):
  45. if self.is_ghost != True:
  46. self.wall_walking = True # 允许穿墙
  47. else:
  48. self.wall_walking = False # 允许穿墙
  49. def if_hide(self):
  50. ret = False
  51. ois = level.current_level.obstacle_instances
  52. if (self.x, self.y) in ois.keys():
  53. an_obstacle = ois[(self.x, self.y)]
  54. if an_obstacle.obstacle_can_hide:
  55. self.blank_img()
  56. ret = True
  57. return ret
  58. def blank_img(self):
  59. # 清空image
  60. self.image = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
  61. def get_ruld_block(self):
  62. right = self.x_pos + G.HALF_GAME_SQUARE - 1
  63. right_grid = int(right // G.GAME_SQUARE)
  64. top = self.y_pos - G.HALF_GAME_SQUARE
  65. top_grid = int(top // G.GAME_SQUARE)
  66. left = self.x_pos - G.HALF_GAME_SQUARE
  67. left_grid = int(left // G.GAME_SQUARE)
  68. bottom = self.y_pos + G.HALF_GAME_SQUARE - 1
  69. bottom_grid = int(bottom // G.GAME_SQUARE)
  70. return right, right_grid, top, top_grid, left, left_grid, bottom, bottom_grid
  71. def slide(self, update_time_old, district_locked, orientation, slide_speed, top, bottom, left, right, block,
  72. top_grid, bottom_grid, left_grid, right_grid, cl):
  73. slide_speed = min(20, slide_speed * G.GAME_SQUARE / 1000 * (pygame.time.get_ticks() - update_time_old))
  74. if orientation == 1:
  75. self.movement_right(district_locked, slide_speed, top, bottom, right, block, top_grid, bottom_grid, cl)
  76. elif orientation == 2:
  77. self.movement_up(district_locked, slide_speed, top, left, right, block, left_grid, right_grid, cl)
  78. elif orientation == 3:
  79. self.movement_left(district_locked, slide_speed, top, bottom, left, block, top_grid, bottom_grid, cl)
  80. elif orientation == 4:
  81. self.movement_down(district_locked, slide_speed, bottom, left, right, block, left_grid, right_grid, cl)
  82. def movement_right(self, district_locked, speed, top, bottom, right, block, top_grid, bottom_grid, cl):
  83. if right // G.GAME_SQUARE != (right + speed) // G.GAME_SQUARE: # 右侧身位跨格子时
  84. right_screen = right + speed >= cl.map_x_pos # 右侧碰撞屏幕
  85. right_district = self.x_pos + speed >= cl.district_square["x2"]
  86. right_block = block[1][self.x + 1][self.y] # 右侧numpy障碍
  87. right_block_top_0 = self.y != top // G.GAME_SQUARE and block[0][self.x + 1][top_grid + 1] # 右上侧横向障碍
  88. if self.y != top // G.GAME_SQUARE and block[1][self.x + 1][top_grid] == 1 and block[1][self.x + 1][
  89. top_grid + 1] == 0:
  90. right_block_top_1 = True
  91. # logging.info(f'右上正常滑')
  92. else:
  93. if move_correction >= (self.y + 1) * G.GAME_SQUARE - self.y_pos >= 0 and block[1][self.x + 1][self.y] == 1 and block[1][self.x + 1][self.y+1] == 0:
  94. right_block_top_1 = True
  95. # logging.info(f'右上额外滑, (self.y+1)*G.GAME_SQUARE - self.y_pos={(self.y+1)*G.GAME_SQUARE - self.y_pos}')
  96. else:
  97. right_block_top_1 = False
  98. # logging.info(f'右上不滑')
  99. right_block_bottom_0 = self.y != bottom // G.GAME_SQUARE and block[0][self.x + 1][bottom_grid] # 右下侧横向障碍
  100. if self.y != bottom // G.GAME_SQUARE and block[1][self.x + 1][bottom_grid] == 1 and block[1][self.x + 1][
  101. bottom_grid - 1] == 0:
  102. right_block_bottom_1 = True
  103. # logging.info(f'右下正常滑')
  104. else:
  105. if move_correction >= self.y_pos - self.y * G.GAME_SQUARE >= 0 and block[1][self.x + 1][self.y] == 1 and block[1][self.x + 1][self.y-1] == 0:
  106. right_block_bottom_1 = True
  107. # logging.info(f'右下额外滑, (self.y+1)*G.GAME_SQUARE - self.y_pos={(self.y+1)*G.GAME_SQUARE - self.y_pos}')
  108. else:
  109. right_block_bottom_1 = False
  110. # logging.info(f'右下不滑')
  111. right_edge = (right + speed) // G.GAME_SQUARE
  112. right_bomb = len(cl.get_bomb_instance(right_edge, self.y)) # 右侧糖泡
  113. right_bomb_top = len(cl.get_bomb_instance(right_edge, top_grid)) # 右上侧糖泡
  114. right_bomb_bottom = len(cl.get_bomb_instance(right_edge, bottom_grid)) # 右下侧糖泡
  115. if self.wall_walking and not right_screen:
  116. self.x_pos += speed
  117. cl.scroll_map()
  118. if district_locked and right_district and not self.wall_walking:
  119. self.x_pos += min(speed, max(0, right_edge * G.GAME_SQUARE - G.HALF_GAME_SQUARE - self.x_pos))
  120. self.collide_wall()
  121. self.collide_district()
  122. elif right_block > 0 or right_screen or right_bomb > 0 and not self.wall_walking:
  123. self.collide_wall()
  124. if right_block_top_1 > 0:
  125. self.y_pos = min(self.y_pos + speed, (self.y + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  126. self.try_push("R", (0, -1))
  127. cl.scroll_map()
  128. # logging.info(f'右上拓展执行')
  129. elif right_block_bottom_1 > 0:
  130. self.y_pos = max(self.y_pos - speed, (self.y - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  131. self.try_push("R", (0, 1))
  132. cl.scroll_map()
  133. # logging.info(f'右下拓展执行')
  134. else:
  135. self.x_pos += min(speed, max(0, right_edge * G.GAME_SQUARE - G.HALF_GAME_SQUARE - self.x_pos))
  136. self.try_push("R")
  137. # logging.info(f'直走执行')
  138. elif right_block_top_0 > 0 or right_block_top_1 > 0 or right_bomb_top > 0 and not self.wall_walking:
  139. self.y_pos = min(self.y_pos + speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  140. self.try_push("R", (0, -1))
  141. # logging.info(f'右上执行')
  142. elif right_block_bottom_0 > 0 or right_block_bottom_1 > 0 or right_bomb_bottom > 0 and not self.wall_walking:
  143. self.y_pos = max(self.y_pos - speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  144. self.try_push("R", (0, 1))
  145. # logging.info(f'右下执行')
  146. else:
  147. self.x_pos += speed
  148. cl.scroll_map()
  149. elif self.x_pos // G.GAME_SQUARE != (self.x_pos + speed) // G.GAME_SQUARE:
  150. if len(level.current_level.get_bomb_instance(self.x + 1, self.y)) == 0 or self.wall_walking:
  151. self.x_pos += speed
  152. cl.scroll_map()
  153. else:
  154. self.x_pos += speed
  155. cl.scroll_map()
  156. def movement_up(self, district_locked, speed, top, left, right, block, left_grid, right_grid, cl):
  157. if top // G.GAME_SQUARE != (top - speed) // G.GAME_SQUARE:
  158. top_screen = top - speed < 0
  159. top_district = self.y_pos - speed < cl.district_square["y1"]
  160. top_block = block[0][self.x][self.y]
  161. top_block_left_1 = self.x != left // G.GAME_SQUARE and block[1][left_grid + 1][self.y - 1]
  162. if self.x != left // G.GAME_SQUARE and block[0][left_grid][self.y] == 1 and block[0][left_grid + 1][
  163. self.y] == 0:
  164. top_block_left_0 = True
  165. # logging.info(f'左上正常滑')
  166. else:
  167. if move_correction >= (self.x + 1) * G.GAME_SQUARE - self.x_pos >= 0 and block[0][self.x][self.y] == 1 and block[0][self.x+1][self.y] == 0:
  168. top_block_left_0 = True
  169. # logging.info(f'左上额外滑')
  170. else:
  171. top_block_left_0 = False
  172. # logging.info(f'左上不滑')
  173. top_block_right_1 = self.x != right // G.GAME_SQUARE and block[1][right_grid][self.y - 1]
  174. if self.x != right // G.GAME_SQUARE and block[0][right_grid][self.y] == 1 and block[0][right_grid - 1][
  175. self.y] == 0:
  176. top_block_right_0 = True
  177. # logging.info(f'右上正常滑')
  178. else:
  179. if move_correction >= self.x_pos - self.x * G.GAME_SQUARE >= 0 and block[0][self.x][self.y] == 1 and block[0][self.x-1][self.y] == 0:
  180. top_block_right_0 = True
  181. # logging.info(f'右上额外滑')
  182. else:
  183. top_block_right_0 = False
  184. # logging.info(f'右上不滑')
  185. # if self.defense == 35000:
  186. # logging.info(f'left={left},right={right},centre(x,y)={(self.x, self.y)},右上0={top_block_right_0},右上1={top_block_right_1},左上0={top_block_left_0}, 左上1={top_block_left_1}, Current time={datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
  187. top_edge = (top - speed) // G.GAME_SQUARE
  188. top_bomb = len(cl.get_bomb_instance(self.x, top_edge))
  189. top_bomb_left = len(cl.get_bomb_instance(left_grid, top_edge))
  190. top_bomb_right = len(cl.get_bomb_instance(right_grid, top_edge))
  191. if self.wall_walking and not top_screen:
  192. self.y_pos -= speed
  193. cl.scroll_map()
  194. elif district_locked and top_district:
  195. self.y_pos -= min(speed, max(0, top_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE - self.y_pos))
  196. self.collide_wall()
  197. self.collide_district()
  198. elif top_block > 0 or top_screen or top_bomb > 0:
  199. self.collide_wall()
  200. if top_block_left_0 > 0:
  201. self.x_pos = min(self.x_pos + speed, (self.x + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  202. self.try_push("U", (-1, 0))
  203. cl.scroll_map()
  204. elif top_block_right_0 > 0:
  205. self.x_pos = max(self.x_pos - speed, (self.x - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  206. self.try_push("U", (1, 0))
  207. cl.scroll_map()
  208. else:
  209. self.y_pos -= min(speed, max(0, top_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE - self.y_pos))
  210. self.try_push("U")
  211. elif top_block_left_0 > 0 or top_block_left_1 > 0 or top_bomb_left > 0:
  212. self.x_pos = min(self.x_pos + speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  213. self.try_push("U", (-1, 0))
  214. elif top_block_right_0 > 0 or top_block_right_1 > 0 or top_bomb_right > 0:
  215. self.x_pos = max(self.x_pos - speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  216. self.try_push("U", (1, 0))
  217. else:
  218. self.y_pos -= speed
  219. cl.scroll_map()
  220. elif self.y_pos // G.GAME_SQUARE != (self.y_pos - speed) // G.GAME_SQUARE:
  221. if len(level.current_level.get_bomb_instance(self.x, self.y - 1)) == 0 or self.wall_walking:
  222. self.y_pos -= speed
  223. cl.scroll_map()
  224. else:
  225. self.y_pos -= speed
  226. cl.scroll_map()
  227. def movement_left(self, district_locked, speed, top, bottom, left, block, top_grid, bottom_grid, cl):
  228. if left // G.GAME_SQUARE != (left - speed) // G.GAME_SQUARE:
  229. left_screen = left - speed < 0
  230. left_district = self.x_pos - speed < cl.district_square["x1"]
  231. left_block = block[1][self.x][self.y]
  232. left_block_top_0 = self.y != top // G.GAME_SQUARE and block[0][self.x - 1][top_grid + 1]
  233. if self.y != top // G.GAME_SQUARE and block[1][self.x][top_grid] == 1 and block[1][self.x][
  234. top_grid + 1] == 0:
  235. left_block_top_1 = True
  236. # logging.info(f'左上正常滑')
  237. else:
  238. if move_correction >= (self.y + 1) * G.GAME_SQUARE - self.y_pos >= 0 and block[1][self.x][self.y] == 1 and block[1][self.x][self.y+1] == 0:
  239. left_block_top_1 = True
  240. # logging.info(f'左上额外滑')
  241. else:
  242. left_block_top_1 = False
  243. # logging.info(f'左上不滑')
  244. left_block_bottom_0 = self.y != bottom // G.GAME_SQUARE and block[0][self.x - 1][bottom_grid]
  245. if self.y != bottom // G.GAME_SQUARE and block[1][self.x][bottom_grid] == 1 and block[1][self.x][
  246. bottom_grid - 1] == 0:
  247. left_block_bottom_1 = True
  248. # logging.info(f'左下正常滑')
  249. else:
  250. if move_correction >= self.y_pos - self.y * G.GAME_SQUARE >= 0 and block[1][self.x][self.y] == 1 and block[1][self.x][self.y-1] == 0:
  251. left_block_bottom_1 = True
  252. # logging.info(f'左下额外滑')
  253. else:
  254. left_block_bottom_1 = False
  255. # logging.info(f'左下不滑')
  256. left_edge = (left - speed) // G.GAME_SQUARE
  257. left_bomb = len(cl.get_bomb_instance(left_edge, self.y))
  258. left_bomb_top = len(cl.get_bomb_instance(left_edge, top_grid))
  259. left_bomb_bottom = len(cl.get_bomb_instance(left_edge, bottom_grid))
  260. if self.wall_walking and not left_screen:
  261. self.x_pos -= speed
  262. cl.scroll_map()
  263. if district_locked and left_district:
  264. self.x_pos -= min(speed, max(0, self.x_pos - (left * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
  265. self.collide_wall()
  266. self.collide_district()
  267. elif left_block > 0 or left_screen or left_bomb > 0:
  268. self.collide_wall()
  269. if left_block_top_1 > 0:
  270. self.y_pos = min(self.y_pos + speed, (self.y + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  271. self.try_push("L", (0, -1))
  272. cl.scroll_map()
  273. # logging.info(f'左上拓展')
  274. elif left_block_bottom_1 > 0:
  275. self.y_pos = max(self.y_pos - speed, (self.y - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  276. self.try_push("L", (0, 1))
  277. cl.scroll_map()
  278. # logging.info(f'左下拓展')
  279. else:
  280. self.x_pos -= min(speed, max(0, self.x_pos - (left * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
  281. self.try_push("L")
  282. # logging.info(f'直走')
  283. elif left_block_top_1 > 0 or left_block_top_0 > 0 or left_bomb_top > 0:
  284. self.y_pos = min(self.y_pos + speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  285. self.try_push("L", (0, -1))
  286. elif left_block_bottom_1 > 0 or left_block_bottom_0 > 0 or left_bomb_bottom > 0:
  287. self.y_pos = max(self.y_pos - speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  288. self.try_push("L", (0, 1))
  289. else:
  290. self.x_pos -= speed
  291. cl.scroll_map()
  292. elif self.x_pos // G.GAME_SQUARE != (self.x_pos - speed) // G.GAME_SQUARE:
  293. if len(level.current_level.get_bomb_instance(self.x - 1, self.y)) == 0 or self.wall_walking:
  294. self.x_pos -= speed
  295. cl.scroll_map()
  296. else:
  297. self.x_pos -= speed
  298. cl.scroll_map()
  299. def movement_down(self, district_locked, speed, bottom, left, right, block, left_grid, right_grid, cl):
  300. if bottom // G.GAME_SQUARE != (bottom + speed) // G.GAME_SQUARE:
  301. bottom_block = block[0][self.x][self.y + 1]
  302. bottom_district = self.y_pos + speed >= cl.district_square["y2"]
  303. bottom_block_right_1 = self.x != right // G.GAME_SQUARE and block[1][int(right // G.GAME_SQUARE)][
  304. self.y + 1]
  305. if self.x != right // G.GAME_SQUARE and block[0][right_grid][self.y + 1] == 1 and block[0][right_grid - 1][
  306. self.y + 1] == 0:
  307. bottom_block_right_0 = True
  308. # logging.info(f'右下正常滑')
  309. else:
  310. if move_correction >= self.x_pos - self.x * G.GAME_SQUARE >= 0 and block[0][self.x][self.y + 1] == 1 and block[0][self.x-1][self.y+1] == 0:
  311. bottom_block_right_0 = True
  312. # logging.info(f'右下额外滑')
  313. else:
  314. bottom_block_right_0 = False
  315. # logging.info(f'右下不滑')
  316. bottom_block_left_1 = self.x != left // G.GAME_SQUARE and block[1][int(left // G.GAME_SQUARE) + 1][
  317. self.y + 1]
  318. if self.x != left // G.GAME_SQUARE and block[0][left_grid][self.y + 1] == 1 and block[0][left_grid + 1][
  319. self.y + 1] == 0:
  320. bottom_block_left_0 = True
  321. # logging.info(f'左下正常滑')
  322. else:
  323. if move_correction >= (self.x + 1) * G.GAME_SQUARE - self.x_pos >= 0 and block[0][self.x][self.y + 1] == 1 and block[0][self.x+1][self.y + 1] == 0:
  324. bottom_block_left_0 = True
  325. # logging.info(f'左下额外滑')
  326. else:
  327. bottom_block_left_0 = False
  328. # logging.info(f'左下不滑')
  329. bottom_screen = bottom + speed >= cl.map_y_pos
  330. bottom_edge = (bottom + speed) // G.GAME_SQUARE
  331. bottom_bomb = len(cl.get_bomb_instance(self.x, bottom_edge))
  332. bottom_bomb_left = len(cl.get_bomb_instance(left_grid, bottom_edge))
  333. bottom_bomb_right = len(cl.get_bomb_instance(right_grid, bottom_edge))
  334. if self.wall_walking and not bottom_screen:
  335. self.y_pos += speed
  336. cl.scroll_map()
  337. elif district_locked and bottom_district:
  338. self.y_pos += min(speed, max(0, self.y_pos - (bottom_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
  339. self.collide_wall()
  340. self.collide_district()
  341. elif bottom_block > 0 or bottom_screen or bottom_bomb > 0:
  342. self.collide_wall()
  343. if bottom_block_left_0 > 0:
  344. self.x_pos = min(self.x_pos + speed, (self.x + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  345. self.try_push("D", (-1, 0))
  346. cl.scroll_map()
  347. elif bottom_block_right_0 > 0:
  348. self.x_pos = max(self.x_pos - speed, (self.x - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  349. self.try_push("D", (1, 0))
  350. cl.scroll_map()
  351. else:
  352. self.y_pos += min(speed, max(0, self.y_pos - (bottom_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
  353. self.try_push("D")
  354. elif bottom_block_right_0 > 0 or bottom_block_right_1 > 0 or bottom_bomb_right > 0:
  355. self.x_pos = max(self.x_pos - speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  356. self.try_push("D", (1, 0))
  357. elif bottom_block_left_0 > 0 or bottom_block_left_1 > 0 or bottom_bomb_left > 0:
  358. self.x_pos = min(self.x_pos + speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
  359. self.try_push("D", (-1, 0))
  360. else:
  361. self.y_pos += speed
  362. cl.scroll_map()
  363. elif self.y_pos // G.GAME_SQUARE != (self.y_pos + speed) // G.GAME_SQUARE:
  364. if len(level.current_level.get_bomb_instance(self.x, self.y + 1)) == 0 or self.wall_walking:
  365. self.y_pos += speed
  366. cl.scroll_map()
  367. else:
  368. self.y_pos += speed
  369. cl.scroll_map()
  370. def collide_wall(self):
  371. pass
  372. def collide_district(self):
  373. pass
  374. def try_push(self, direction, offset=(0, 0)):
  375. pass
  376. def draw(self, screen: pygame.Surface):
  377. screen.blit(self.image, self.rect)
  378. def get_y(self):
  379. return self.y