用pygame蹲在python平台游戏中

时间:2019-02-14 16:33:45

标签: python python-3.x pygame

我正在python / pygame类中为最终项目制作游戏。我遇到了“蹲伏”能力的问题。我希望能够更改播放器的大小,但唯一可以更改的是顶部的精灵。

我的班级正在使用this网站上的以下特定示例:

如何缩放或更改播放器的大小以适合精灵,并在每次“放开”时都可以将其更改回原来的状态?

不确定哪些部分是相关的,但这是我的代码(对此我很抱歉,我对此表示歉意):

class Bink(pygame.sprite.Sprite):
    """ This class represents the bar at the bottom that the player
    controls. """

    # -- Methods
    def __init__(self):
        """ Constructor function """

        # Call the parent's constructor
        super().__init__()

        # -- Attributes
        # Set speed vector of player
        self.change_x = 0
        self.change_y = 0

        # Set fall rate
        self.y_speedup = .45

        # Double jump functions
        self.jump_no = 0

        # Set the players height and length
        self.length = 40
        self.height = 60

        # Set crouching to false
        self.crouching = False

        # This holds all the images for the animated walk left/right
        # of our player
        self.walking_frames_l = []
        self.walking_frames_r = []

        self.standing_frames_l = []
        self.standing_frames_r = []

        self.jumping_frames_l = []
        self.jumping_frames_r = []

        self.falling_frames_l = []
        self.falling_frames_r = []

        self.crouching_frames_l = []
        self.crouching_frames_r = []

        # What direction is the player facing?
        self.direction = "Right"

        # What 'state' is the player in
        self.state = "Stand"

        # List of sprites we can bump against
        self.level = None

        sprite_sheet = SpriteSheet("player_sprite_sheet.png")

        # Load all the right walking images into a list
        image = sprite_sheet.get_image(419, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(460, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(501, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(542, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(583, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(624, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(665, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(706, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(747, 65, 40, 60)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(788, 65, 40, 60)
        self.walking_frames_r.append(image)

        # Load all the left walking images

        image = sprite_sheet.get_image(419, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(460, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(501, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(542, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(583, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(624, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(665, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(706, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(747, 0, 40, 60)
        self.walking_frames_l.append(image)
        image = sprite_sheet.get_image(788, 0, 40, 60)
        self.walking_frames_l.append(image)

        # Load all the left standing sprites
        image = sprite_sheet.get_image(0, 0, 40, 60)
        self.standing_frames_l.append(image)

        # Load all the right standing sprites
        image = sprite_sheet.get_image(419, 195, 40, 60)
        self.standing_frames_r.append(image)

        # Load all the left jumping sprites
        image = sprite_sheet.get_image(0, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(41, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(82, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(123, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(164, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(205, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(246, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(287, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(328, 65, 40, 60)
        self.jumping_frames_l.append(image)
        image = sprite_sheet.get_image(369, 65, 40, 60)
        self.jumping_frames_l.append(image)

        # Load all the right jumping sprites
        image = sprite_sheet.get_image(419, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(460, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(501, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(542, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(583, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(624, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(665, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(706, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(747, 130, 40, 60)
        self.jumping_frames_r.append(image)
        image = sprite_sheet.get_image(788, 130, 40, 60)
        self.jumping_frames_r.append(image)

        # Load all left falling sprites
        image = sprite_sheet.get_image(0, 130, 40, 60)
        self.falling_frames_l.append(image)
        image = sprite_sheet.get_image(41, 130, 40, 60)
        self.falling_frames_l.append(image)
        image = sprite_sheet.get_image(82, 130, 40, 60)
        self.falling_frames_l.append(image)
        image = sprite_sheet.get_image(123, 130, 40, 60)
        self.falling_frames_l.append(image)
        image = sprite_sheet.get_image(164, 130, 40, 60)
        self.falling_frames_l.append(image)

        # Load all right falling sprites
        image = sprite_sheet.get_image(205, 130, 40, 60)
        self.falling_frames_r.append(image)
        image = sprite_sheet.get_image(246, 130, 40, 60)
        self.falling_frames_r.append(image)
        image = sprite_sheet.get_image(287, 130, 40, 60)
        self.falling_frames_r.append(image)
        image = sprite_sheet.get_image(328, 130, 40, 60)
        self.falling_frames_r.append(image)
        image = sprite_sheet.get_image(369, 130, 40, 60)
        self.falling_frames_r.append(image)

        # Load all the left crouching sprites
        image = sprite_sheet.get_image(0, 195, 45, 40)
        self.crouching_frames_l.append(image)
        image = sprite_sheet.get_image(46, 195, 45, 40)
        self.crouching_frames_l.append(image)
        image = sprite_sheet.get_image(92, 195, 45, 40)
        self.crouching_frames_l.append(image)
        image = sprite_sheet.get_image(138, 195, 45, 40)
        self.crouching_frames_l.append(image)
        image = sprite_sheet.get_image(184, 195, 45, 40)
        self.crouching_frames_l.append(image)

        # Load all right crouching sprites
        image = sprite_sheet.get_image(0, 240, 45, 40)
        self.crouching_frames_r.append(image)
        image = sprite_sheet.get_image(46, 240, 45, 40)
        self.crouching_frames_r.append(image)
        image = sprite_sheet.get_image(92, 240, 45, 40)
        self.crouching_frames_r.append(image)
        image = sprite_sheet.get_image(138, 240, 45, 40)
        self.crouching_frames_r.append(image)
        image = sprite_sheet.get_image(184, 240, 45, 40)
        self.crouching_frames_r.append(image)

        # Set the image the player starts with
        self.image = self.standing_frames_r[0]

        # Set a reference to the image rect.
        self.rect = self.image.get_rect()

        # Build a hitbox that objects collide with
        self.hitbox = self.rect.inflate(0, 0)

        # Add or subtract to see if the player is crouching
        self.is_crouching = 0

    def update(self):
        """ Move the player. """
        # Gravity
        self.calc_grav()

        # Set the hitbox on top of the player
        self.hitbox.center = self.rect.center

        # Check for the direction
        if self.change_x < 0:
            self.direction = "Left"
        elif self.change_x > 0:
            self.direction = "Right"

        # Check for the state
        # See if we hit anything
        self.rect.y += 2
        platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        self.rect.y -= 2

        # If it is ok to jump, set our speed upwards
        if len(platform_hit_list) > 0 or self.hitbox.bottom >= SCREEN_HEIGHT and self.change_x == 0:
            self.state = "Stand"
        elif len(platform_hit_list) > 0 or self.hitbox.bottom >= SCREEN_HEIGHT and self.change_x != 0:
            self.state = "Walk"
        elif self.change_y < 0 or self.change_y < 0 and self.change_x != 0:
            self.state = "Jump"
        elif self.change_y > 0 or self.change_y > 0 and self.change_x != 0:
            self.state = "Fall"
        if self.is_crouching == 1:
            self.state = "Crouch"

        # Move left/right
        self.rect.x += self.change_x
        x_pos = self.rect.x + self.level.world_shift
        y_pos = self.rect.y + self.level.world_shift

        # "If" in 'this' state and 'this' direction use 'that' sprite list for reference
        if self.direction == "Right" and self.state == "Stand":
            frame = (x_pos // 30) % len(self.standing_frames_r)
            self.image = self.standing_frames_r[frame]
        elif self.direction == "Left" and self.state == "Stand":
            frame = (x_pos // 30) % len(self.standing_frames_l)
            self.image = self.standing_frames_l[frame]

        elif self.direction == "Right" and self.state == "Walk":
            frame = (x_pos // 30) % len(self.walking_frames_r)
            self.image = self.walking_frames_r[frame]
        elif self.direction == "Left" and self.state == "Walk":
            frame = (x_pos // 30) % len(self.walking_frames_l)
            self.image = self.walking_frames_l[frame]

        elif self.direction == "Right" and self.state == "Jump":
            frame = (y_pos // 30) % len(self.jumping_frames_r)
            self.image = self.jumping_frames_r[frame]
        elif self.direction == "Left" and self.state == "Jump":
            frame = (y_pos // 30) % len(self.jumping_frames_l)
            self.image = self.jumping_frames_l[frame]

        elif self.direction == "Right" and self.state == "Fall":
            frame = (y_pos // 30) % len(self.falling_frames_r)
            self.image = self.falling_frames_r[frame]
        elif self.direction == "Left" and self.state == "Fall":
            frame = (y_pos // 30) % len(self.falling_frames_l)
            self.image = self.falling_frames_l[frame]

        elif self.direction == "Right" and self.state == "Crouch":
            frame = (x_pos // 30) % len(self.crouching_frames_r)
            self.rect.inflate(5, -20)
            # if self.length != 40:
            #     self.rect.inflate(0, 0)
            self.image = self.crouching_frames_r[frame]
        elif self.direction == "Left" and self.state == "Crouch":
            frame = (x_pos // 30) % len(self.crouching_frames_l)
            self.image = self.crouching_frames_l[frame]

        # See if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:
            # If we are moving right,
            # set our right side to the left side of the item we hit
            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right

        # Move up/down
        self.rect.y += self.change_y

        # Check and see if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:

            # Reset our position based on the top/bottom of the object.
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            elif self.change_y < 0:
                self.rect.top = block.rect.bottom

            # Stop our vertical movement
            self.change_y = 0

            if isinstance(block, MovingPlatform):
                self.rect.x += block.change_x

        self.length = self.rect.right - self.rect.left
        print(self.length)

    def calc_grav(self):
        """ Calculate effect of gravity. """

        # Import the wallslide method
        self.wall_slide()

        if self.change_y == 0:
            self.change_y = 1.75
        else:
            self.change_y += .45

        # See if we are on the ground.
        if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
            self.change_y = 0
            self.rect.y = SCREEN_HEIGHT - self.rect.height

我真的很感谢任何反馈!我对此很陌生,因此我完全愿意完全完全错误。

1 个答案:

答案 0 :(得分:0)

我决定将评论扩展为答案。

因此该程序正在从Sprite-sheet加载一堆Sprite。让我们注意,“标准”字符图像似乎是40 x 60像素,而“下蹲”图像是45 x 40。相差20像素(蹲下的图像也宽5像素)。

当精灵进入“蹲伏”模式self.state == "Crouch"时,代码仅更改了精灵图像。它不考虑位置变化。如果角色通过向上拉动其腿/轮/触手而“蹲伏”,并且很好地保持了相同的高度。但是,如果按传统意义蹲伏,则Y位置需要通过子画面图像高度的差异来增加。这是因为PyGame从左上角绘制了精灵。

代码未包含在问题中,但是无论什么操作导致self.state更改为"Crouch",这还需要将字符的Y位置调整{{1} }进入蹲伏模式时,20 pixels则返回直立形式。这种差异可能最好在加载的图像上计算,而不是作为常数。另外,必须重新设置精灵-20 pixels,因为精灵图像的大小现在已经不同,并且需要调整碰撞盒。

我写了一些快速演示代码来说明如何在sprite上工作。它只是一个螺旋弹簧-原来制作的像素化位图差,但它说明了压缩/未压缩状态。该代码的重要部分是rect函数,该函数通过图像高度的不同来实现SpringSprite.crouch()位置的变化。由于此代码位于子对象 centred rect上,因此高度变化明显减少了一半。

spring.png spring_compressed.png

向上/向下按压缩和解压缩弹簧。

Y