【问题标题】:Collision detection in pygamepygame中的碰撞检测
【发布时间】:2015-08-28 21:15:11
【问题描述】:

我写了一个小测试来感受一下 pygame 中的碰撞检测。我的球员移动正常并停在岩石上,碰撞发生的地方,但当我到达岩石时,我无法再次离开。你们知道为什么会这样吗?这是我的测试代码:

import pygame
import sys

white = (255, 255, 255)
black = (  0,   0,   0)


# Player class
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load('../foo.png')
        self.rect = self.image.get_rect()


class Rock(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load('../rock.png')
        self.rect = self.image.get_rect()
        self.rect.x = 50
        self.rect.y = 50

# essential pygame init
pygame.init()

# screen
screen_width = 400
screen_height = 400
screen_size = (screen_width, screen_height)
screen = pygame.display.set_mode(screen_size)

# List for all sprites
sprites = pygame.sprite.Group()


# Rock
rock = Rock()
sprites.add(rock)

# Create player
player = Player()
sprites.add(player)

done = False

clock = pygame.time.Clock()
while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            sys.exit()

    sprites.update()

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_LEFT] and not player.rect.colliderect(rock.rect):
        #if not player.rect.colliderect(rock.rect):
        move = (-1, 0)
        player.rect = player.rect.move(move)
    if pressed[pygame.K_RIGHT] and not player.rect.colliderect(rock.rect):
        move = (1, 0)
        player.rect = player.rect.move(move)
    if pressed[pygame.K_UP] and not player.rect.colliderect(rock.rect):
        move = (0, -1)
        player.rect = player.rect.move(move)
    if pressed[pygame.K_DOWN] and not player.rect.colliderect(rock.rect):
        move = (0, 1)
        player.rect = player.rect.move(move)

    screen.fill(white)

    sprites.draw(screen)

    pygame.display.flip()

    clock.tick(30)

pygame.quit()

编辑:当我将移动速度降低到 1 时,玩家仍然卡在岩石中。我也更新了代码。

【问题讨论】:

    标签: python pygame collision-detection collision


    【解决方案1】:

    您的答案包含在您的问题中..

        if .... . and not player.rect.colliderect(rock.rect):
    

    只有在不发生碰撞时才允许使用所有移动键。 一旦你在碰撞范围内,你的任何移动动作都是不允许的。

    你必须让移动键离开障碍物! 为此,您需要知道发生碰撞的位置.. (字符前面,字符左侧等)

    编辑:灵感来自另一个答案

    在接受答案后,已包含此编辑;) 原因是解决方案比检查碰撞的方向更好,因为它不需要你计算任何东西..

    这个想法很简单.. 总是尝试移动,并且只有当结果不会导致碰撞时,才应用它。

    这将始终允许合法移动,并且不会让您陷入困境。

    唯一需要注意的是,如果一个移动步骤大于障碍步骤,则可以“跳过”非常小的障碍物。..

    检查碰撞的简单而有效的方法是先移动,检查 碰撞并在必要时后退。

    David Mašek

    还有这条评论:

    move 返回一个新的 Rect,因此您可以使用它来检查碰撞。如果有,什么也不做。如果没有碰撞,将 player.rect 设置为新的 rect 或(在 player.rect 上调用 move_ip)。

    sloth

    【讨论】:

    • @Don'tclickonmyprofile 但是一旦发生碰撞,您就已经陷入困境。即使您以后可以摆脱它,这也可能不是您想要的行为。
    【解决方案2】:

    你检查你没有碰撞然后移动。想象一下,您在岩石下方 1 个单位,然后向上移动 1 个单位。您可以这样做,因为您现在没有被卡住。但现在你在岩石中无法移动

    检查碰撞的简单而有效的方法是先移动,检查碰撞并在必要时返回。

    【讨论】:

    • 您实际上并没有向后移动move 返回一个新的Rect,因此您可以使用它来检查碰撞。如果有,什么也不做。如果没有冲突,请将player.rect 设置为新的矩形或(在player.rect 上调用move_ip)。
    • @sloth 我的意思不是像Rect.move() 那样移动,而是“改变位置”(即动词的正常含义)。
    • 这个答案 + cmets 是个好方法,我偷了它来编辑我的答案
    【解决方案3】:

    如果你的岩石不在 5 单位边界处,你可以移动“进入”岩石,然后被卡住。

    【讨论】:

    • 我将移动速度改写为1,再次测试,再次卡住。我错过了你回答的重点吗?
    • @Don'tclickonmyprofile:这很有趣。将该信息添加到您的原始问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多