diff --git a/cpgzh/actor.py b/cpgzh/actor.py index d90da7d28c7f5443f06c476a24d73a76bd72a664..685ce7b87407311295c5d9f63b43a263e3ff5cac 100755 --- a/cpgzh/actor.py +++ b/cpgzh/actor.py @@ -53,11 +53,9 @@ class Actor(Actor): images = loadimgs(dirname) return images - def distance_to(self, actor): + def distance_to(self, actor: Actor): "计算到另一个角色的距离" - dx = actor.x - self.x - dy = actor.y - self.y - return math.sqrt(dx ** 2 + dy ** 2) + return math.dist(actor.pos, self.pos) def direction_to(self, actor): "计算面向另一个角色的方向" @@ -175,10 +173,9 @@ class Actor(Actor): # 如果任务过期就执行并删除 for task_count in list(self.tasks): if task_count <= now: - for task_id in self.tasks[task_count]: - self.tasks[task_count][task_id]() + for task_id, func in self.tasks.pop(task_count).items(): + func() # print(f'任务{task_id}执行完毕') - self.tasks.pop(task_count) else: break @@ -297,12 +294,12 @@ class Actor(Actor): self._surf, (int(size[0] * self._scale_x), int(size[1] * self._scale_y)) ) - if self._flip_x: - self._surf = pygame.transform.flip(self._surf, True, False) - if self._flip_y: - self._surf = pygame.transform.flip(self._surf, False, True) + if self._flip_x or self._flip_y: + self._surf = pygame.transform.flip( + self._surf, self._flip_x, self._flip_y) self._surf = pygame.transform.rotate(self._surf, self._angle) + # 应该改成用pygame.transform.rotozoom合并旋转和缩放操作 self.width, self.height = self._surf.get_size() w, h = self._orig_surf.get_size() @@ -316,8 +313,7 @@ class Actor(Actor): def collidepoint_pixel(self, x, y=0): "检测碰撞到某个像素,像素级精确检测" if isinstance(x, tuple): - y = x[1] - x = x[0] + x, y = x if self._mask == None: self._mask = pygame.mask.from_surface(self._surf) @@ -345,18 +341,14 @@ class Actor(Actor): def collidelist_pixel(self, actors): "检测碰撞角色列表,返回碰撞到的角色的索引,没碰到返回None,像素级精确检测" - for i in range(len(actors)): - if self.collide_pixel(actors[i]): + for i, actor in enumerate(actors): + if self.collide_pixel(actor): return i return None def collidelistall_pixel(self, actors): "检测碰撞角色列表,返回值是碰撞到的角色,返回一个列表,如果列表为空说明没碰到,像素级精确检测" - collided = [] - for i in range(len(actors)): - if self.collide_pixel(actors[i]): - collided.append(i) - return collided + return list(filter(self.collide_pixel, actors)) def obb_collidepoints(self, actors): "检测多个角色碰撞,旋转了rect,使得rect贴合角色" @@ -367,29 +359,21 @@ class Actor(Actor): half_width = width / 2 half_height = height / 2 - i = 0 - for actor in actors: + for i, actor in enumerate(actors): tx = actor.x - self.x ty = actor.y - self.y rx = tx * costheta - ty * sintheta ry = ty * costheta + tx * sintheta - if ( - rx > -half_width - and rx < half_width - and ry > -half_height - and ry < half_height - ): + if -half_width < rx < half_width and -half_height < ry < half_height: return i - i += 1 return -1 def obb_collidepoint(self, x, y=0): "检测碰撞一个点,旋转了rect,使得rect贴合角色" if isinstance(x, tuple): - y = x[1] - x = x[0] + x, y = x angle = math.radians(self._angle) costheta = math.cos(angle) sintheta = math.sin(angle) @@ -402,42 +386,28 @@ class Actor(Actor): rx = tx * costheta - ty * sintheta ry = ty * costheta + tx * sintheta - if ( - rx > -half_width - and rx < half_width - and ry > -half_height - and ry < half_height - ): - return True - - return False + return -half_width < rx < half_width and -half_height < ry < half_height def circle_collidepoints(self, radius, actors): "检测碰撞一堆点,将角色变成圆形区域,适合于圆形角色的碰撞检测" rSquare = radius ** 2 - i = 0 - for actor in actors: + for i, actor in enumerate(actors): dSquare = (actor.x - self.x) ** 2 + (actor.y - self.y) ** 2 if dSquare < rSquare: return i - i += 1 return -1 def circle_collidepoint(self, radius, x, y=0): "检测碰撞一个点,将角色变成圆形区域,适合于圆形角色的碰撞检测" if isinstance(x, tuple): - y = x[1] - x = x[0] + x, y = x rSquare = radius ** 2 dSquare = (x - self.x) ** 2 + (y - self.y) ** 2 - if dSquare < rSquare: - return True - - return False + return dSquare < rSquare def draw(self): "绘图" @@ -463,9 +433,9 @@ class Actor(Actor): pos传入一个坐标则面向这个坐标\n pos传入一个Actor则面向这个演员\n ''' - if type(pos) == list or type(pos) == tuple: + if isinstance(pos, (list, tuple)): x, y = pos[0], pos[1] - elif type(pos) == Actor: + elif isinstance(pos, Actor): x, y = pos.pos else: x, y = mouse.get_pos()