如何解决如何计算pyglet中的一个形状是否在另一个下面
我正在使用 pyglet 制作一个无尽的跑步者。
我正在尝试确定玩家下方地板的高度。每层楼都是一个班级:
class Floor():
def __init__(self,width,height,offset,batch=None):
self.width = width
self.height = height
self.floor = shapes.Rectangle(offset,self.width,self.height,color=(9,197,0),batch=batch)
def move(self,amount):
self.floor.x -= amount
所以我在类中添加了一个方法来计算地板是否在玩家下方(玩家也是一个形状):
def under_player(self,player):
if player.x <= self.floor.x and self.floor.x + self.width >= player.x:
return True
return False
现在为了计算玩家所在的楼层高度,我做了这个函数(floors 是一个 Floor
类的列表):
def calc_under_floor():
global floors,player
for floor in floors:
if floor.under_player(player):
return floor.height
return -100
问题是under_player
中检查玩家是否在该楼层下的条件错误。我找不到有效的解决方案。有什么办法可以解决这个问题吗?
解决方法
我希望我找到了解决方案。至少它有效:)
def under_player(self,player):
if player.x + 50 >= self.floor.x and player.x <= self.floor.x + self.floor.width:
return True
return False
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。