| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- import pygame
- from game.const import game as G
- from game.level import level
- import json
- from json import JSONDecodeError
- import logging
- from datetime import datetime
- # 配置logging基础配置并设置日志级别为INFO
- # logging.basicConfig(filename='move.log', level=logging.INFO)
- # 游戏初始化 读取配置文件
- try:
- try:
- with open(G.GAME_PATH+"\\config.json", encoding="utf-8") as f:
- cfg_json = json.load(f)
- except JSONDecodeError:
- with open(G.GAME_PATH+"\\config.json", encoding="utf-8_sig") as f:
- cfg_json = json.load(f)
- except Exception as e:
- print(e)
- with open("config.json", encoding="utf-8") as f:
- cfg_json = json.load(f)
- def move_correction_param(x):
- if x <= 0:
- return 0
- if x >= 19:
- return 19
- else:
- return round(x, 3)
- # 半身滑墙校正
- G.MOVE_CORRECTION = cfg_json.get("move_correction", 8)
- move_correction = move_correction_param(G.MOVE_CORRECTION)
- def current_grid(x_pos, y_pos):
- x = x_pos // G.GAME_SQUARE
- y = y_pos // G.GAME_SQUARE
- return int(x), int(y)
- class Updatable(pygame.sprite.Sprite):
- move_correction = move_correction_param(G.MOVE_CORRECTION)
- def __init__(self, x, y):
- super(Updatable, self).__init__()
- self.x = x
- self.y = y
- self.x_pos = x * G.GAME_SQUARE + G.HALF_GAME_SQUARE # 中心点x坐标
- self.y_pos = y * G.GAME_SQUARE + G.HALF_GAME_SQUARE # 中心点y坐标
- if hasattr(self, 'is_ghost'):
- if self.is_ghost != True:
- self.wall_walking = True # 允许穿墙
- else:
- self.wall_walking = False # 允许穿墙
- def if_hide(self):
- ret = False
- ois = level.current_level.obstacle_instances
- if (self.x, self.y) in ois.keys():
- an_obstacle = ois[(self.x, self.y)]
- if an_obstacle.obstacle_can_hide:
- self.blank_img()
- ret = True
- return ret
- def blank_img(self):
- # 清空image
- self.image = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
- def get_ruld_block(self):
- right = self.x_pos + G.HALF_GAME_SQUARE - 1
- right_grid = int(right // G.GAME_SQUARE)
- top = self.y_pos - G.HALF_GAME_SQUARE
- top_grid = int(top // G.GAME_SQUARE)
- left = self.x_pos - G.HALF_GAME_SQUARE
- left_grid = int(left // G.GAME_SQUARE)
- bottom = self.y_pos + G.HALF_GAME_SQUARE - 1
- bottom_grid = int(bottom // G.GAME_SQUARE)
- return right, right_grid, top, top_grid, left, left_grid, bottom, bottom_grid
- def slide(self, update_time_old, district_locked, orientation, slide_speed, top, bottom, left, right, block,
- top_grid, bottom_grid, left_grid, right_grid, cl):
- slide_speed = min(20, slide_speed * G.GAME_SQUARE / 1000 * (pygame.time.get_ticks() - update_time_old))
- if orientation == 1:
- self.movement_right(district_locked, slide_speed, top, bottom, right, block, top_grid, bottom_grid, cl)
- elif orientation == 2:
- self.movement_up(district_locked, slide_speed, top, left, right, block, left_grid, right_grid, cl)
- elif orientation == 3:
- self.movement_left(district_locked, slide_speed, top, bottom, left, block, top_grid, bottom_grid, cl)
- elif orientation == 4:
- self.movement_down(district_locked, slide_speed, bottom, left, right, block, left_grid, right_grid, cl)
- def movement_right(self, district_locked, speed, top, bottom, right, block, top_grid, bottom_grid, cl):
- if right // G.GAME_SQUARE != (right + speed) // G.GAME_SQUARE: # 右侧身位跨格子时
- right_screen = right + speed >= cl.map_x_pos # 右侧碰撞屏幕
- right_district = self.x_pos + speed >= cl.district_square["x2"]
- right_block = block[1][self.x + 1][self.y] # 右侧numpy障碍
- right_block_top_0 = self.y != top // G.GAME_SQUARE and block[0][self.x + 1][top_grid + 1] # 右上侧横向障碍
- if self.y != top // G.GAME_SQUARE and block[1][self.x + 1][top_grid] == 1 and block[1][self.x + 1][
- top_grid + 1] == 0:
- right_block_top_1 = True
- # logging.info(f'右上正常滑')
- else:
- 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:
- right_block_top_1 = True
- # logging.info(f'右上额外滑, (self.y+1)*G.GAME_SQUARE - self.y_pos={(self.y+1)*G.GAME_SQUARE - self.y_pos}')
- else:
- right_block_top_1 = False
- # logging.info(f'右上不滑')
- right_block_bottom_0 = self.y != bottom // G.GAME_SQUARE and block[0][self.x + 1][bottom_grid] # 右下侧横向障碍
- if self.y != bottom // G.GAME_SQUARE and block[1][self.x + 1][bottom_grid] == 1 and block[1][self.x + 1][
- bottom_grid - 1] == 0:
- right_block_bottom_1 = True
- # logging.info(f'右下正常滑')
- else:
- 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:
- right_block_bottom_1 = True
- # logging.info(f'右下额外滑, (self.y+1)*G.GAME_SQUARE - self.y_pos={(self.y+1)*G.GAME_SQUARE - self.y_pos}')
- else:
- right_block_bottom_1 = False
- # logging.info(f'右下不滑')
- right_edge = (right + speed) // G.GAME_SQUARE
- right_bomb = len(cl.get_bomb_instance(right_edge, self.y)) # 右侧糖泡
- right_bomb_top = len(cl.get_bomb_instance(right_edge, top_grid)) # 右上侧糖泡
- right_bomb_bottom = len(cl.get_bomb_instance(right_edge, bottom_grid)) # 右下侧糖泡
- if self.wall_walking and not right_screen:
- self.x_pos += speed
- cl.scroll_map()
- if district_locked and right_district and not self.wall_walking:
- self.x_pos += min(speed, max(0, right_edge * G.GAME_SQUARE - G.HALF_GAME_SQUARE - self.x_pos))
- self.collide_wall()
- self.collide_district()
- elif right_block > 0 or right_screen or right_bomb > 0 and not self.wall_walking:
- self.collide_wall()
- if right_block_top_1 > 0:
- self.y_pos = min(self.y_pos + speed, (self.y + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("R", (0, -1))
- cl.scroll_map()
- # logging.info(f'右上拓展执行')
- elif right_block_bottom_1 > 0:
- self.y_pos = max(self.y_pos - speed, (self.y - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("R", (0, 1))
- cl.scroll_map()
- # logging.info(f'右下拓展执行')
- else:
- self.x_pos += min(speed, max(0, right_edge * G.GAME_SQUARE - G.HALF_GAME_SQUARE - self.x_pos))
- self.try_push("R")
- # logging.info(f'直走执行')
- elif right_block_top_0 > 0 or right_block_top_1 > 0 or right_bomb_top > 0 and not self.wall_walking:
- self.y_pos = min(self.y_pos + speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("R", (0, -1))
- # logging.info(f'右上执行')
- elif right_block_bottom_0 > 0 or right_block_bottom_1 > 0 or right_bomb_bottom > 0 and not self.wall_walking:
- self.y_pos = max(self.y_pos - speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("R", (0, 1))
- # logging.info(f'右下执行')
- else:
- self.x_pos += speed
- cl.scroll_map()
- elif self.x_pos // G.GAME_SQUARE != (self.x_pos + speed) // G.GAME_SQUARE:
- if len(level.current_level.get_bomb_instance(self.x + 1, self.y)) == 0 or self.wall_walking:
- self.x_pos += speed
- cl.scroll_map()
- else:
- self.x_pos += speed
- cl.scroll_map()
- def movement_up(self, district_locked, speed, top, left, right, block, left_grid, right_grid, cl):
- if top // G.GAME_SQUARE != (top - speed) // G.GAME_SQUARE:
- top_screen = top - speed < 0
- top_district = self.y_pos - speed < cl.district_square["y1"]
- top_block = block[0][self.x][self.y]
- top_block_left_1 = self.x != left // G.GAME_SQUARE and block[1][left_grid + 1][self.y - 1]
- if self.x != left // G.GAME_SQUARE and block[0][left_grid][self.y] == 1 and block[0][left_grid + 1][
- self.y] == 0:
- top_block_left_0 = True
- # logging.info(f'左上正常滑')
- else:
- 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:
- top_block_left_0 = True
- # logging.info(f'左上额外滑')
- else:
- top_block_left_0 = False
- # logging.info(f'左上不滑')
- top_block_right_1 = self.x != right // G.GAME_SQUARE and block[1][right_grid][self.y - 1]
- if self.x != right // G.GAME_SQUARE and block[0][right_grid][self.y] == 1 and block[0][right_grid - 1][
- self.y] == 0:
- top_block_right_0 = True
- # logging.info(f'右上正常滑')
- else:
- 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:
- top_block_right_0 = True
- # logging.info(f'右上额外滑')
- else:
- top_block_right_0 = False
- # logging.info(f'右上不滑')
- # if self.defense == 35000:
- # 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")}')
- top_edge = (top - speed) // G.GAME_SQUARE
- top_bomb = len(cl.get_bomb_instance(self.x, top_edge))
- top_bomb_left = len(cl.get_bomb_instance(left_grid, top_edge))
- top_bomb_right = len(cl.get_bomb_instance(right_grid, top_edge))
- if self.wall_walking and not top_screen:
- self.y_pos -= speed
- cl.scroll_map()
- elif district_locked and top_district:
- self.y_pos -= min(speed, max(0, top_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE - self.y_pos))
- self.collide_wall()
- self.collide_district()
- elif top_block > 0 or top_screen or top_bomb > 0:
- self.collide_wall()
- if top_block_left_0 > 0:
- self.x_pos = min(self.x_pos + speed, (self.x + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("U", (-1, 0))
- cl.scroll_map()
- elif top_block_right_0 > 0:
- self.x_pos = max(self.x_pos - speed, (self.x - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("U", (1, 0))
- cl.scroll_map()
- else:
- self.y_pos -= min(speed, max(0, top_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE - self.y_pos))
- self.try_push("U")
- elif top_block_left_0 > 0 or top_block_left_1 > 0 or top_bomb_left > 0:
- self.x_pos = min(self.x_pos + speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("U", (-1, 0))
- elif top_block_right_0 > 0 or top_block_right_1 > 0 or top_bomb_right > 0:
- self.x_pos = max(self.x_pos - speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("U", (1, 0))
- else:
- self.y_pos -= speed
- cl.scroll_map()
- elif self.y_pos // G.GAME_SQUARE != (self.y_pos - speed) // G.GAME_SQUARE:
- if len(level.current_level.get_bomb_instance(self.x, self.y - 1)) == 0 or self.wall_walking:
- self.y_pos -= speed
- cl.scroll_map()
- else:
- self.y_pos -= speed
- cl.scroll_map()
- def movement_left(self, district_locked, speed, top, bottom, left, block, top_grid, bottom_grid, cl):
- if left // G.GAME_SQUARE != (left - speed) // G.GAME_SQUARE:
- left_screen = left - speed < 0
- left_district = self.x_pos - speed < cl.district_square["x1"]
- left_block = block[1][self.x][self.y]
- left_block_top_0 = self.y != top // G.GAME_SQUARE and block[0][self.x - 1][top_grid + 1]
- if self.y != top // G.GAME_SQUARE and block[1][self.x][top_grid] == 1 and block[1][self.x][
- top_grid + 1] == 0:
- left_block_top_1 = True
- # logging.info(f'左上正常滑')
- else:
- 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:
- left_block_top_1 = True
- # logging.info(f'左上额外滑')
- else:
- left_block_top_1 = False
- # logging.info(f'左上不滑')
- left_block_bottom_0 = self.y != bottom // G.GAME_SQUARE and block[0][self.x - 1][bottom_grid]
- if self.y != bottom // G.GAME_SQUARE and block[1][self.x][bottom_grid] == 1 and block[1][self.x][
- bottom_grid - 1] == 0:
- left_block_bottom_1 = True
- # logging.info(f'左下正常滑')
- else:
- 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:
- left_block_bottom_1 = True
- # logging.info(f'左下额外滑')
- else:
- left_block_bottom_1 = False
- # logging.info(f'左下不滑')
- left_edge = (left - speed) // G.GAME_SQUARE
- left_bomb = len(cl.get_bomb_instance(left_edge, self.y))
- left_bomb_top = len(cl.get_bomb_instance(left_edge, top_grid))
- left_bomb_bottom = len(cl.get_bomb_instance(left_edge, bottom_grid))
- if self.wall_walking and not left_screen:
- self.x_pos -= speed
- cl.scroll_map()
- if district_locked and left_district:
- self.x_pos -= min(speed, max(0, self.x_pos - (left * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
- self.collide_wall()
- self.collide_district()
- elif left_block > 0 or left_screen or left_bomb > 0:
- self.collide_wall()
- if left_block_top_1 > 0:
- self.y_pos = min(self.y_pos + speed, (self.y + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("L", (0, -1))
- cl.scroll_map()
- # logging.info(f'左上拓展')
- elif left_block_bottom_1 > 0:
- self.y_pos = max(self.y_pos - speed, (self.y - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("L", (0, 1))
- cl.scroll_map()
- # logging.info(f'左下拓展')
- else:
- self.x_pos -= min(speed, max(0, self.x_pos - (left * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
- self.try_push("L")
- # logging.info(f'直走')
- elif left_block_top_1 > 0 or left_block_top_0 > 0 or left_bomb_top > 0:
- self.y_pos = min(self.y_pos + speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("L", (0, -1))
- elif left_block_bottom_1 > 0 or left_block_bottom_0 > 0 or left_bomb_bottom > 0:
- self.y_pos = max(self.y_pos - speed, self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("L", (0, 1))
- else:
- self.x_pos -= speed
- cl.scroll_map()
- elif self.x_pos // G.GAME_SQUARE != (self.x_pos - speed) // G.GAME_SQUARE:
- if len(level.current_level.get_bomb_instance(self.x - 1, self.y)) == 0 or self.wall_walking:
- self.x_pos -= speed
- cl.scroll_map()
- else:
- self.x_pos -= speed
- cl.scroll_map()
- def movement_down(self, district_locked, speed, bottom, left, right, block, left_grid, right_grid, cl):
- if bottom // G.GAME_SQUARE != (bottom + speed) // G.GAME_SQUARE:
- bottom_block = block[0][self.x][self.y + 1]
- bottom_district = self.y_pos + speed >= cl.district_square["y2"]
- bottom_block_right_1 = self.x != right // G.GAME_SQUARE and block[1][int(right // G.GAME_SQUARE)][
- self.y + 1]
- if self.x != right // G.GAME_SQUARE and block[0][right_grid][self.y + 1] == 1 and block[0][right_grid - 1][
- self.y + 1] == 0:
- bottom_block_right_0 = True
- # logging.info(f'右下正常滑')
- else:
- 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:
- bottom_block_right_0 = True
- # logging.info(f'右下额外滑')
- else:
- bottom_block_right_0 = False
- # logging.info(f'右下不滑')
- bottom_block_left_1 = self.x != left // G.GAME_SQUARE and block[1][int(left // G.GAME_SQUARE) + 1][
- self.y + 1]
- if self.x != left // G.GAME_SQUARE and block[0][left_grid][self.y + 1] == 1 and block[0][left_grid + 1][
- self.y + 1] == 0:
- bottom_block_left_0 = True
- # logging.info(f'左下正常滑')
- else:
- 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:
- bottom_block_left_0 = True
- # logging.info(f'左下额外滑')
- else:
- bottom_block_left_0 = False
- # logging.info(f'左下不滑')
- bottom_screen = bottom + speed >= cl.map_y_pos
- bottom_edge = (bottom + speed) // G.GAME_SQUARE
- bottom_bomb = len(cl.get_bomb_instance(self.x, bottom_edge))
- bottom_bomb_left = len(cl.get_bomb_instance(left_grid, bottom_edge))
- bottom_bomb_right = len(cl.get_bomb_instance(right_grid, bottom_edge))
- if self.wall_walking and not bottom_screen:
- self.y_pos += speed
- cl.scroll_map()
- elif district_locked and bottom_district:
- self.y_pos += min(speed, max(0, self.y_pos - (bottom_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
- self.collide_wall()
- self.collide_district()
- elif bottom_block > 0 or bottom_screen or bottom_bomb > 0:
- self.collide_wall()
- if bottom_block_left_0 > 0:
- self.x_pos = min(self.x_pos + speed, (self.x + 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("D", (-1, 0))
- cl.scroll_map()
- elif bottom_block_right_0 > 0:
- self.x_pos = max(self.x_pos - speed, (self.x - 1) * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("D", (1, 0))
- cl.scroll_map()
- else:
- self.y_pos += min(speed, max(0, self.y_pos - (bottom_edge * G.GAME_SQUARE + G.HALF_GAME_SQUARE)))
- self.try_push("D")
- elif bottom_block_right_0 > 0 or bottom_block_right_1 > 0 or bottom_bomb_right > 0:
- self.x_pos = max(self.x_pos - speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("D", (1, 0))
- elif bottom_block_left_0 > 0 or bottom_block_left_1 > 0 or bottom_bomb_left > 0:
- self.x_pos = min(self.x_pos + speed, self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE)
- self.try_push("D", (-1, 0))
- else:
- self.y_pos += speed
- cl.scroll_map()
- elif self.y_pos // G.GAME_SQUARE != (self.y_pos + speed) // G.GAME_SQUARE:
- if len(level.current_level.get_bomb_instance(self.x, self.y + 1)) == 0 or self.wall_walking:
- self.y_pos += speed
- cl.scroll_map()
- else:
- self.y_pos += speed
- cl.scroll_map()
- def collide_wall(self):
- pass
- def collide_district(self):
- pass
- def try_push(self, direction, offset=(0, 0)):
- pass
- def draw(self, screen: pygame.Surface):
- screen.blit(self.image, self.rect)
- def get_y(self):
- return self.y
|