【发布时间】:2021-09-15 04:36:18
【问题描述】:
我目前正在开发一个平台游戏,它依赖于对象(以平台的形式)作为它们的碰撞,以防止玩家退出窗口。 这是我与平台/块碰撞的代码:
#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
但是,这看起来不专业,我想添加代码,它引入了一个不可见的屏障,阻止玩家离开屏幕。 我尝试了不同的方法,但目前不确定,任何建议都将不胜感激。
【问题讨论】:
-
为什么要标记
tkinter?是什么让您认为代码看起来不专业?唯一突出的是大缩进,您应该尝试限制缩进,以便于阅读,而不是产生语法错误或与之相关的问题(例如在 if 语句之外有代码,因为你有许多缩进很难跟踪)(我个人会在这种情况下对矩形进行投影,然后将该矩形用作.colliderect的参数,而不是像这样的四个数字)
标签: python pygame sprite boundaries