简体   繁体   English

添加碰撞检测到sprite,pyGame

[英]Add collision detection to sprite, pyGame

bassically im trying to add collision detection to the sprite below, using the following: 我试图在下面的sprite中添加碰撞检测,使用以下方法:

self.rect = bounds_rect


                collide = pygame.sprite.spritecollide(self, wall_list, False)

                if collide:
            # yes

                     print("collide")

However it seems that when the collide is triggered it continuously prints 'collide' over and over when instead i want them to simply not be able to walk through the object, any help? 然而,似乎当碰撞被触发时,它会不断地反复打印“碰撞”而我希望它们根本无法穿过物体,任何帮助?

def update(self, time_passed):
        """ Update the creep.

            time_passed:
                The time passed (in ms) since the previous update.
        """
        if self.state == Creep.ALIVE:
            # Maybe it's time to change the direction ?
            #
            self._change_direction(time_passed)

            # Make the creep point in the correct direction.
            # Since our direction vector is in screen coordinates 
            # (i.e. right bottom is 1, 1), and rotate() rotates 
            # counter-clockwise, the angle must be inverted to 
            # work correctly.
            #
            self.image = pygame.transform.rotate(
                self.base_image, -self.direction.angle)

            # Compute and apply the displacement to the position 
            # vector. The displacement is a vector, having the angle
            # of self.direction (which is normalized to not affect
            # the magnitude of the displacement)
            #
            displacement = vec2d(    
                self.direction.x * self.speed * time_passed,
                self.direction.y * self.speed * time_passed)

            self.pos += displacement

            # When the image is rotated, its size is changed.
            # We must take the size into account for detecting 
            # collisions with the walls.
            #
            self.image_w, self.image_h = self.image.get_size()
            global bounds_rect
            bounds_rect = self.field.inflate(
                            -self.image_w, -self.image_h)

            if self.pos.x < bounds_rect.left:
                self.pos.x = bounds_rect.left
                self.direction.x *= -1
            elif self.pos.x > bounds_rect.right:
                self.pos.x = bounds_rect.right
                self.direction.x *= -1
            elif self.pos.y < bounds_rect.top:
                self.pos.y = bounds_rect.top
                self.direction.y *= -1
            elif self.pos.y > bounds_rect.bottom:
                self.pos.y = bounds_rect.bottom
                self.direction.y *= -1

            self.rect = bounds_rect


            collide = pygame.sprite.spritecollide(self, wall_list, False)

            if collide:
        # yes

                 print("collide")




        elif self.state == Creep.EXPLODING:
            if self.explode_animation.active:
                self.explode_animation.update(time_passed)
            else:
                self.state = Creep.DEAD
                self.kill()

        elif self.state == Creep.DEAD:
            pass

        #------------------ PRIVATE PARTS ------------------#

    # States the creep can be in.
    #
    # ALIVE: The creep is roaming around the screen
    # EXPLODING: 
    #   The creep is now exploding, just a moment before dying.
    # DEAD: The creep is dead and inactive
    #
    (ALIVE, EXPLODING, DEAD) = range(3)

    _counter = 0

    def _change_direction(self, time_passed):
        """ Turn by 45 degrees in a random direction once per
            0.4 to 0.5 seconds.
        """
        self._counter += time_passed
        if self._counter > randint(400, 500):
            self.direction.rotate(45 * randint(-1, 1))
            self._counter = 0

    def _point_is_inside(self, point):
        """ Is the point (given as a vec2d) inside our creep's
            body?
        """
        img_point = point - vec2d(  
            int(self.pos.x - self.image_w / 2),
            int(self.pos.y - self.image_h / 2))

        try:
            pix = self.image.get_at(img_point)
            return pix[3] > 0
        except IndexError:
            return False

    def _decrease_health(self, n):
        """ Decrease my health by n (or to 0, if it's currently
            less than n)
        """
        self.health = max(0, self.health - n)
        if self.health == 0:
            self._explode()

    def _explode(self):
        """ Starts the explosion animation that ends the Creep's
            life.
        """
        self.state = Creep.EXPLODING
        pos = ( self.pos.x - self.explosion_images[0].get_width() / 2,
                self.pos.y - self.explosion_images[0].get_height() / 2)
        self.explode_animation = SimpleAnimation(
            self.screen, pos, self.explosion_images,
            100, 300)
        global remainingCreeps

        remainingCreeps-=1

        if remainingCreeps == 0:
                print("all dead")

A check for collision is only a check to see if two rectangular sprites have a common area. 检查碰撞只是检查两个矩形精灵是否具有公共区域。 There isn't a built in collision that unables player input during collision. 在碰撞过程中没有内置碰撞无法让玩家输入。 You have to write that yourself. 你必须自己写。

You should probably want to change the player coordinates when a collision takes place. 您可能希望在发生碰撞时更改玩家坐标。 An example: 一个例子:

Let's say we play mario. 假设我们玩马里奥。 When the state of mario is JUMPING check for collision. 当马里奥的状态是JUMPING检查碰撞。 Somewhere we will store the speed of mario in the y axis. 我们会在某处将马里奥的速度存储在y轴上。 When the collision returns True, with any of the blocks, we now set the speed to 0, and the y to the top/bottom of a block. 当碰撞返回True时,对于任何一个块,我们现在将速度设置为0,将y设置为块的顶部/底部。 If it will be the bottom, we still keep JUMPING, so it can fall back to the ground. 如果它将是底部,我们仍然保持JUMPING,所以它可以回落到地面。

My tip for the creeper is to have some oldx and oldy value, to return to when the collision takes place. 我对爬行者的提示是要有一些oldx和oldy值,以便在发生碰撞时返回。 That way the creeper will never go into a wall. 那样爬行者永远不会进入墙壁。 Another approach would be to simply change the direction when a collision takes place, but that may not always work. 另一种方法是在发生碰撞时简单地改变方向,但这可能并不总是有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM