updatable.py 21 KB

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