Pygame在碰撞检测时停止运动

时间:2013-04-23 04:47:48

标签: python pygame rect

我本质上是想用pygame制作一个“可靠的”对象。目标是在玩家接触时击退玩家。我目前使用的(但无法正常工作)如下:

keys_pressed = pygame.key.get_pressed()
if 1 in keys_pressed:
    if keys_pressed[K_w]:
        self.player_l[1] += -2
        if self.player_r.colliderect(self.tower_r): self.player_l[1] -= -2
    if keys_pressed[K_a]:
        self.player_l[0] += -2
        if self.player_r.colliderect(self.tower_r): self.player_l[0] -= -2
    if keys_pressed[K_s]:
        self.player_l[1] += 2
        if self.player_r.colliderect(self.tower_r): self.player_l[1] -= 2
    if keys_pressed[K_d]:
        self.player_l[0] += 2
        if self.player_r.colliderect(self.tower_r): self.player_l[0] -= 2

这个问题是玩家被“卡住”在Tower Rect内,尽管他们在碰撞开始之前回到了他们所在的位置,玩家Rect将总是被拉回到塔中,并且碰撞将继续触发。在最初触摸塔Rect后,玩家将无法向任何方向移动。

1 个答案:

答案 0 :(得分:1)

我在我的pygame游戏中做了同样的事情。你想要做的是做一个移动所有对象将使用的功能。它使得无法通过称为所有内容的渲染更新组中的任何精灵。如果精灵不是一切的一部分,它就不会碰撞。这是功能。这会产生一定量的碰撞阻力。基本上,在推动物体时,它会推回一定量。任何不调用移动功能的对象即使被推动也不会移动,因此只有可以在第一个位置移动的对象才能被推动,而墙壁之类的东西在推动时不会滑过整个板。

    def moveRelative(self,other,speed):                                   #This function is a function the one you need uses, which you may find useful. It is designed to move towards or a way from another sprite. Other is the other sprite, speed is an integer, where a negative value specifies moving away from the sprite, which is how many pixels it will move away from the target. This returns coordinates for the move_ip function to move to or away from the sprite, as a tuple
            dx = other.rect.x - self.rect.x
            dy = other.rect.y - self.rect.y
            if abs(dx) > abs(dy):
                    # other is farther away in x than in y
                    if dx > 0:
                            return (+speed,0)
                    else:
                            return (-speed,0)
            else:
                    if dy > 0:
                            return (0,+speed)
                    else:
                            return (0,-speed)

    def move(self,dx,dy):
            screen.fill((COLOR),self.rect)                                 #covers over the sprite's rectangle with the background color, a constant in the program
            collisions = pygame.sprite.spritecollide(self, everything, False)
            for other in collisions:
                    if other != self:
                            (awayDx,awayDy) = self.moveRelative(other,-1)  #moves away from the object it is colliding with
                            dx = dx + 9*(awayDx)                           #the number 9 here represents the object's resistance. When you push on an object, it will push with a force of nine back. If you make it too low, players can walk right through other objects. If you make it too high, players will bounce back from other objects violently upon contact. In this, if a player moves in a direction faster than a speed of nine, they will push through the other object (or simply push the other object back if they are also in motion)
                            dy = dy + 9*(awayDy)
            self.rect.move_ip(dx,dy)                                       #this finally implements the movement, with the new calculations being used

它是一种很多代码,你可能想要为你的目的改变它,但这是一个非常好的方法。如果您想要消除反弹功能,您可以考虑将对象的任何移动设置为零,并允许仅移动它。不过,我发现反弹功能对我的游戏更有用,更准确。