我目前正在开发一个平台游戏,它依赖于对象(以平台的形式)作为它们的碰撞,以防止玩家退出窗口。这是我与平台/块碰撞的代码:
#check for collision
self.in_air = True
for tile in world.tile_list:
#collison x
if tile[1].colliderect(self.rect.x + dx,self.rect.y , self.width, self.height):
dx = 0
# collision y
if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
# below ground?
if self.vel_y < 0:
dy = tile[1].bottom - self.rect.top
self.vel_y = 0
# above ground?
elif self.vel_y >= 0:
dy = tile[1].top - self.rect.bottom
self.vel_y = 0
self.in_air = False
但是,这看起来不专业,我想添加代码,它引入了一个不可见的屏障,阻止玩家离开屏幕。我尝试了不同的方法,但目前不确定,任何建议将不胜感激。
