5

我本质上是在尝试用 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

这样做的问题是玩家被“卡”在了塔 Rect 内,尽管返回到他们在碰撞开始之前的位置,玩家 Rect 总是会被拉回塔内,并且碰撞将继续扳机。在最初接触塔矩形后,玩家将无法向任何方向移动。

4

1 回答 1

3

我在我的一个 pygame 游戏中做了同样的事情。您要做的是创建一个所有对象都将使用的移动功能。它使得不可能通过渲染更新组中的任何精灵,称为一切。如果一个精灵不是一切的一部分,它就不会发生碰撞。这是功能。这会产生一定量的碰撞阻力。基本上,在推动一个物体时,它会推回一定的量。任何不调用move函数的物体即使被推也不会移动,所以只有最初可以移动的物体才能被推,而墙壁等物体在推时不会滑过板面。

    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

它有很多代码,您可能希望根据自己的目的对其进行更改,但这是一种非常好的方法。如果您想消除反弹功能,您可以考虑将任何朝向对象的移动设置为零,并且只允许远离它的移动。但是,我发现反弹功能对我的游戏有用且更准确。

于 2013-11-23T04:50:00.983 回答