【问题标题】:How would I fix my characters position after hitting a block?撞到块后如何固定我的角色位置?
【发布时间】:2020-04-18 20:43:21
【问题描述】:

在我击中平台后,我的角色无法再沿 x 轴移动,除非您再次按左键或右键。例如,如果我跳跃并夹住平台的一角,则角色不会继续移动,我必须再次跳跃或快速反应并按下相应的键。 当我在碰撞检测系统中删除 player.friction() 时,如果我停止按右键,它最终会夹在块的边缘,这会导致玩家永久卡住,除非他们按右键和跳转键同时。我认为这可以通过再次更改块位置来解决,但到目前为止我不知道如何实现。

这是我的代码:

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 255, 255)

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600
gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))

def textObjects(text, font):
    textSurface = font.render(text, True, BLACK)
    return textSurface, textSurface.get_rect()
def messageDisplay(text):
    largeText = pygame.font.Font('freesansbold.ttf', 115)
    textSurf, textRect = textObjects(text, largeText)
    textRect.center = ((DISPLAY_WIDTH/2), (DISPLAY_HEIGHT/2))
    gameDisplay.blit(textSurf, textRect)


class Player(pygame.sprite.Sprite):

    def __init__(self):


        super().__init__()


        width = 100
        height = 150
        self.image = pygame.Surface([width, height])
        self.image.fill(RED)


        self.rect = self.image.get_rect()


        self.xVel = 0
        self.yVel = 0


        self.level = None

    def update(self):
        self.calc_grav()


        self.level.bgX += self.xVel



        correction = 0
        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:
            if self.xVel < 0:
                self.friction()
                correction = self.rect.right - block.rect.left
            elif self.xVel > 0:
                self.friction()
                correction = self.rect.left - block.rect.right

        if correction != 0:
            for block in self.level.platform_list:
                block.rect.x += correction


        self.rect.y += self.yVel


        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:
            if self.yVel > 0:
                self.rect.bottom = block.rect.top
            if self.yVel < 0:
                self.rect.top = block.rect.bottom


            self.yVel = 0

            if isinstance(block, MovingPlatform):
                self.level.bgX += block.xVel

    def calc_grav(self):

        if self.yVel == 0:
            self.yVel = 1
        else:
            self.yVel +=0.3

        if self.rect.y >= DISPLAY_HEIGHT - self.rect.height and self.yVel >= 0:
            self.yVel = 0
            self.rect.y = DISPLAY_HEIGHT - self.rect.height


    def jump(self):
        self.rect.y += 2
        platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        self.rect.y -= 2


        if len(platform_hit_list) > 0 or self.rect.bottom >= DISPLAY_HEIGHT:
            self.yVel = -10

    def moveLeft(self):

        self.xVel = 10

    def moveRight(self):

        self.xVel = -10

    def friction(self):

        self.xVel = 0


class Platform(pygame.sprite.Sprite):

    def __init__(self, width, height):
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(GREEN)

        self.rect = self.image.get_rect()


class MovingPlatform(Platform):

    xVel = 0
    yVel = 0

    boundary_top = 0
    boundary_bottom = 0
    boundary_left = 0
    boundary_right = 0

    player = None

    level = None

    def update(self):
        self.rect.x += self.xVel
        correction = 0
        hit = pygame.sprite.collide_rect(self, self.player)
        if hit:
            if self.xVel < 0:
                self.player.friction()
                correction = self.player.rect.right - self.rect.left
            elif self.xVel > 0:
                self.player.friction()
                correction = self.player.rect.left - self.rect.right

        if correction != 0:
            for block in self.level.platform_list:
                block.rect.x += correction

        self.rect.y += self.yVel


        hit = pygame.sprite.collide_rect(self, self.player)
        if hit:
            if self.yVel < 0:
                self.player.rect.bottom = self.rect.top
            else:
                self.player.rect.top = self.rect.bottom

        if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
            self.yVel *= -1
        cur_pos = self.rect.x - self.level.bgX
        if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
            self.xVel *= -1


class Level(object):

    def __init__(self, player):
        self.platform_list = pygame.sprite.Group()
        self.enemy_list = pygame.sprite.Group()
        self.player = player

        self.background = None


        self.bgX = 0
        self.level_limit = -1000

    def update(self):
        self.platform_list.update()
        self.enemy_list.update()

    def draw(self, screen):

        screen.fill(BLUE)


        self.platform_list.draw(screen)
        self.enemy_list.draw(screen)

        for platform in self.platform_list:
            platform.rect.x += self.player.xVel

        for enemy in self.enemy_list:
            enemy.rect.x += self.player.xVel

class Level_01(Level):

    def __init__(self, player):


        Level.__init__(self, player)

        self.level_limit = -1500


        level = [[210, 70, 500, 500],
                 [210, 70, 800, 400],
                 [210, 70, 1000, 500],
                 [210, 70, 1120, 280],
                 ]


        for platform in level:
            block = Platform(platform[0], platform[1])
            block.rect.x = platform[2]
            block.rect.y = platform[3]
            block.player = self.player
            self.platform_list.add(block)


        block = MovingPlatform(100, 40)
        block.rect.x = 1350
        block.rect.y = 280
        block.boundary_left = 1350
        block.boundary_right = 1600
        block.xVel = -1
        block.player = self.player
        block.level = self
        self.platform_list.add(block)


