updatable.py 21 KB

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