如何使用向量和矩形让敌人跟随 pygame 中的玩家?

问题描述

类猴子(对象):

def init(self,x,y,width,height):

 self.walkRight = [ list of images]

 self.walkLeft = [list of images]

 self.walkDown = [list of images]

 self.walkUp = [list of images]

#get rect 使用列表理解为每个图像。我为每个箭头键执行此操作 4 次

 self.rect_Right = [img.get_rect() for img in 
 self.walkRight] #repeat for other arrows

类敌人(对象):

(与有敌人图像的玩家基本相同)

 def move_towards_player(self,monkey):
 dirvect = pygame.math.Vector2(monkey.rect.x - self.rect.x,monkey.rect.y - self.rect.y)

 dirvect.normalize()

 dirvect.scale_to_length(self.speed)

 self.rect.move_ip(dirvect)

我遇到了两个问题。第一,由于我将多个图像上传到列表中,我不知道我是否正确使用了 get_rect()。我目前正在使用列表理解。我如何获得 rect.x 和 rect。你的职位?我如何获得中心位置?

第二,我不知道如何让 move_towards_player 开始工作。我将播放器类传递给函数,但我认为它无法读取矩形。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)