def checkCollision(self):
for entity in self.Entities:
ox = entity.x
oy = entity.y
for block in self.Blocks:
if block.y < entity.y and entity.y < block.y + block.size:
if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x):
print ("Hit Bottom")
entity.tpEntity(ox, oy+0.5)
elif entity.y < block.y and block.y < entity.y+entity.height:
if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x):
print ("Hit Top")
entity.tpEntity(ox, oy-self.gravity)
elif entity.x < block.x and block.x < entity.x+entity.width:
if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height):
print("Hit Left")
entity.tpEntity(ox-0.5,oy)
elif entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x:
if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height):
print("Hit Right")
entity.tpEntity(ox+0.5,oy)我有一个奇怪的错误,如果我走到墙上,它总是注册它,好像它在上面。有人能向我解释一下为什么会发生这种情况吗?我应该如何解决这个问题?谢谢您:)
编辑: self.gravity变量是行星/地图上的重力,因为它在每个行星/地图上应该是不同的。默认情况下(此处)为0.2。
发布于 2016-04-07 04:02:58
我假设实体和块是类?如果使用pygame.Rect对象设置每个对象,并在实体移动时对其进行更新,则可以通过以下操作简化此操作:
def checkCollision(self):
for entity in self.Entities:
ox = entity.x
oy = entity.y
for block in self.Blocks:
if entity.rect.colliderect(block.rect):然后测试x和y的位置来确定反射。
https://stackoverflow.com/questions/36453773
复制相似问题