updatable.py 21 KB

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