查找 python 中对象之间的距离 - 不工作
Find distance between objects in python - Not working
我目前正在 Python 上开发一款简单的飞鸟类游戏。出于某种原因,当管道和鸟接触时,碰撞代码不起作用。
def collision():
global distanceDown, distanceUp
distanceUp = math.sqrt(math.pow(pipeUpX - birdX, 2) + math.pow(pipeUpY - birdY, 2)) # distance formula
distanceDown = math.sqrt(math.pow(pipeDownX - birdX, 2) + math.pow(pipeDownX - birdY, 2))
if distanceUp <= 20 or distanceDown <= 20:
return True
else:
return False
我已经在主游戏循环中调用了该函数并要求 python 如果为真则结束游戏,但是小鸟只是穿过 pipe.FYI,我没有使用 OOP 并且classes.Here 是值..
pipeWidth = 50
pipeHeight = 130
pipeUpX = 800
pipeUpY = 0
pipeDownY = screenY - pipeHeight
pipeDownX = 900
pipeX_change = 1
此外,我对 python 和整个编程还很陌生,所以请用易于理解的代码回答。
谢谢 :)
代码不验证到管道的距离。该代码验证到管道起点和终点的距离。那是不同的东西。
由于您的对象只是矩形,我建议使用 pygame.Rect objeccts
和 colliderect
方法。例如一些伪代码:
def collision():
pipeRect = pygame.Rect(pipeLeft, pipeTop, pipeWidth, pipeHeight)
birdRect = pygame.Rect(birdLeft, birdTop, birdWidth, birdHeight)
return pipeRect.colliderect(birdRect)
对于"images"的碰撞,我推荐使用pygame.sprite.Sprite
/ pygame.sprite.collide_mask()
respectively pygame.mask.Mask
/ pygame.mask.Mask.overlap()
。
进一步了解:
How can I made a collision mask?
我目前正在 Python 上开发一款简单的飞鸟类游戏。出于某种原因,当管道和鸟接触时,碰撞代码不起作用。
def collision():
global distanceDown, distanceUp
distanceUp = math.sqrt(math.pow(pipeUpX - birdX, 2) + math.pow(pipeUpY - birdY, 2)) # distance formula
distanceDown = math.sqrt(math.pow(pipeDownX - birdX, 2) + math.pow(pipeDownX - birdY, 2))
if distanceUp <= 20 or distanceDown <= 20:
return True
else:
return False
我已经在主游戏循环中调用了该函数并要求 python 如果为真则结束游戏,但是小鸟只是穿过 pipe.FYI,我没有使用 OOP 并且classes.Here 是值..
pipeWidth = 50
pipeHeight = 130
pipeUpX = 800
pipeUpY = 0
pipeDownY = screenY - pipeHeight
pipeDownX = 900
pipeX_change = 1
此外,我对 python 和整个编程还很陌生,所以请用易于理解的代码回答。 谢谢 :)
代码不验证到管道的距离。该代码验证到管道起点和终点的距离。那是不同的东西。
由于您的对象只是矩形,我建议使用 pygame.Rect objeccts
和 colliderect
方法。例如一些伪代码:
def collision():
pipeRect = pygame.Rect(pipeLeft, pipeTop, pipeWidth, pipeHeight)
birdRect = pygame.Rect(birdLeft, birdTop, birdWidth, birdHeight)
return pipeRect.colliderect(birdRect)
对于"images"的碰撞,我推荐使用pygame.sprite.Sprite
/ pygame.sprite.collide_mask()
respectively pygame.mask.Mask
/ pygame.mask.Mask.overlap()
。
进一步了解:
How can I made a collision mask?