【问题标题】:Collision with walls in Pygame [closed]Pygame中的墙壁碰撞[关闭]
【发布时间】:2018-01-21 10:26:12
【问题描述】:

我得到了一个任务,用 Python Pygame 制作一个益智迷宫游戏。我使用画线功能绘制了您必须导航的迷宫网格,现在我不知道如何使图像(角色)在撞到墙上时停止。

【问题讨论】:

  • 如果你知道如何使用 pygame sprites,看看这个answer

标签: python pygame collision


【解决方案1】:

可以使用 PyGame 的 SpriteCollide 函数 - https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide

Example:

# See if the Sprite block has collided with anything in the Group block_list
# The True flag will remove the sprite in block_list
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)

# Check the list of colliding sprites, and add one to the score for each one
for block in blocks_hit_list:
    score +=1

您可以在上述链接中阅读有关该函数的更多信息,我建议您按照教程了解有关 PyGame 的其他一些见解。

【讨论】:

    【解决方案2】:

    你需要检查玩家的x和y坐标是否为0和0。如果是,你可以说,他们的坐标应该设置为1和1,这样他们就出不来了。

    【讨论】:

    • 它是否适用于在屏幕上绘制的边界
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多