class Level_02(Level):

    def __init__(self, player):

        Level.__init__(self, player)

        self.level_limit = -1000


        level = [[210, 70, 500, 550],
                 [210, 70, 800, 400],
                 [210, 70, 1000, 500],
                 [210, 70, 1120, 280],
                 ]


        for platform in level:
            block = Platform(platform[0], platform[1])
            block.rect.x = platform[2]
            block.rect.y = platform[3]
            block.player = self.player
            self.platform_list.add(block)

        block = MovingPlatform(70, 70)
        block.rect.x = 1500
        block.rect.y = 300
        block.boundary_top = 100
        block.boundary_bottom = 550
        block.yVel = -1
        block.player = self.player
        block.level = self
        self.platform_list.add(block)


def main():
    pygame.init()


    size = [DISPLAY_WIDTH, DISPLAY_HEIGHT]
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Platformer with moving platforms")

    player = Player()

    level_list = []
    level_list.append(Level_01(player))
    level_list.append(Level_02(player))

    current_level_no = 0
    current_level = level_list[current_level_no]

    active_sprite_list = pygame.sprite.Group()
    player.level = current_level

    player.rect.x = 350
    player.rect.y = DISPLAY_HEIGHT - player.rect.height
    active_sprite_list.add(player)


    done = False


    clock = pygame.time.Clock()

    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.moveLeft()
                if event.key == pygame.K_RIGHT:
                    player.moveRight()
                if event.key == pygame.K_UP:
                    player.jump()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and player.xVel > 0:
                    player.friction()
                if event.key == pygame.K_RIGHT and player.xVel < 0:
                    player.friction()

        active_sprite_list.update()

        current_level.update()


        current_position = current_level.bgX
        if current_position < current_level.level_limit:
            if current_level_no < len(level_list)-1:
                player.rect.x = 350
                current_level_no += 1
                current_level = level_list[current_level_no]
                player.level = current_level
            else:

                done = True


        current_level.draw(screen)
        active_sprite_list.draw(screen)


        clock.tick(60)


        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    main()

这是我控制运动和平台碰撞系统的代码部分:

碰撞(位于播放器类中):

hit = pygame.sprite.collide_rect(self, self.player)
if hit:
    if self.xVel < 0:
        self.player.friction()
        correction = self.player.rect.right - self.rect.left
    elif self.xVel > 0:
        self.player.friction()
        correction = self.player.rect.left - self.rect.right

if correction != 0:
    for block in self.level.platform_list:
        block.rect.x += correction

运动(位于游戏循环中):

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        player.moveLeft()
    if event.key == pygame.K_RIGHT:
        player.moveRight()
    if event.key == pygame.K_UP:
        player.jump()

if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT and player.xVel > 0:
        player.friction()
    if event.key == pygame.K_RIGHT and player.xVel < 0:
        player.friction()

【问题讨论】:

  • 你需要更准确地说出什么情况下的行为不端——在没有输入的情况下不横向移动听起来很合理。
  • @DavisHerring 抱歉,您能改写一下吗?我无法理解。
  • 您说您跳跃并撞到平台,然后您必须提供更多输入才能移动。这听起来大部分是想要的行为,所以请澄清问题所在。
  • 当你按下右键时,你必须再次按下它才能在与块碰撞后向右移动
  • 如果我理解了你的问题,我就回答了。

标签: python pygame key collision


【解决方案1】:

对于必须重新按左/右键才能继续朝所需方向的问题:

您应该对按键状态(按下/释放)而不是按键更改(向上/向下)进行操作。这是您可能正在寻找的修改后的事件循环:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done = True
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            player.left_pressed=True
            #player.moveLeft()
        if event.key == pygame.K_RIGHT:
            player.right_pressed=True
            #player.moveRight()
        if event.key == pygame.K_UP:
            player.jump()
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT:
            player.left_pressed=False
            if player.xVel > 0:
                player.friction()
        if event.key == pygame.K_RIGHT:
            player.right_pressed=False
            if player.xVel < 0:
                player.friction()

if player.left_pressed:
    player.moveLeft()
if player.right_pressed:
    player.moveRight()

确保在你的播放器类中添加 left_pressed 和 right_pressed 变量,初始化为 False。另外我有一段时间没有使用 pygame,所以可能已经有 成为可用的关键状态变量,因此您不需要关键更改事件。

【讨论】:

  • 嗯。当我使用它时,如果您停止按右键,播放器仍会卡入块中。我将如何纠正玩家的位置?
  • 这样,friction 的效果会被后续的moveRight 简单地覆盖。边缘触发的速度很好,但这意味着必须仅对位置进行校正。
  • @DavisHerring 那我该怎么办?
  • @BenDover:如果“velocity”代表你没有被阻碍的速度,那么你只需调整位置以防止撞击障碍物而不重置速度,这将恢复每当您越过障碍物时都会产生效果。
  • 我应该解释说我的回答只是为了解决必须重新按键才能继续前进的问题。
猜你喜欢
  • 1970-01-01
  • 2016-06-19
  • 2023-03-04
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
相关资源
最近更新 更多