Pygame 碰撞点 () 无法正常工作
Pygame collidepoint() not working correctly
我正在制作一个游戏,你可以在其中砍伐树木并想要它,以便你只能砍伐你在游戏中的位置(由一个正方形)。问题是,当我测试它时,我发现它只起作用了一次,我的意思是你只需要移动来阻止半径屏障起作用,你就可以摧毁任何一棵树。有人能告诉我为什么会这样以及如何解决吗?代码如下:
# I'll only put in the bit that makes the bug
# Tree objects are sorted in a Tree class with a method destroy() to destroy the tree
for tree in trees:
if pygame.mouse.get_pressed()[0] and tree.trunk.collidepoint(pygame.mouse.get_pos()):
mouse_x, mouse_y = pygame.mouse.get_pos()
print('clicked tree') # For testing purposes
if mouse_x < x + 51 and mouse_y < y + 51:
countdown = 3
destroy_tree = tree
elif mouse_x < x + 51 and mouse_y < y - 51:
countdown = 3
destroy_tree = tree
elif mouse_x < x - 51 and mouse_y < y - 51:
countdown = 3
destroy_tree = tree
elif mouse_x < x - 51 and mouse_y < y + 51:
countdown = 3
destroy_tree = tree
你必须评估坐标是否在一个范围内,在一个相同的条件下。你实际做的是这样的:
if x < value + 50:
countdown = 3
elif x > value - 50:
countdown = 3
总是满足其中一个条件,最后 countdown
在任何情况下都成立。
条件必须是:
if x - 51 < mouse_x < x + 51:
if y - 51 < mouse_y < y + 51:
countdown = 3
destroy_tree = tree
此外,使用abs()
可以简化算法。例如:
mouse_x, mouse_y = pygame.mouse.get_pos()
for tree in trees:
if pygame.mouse.get_pressed()[0] and tree.trunk.collidepoint((mouse_x, mouse_y)):
print('clicked tree') # For testing purposes
dx = mouse_x - x
dy = mouse_y - y
if abs(dx) <= 50 and abs(dy) <= 50:
countdown = 3
destroy_tree = tree
或者您可以计算 Euclidean distance:
mouse_x, mouse_y = pygame.mouse.get_pos()
for tree in trees:
if pygame.mouse.get_pressed()[0] and tree.trunk.collidepoint((mouse_x, mouse_y)):
print('clicked tree') # For testing purposes
dx, dy = (mouse_x - x, mouse_y - y)
if dx*dx + dy*dy <= 50*50:
countdown = 3
destroy_tree = tree
我正在制作一个游戏,你可以在其中砍伐树木并想要它,以便你只能砍伐你在游戏中的位置(由一个正方形)。问题是,当我测试它时,我发现它只起作用了一次,我的意思是你只需要移动来阻止半径屏障起作用,你就可以摧毁任何一棵树。有人能告诉我为什么会这样以及如何解决吗?代码如下:
# I'll only put in the bit that makes the bug
# Tree objects are sorted in a Tree class with a method destroy() to destroy the tree
for tree in trees:
if pygame.mouse.get_pressed()[0] and tree.trunk.collidepoint(pygame.mouse.get_pos()):
mouse_x, mouse_y = pygame.mouse.get_pos()
print('clicked tree') # For testing purposes
if mouse_x < x + 51 and mouse_y < y + 51:
countdown = 3
destroy_tree = tree
elif mouse_x < x + 51 and mouse_y < y - 51:
countdown = 3
destroy_tree = tree
elif mouse_x < x - 51 and mouse_y < y - 51:
countdown = 3
destroy_tree = tree
elif mouse_x < x - 51 and mouse_y < y + 51:
countdown = 3
destroy_tree = tree
你必须评估坐标是否在一个范围内,在一个相同的条件下。你实际做的是这样的:
if x < value + 50:
countdown = 3
elif x > value - 50:
countdown = 3
总是满足其中一个条件,最后 countdown
在任何情况下都成立。
条件必须是:
if x - 51 < mouse_x < x + 51:
if y - 51 < mouse_y < y + 51:
countdown = 3
destroy_tree = tree
此外,使用abs()
可以简化算法。例如:
mouse_x, mouse_y = pygame.mouse.get_pos()
for tree in trees:
if pygame.mouse.get_pressed()[0] and tree.trunk.collidepoint((mouse_x, mouse_y)):
print('clicked tree') # For testing purposes
dx = mouse_x - x
dy = mouse_y - y
if abs(dx) <= 50 and abs(dy) <= 50:
countdown = 3
destroy_tree = tree
或者您可以计算 Euclidean distance:
mouse_x, mouse_y = pygame.mouse.get_pos()
for tree in trees:
if pygame.mouse.get_pressed()[0] and tree.trunk.collidepoint((mouse_x, mouse_y)):
print('clicked tree') # For testing purposes
dx, dy = (mouse_x - x, mouse_y - y)
if dx*dx + dy*dy <= 50*50:
countdown = 3
destroy_tree = tree