首页 > 解决方案 > Pygame 碰撞不断传送我的玩家如何修复?

问题描述

我不知道为什么,但是当我在移动我的播放器时按下右键然后按下左键时,它会出现故障,它会抛出矩形VIDEO它完全可以反过来工作,但是如果我按住右键然后它会继续这样做在按住右键的同时单击左键它会传送我扔块我不知道如何解决这个问题任何帮助表示赞赏!VIDEO <另一个问题的视频

我对双方的碰撞

    # sides for player and player screen movement
    platform_rect_list = [p.rect for p in collids]
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)

    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px

    move_right = keys[pygame.K_RIGHT]
    move_left = keys[pygame.K_LEFT]
    if move_right:
        base1.x -= playerman.speed
        base2.x -= playerman.speed
        base3.x -= playerman.speed
        base4.x -= playerman.speed
        base5.x -= playerman.speed
        base6.x -= playerman.speed
        crate1.x -= playerman.speed
        base44.x -= playerman.speed
        end1.x -= playerman.speed
        base45.x -= playerman.speed
        stopmove1.x -= playerman.speed
        text_show_collision.x -= playerman.speed
        text1.x -= playerman.speed
        rop1.x -= playerman.speed
        text22.x -= playerman.speed
        text_show_collision2.x -= playerman.speed
        portal1.x -= playerman.speed
        base46.x -= playerman.speed
        for shot in shots:
            shot.x -= playerman.speed
        
        for collid in collids:
            collid.x -= playerman.speed
            

    if move_left:
         base1.x += playerman.speed
         base2.x += playerman.speed
         base3.x += playerman.speed
         base4.x += playerman.speed
         base5.x += playerman.speed
         base6.x += playerman.speed
         crate1.x += playerman.speed
         base44.x += playerman.speed
         end1.x += playerman.speed
         base45.x += playerman.speed
         stopmove1.x += playerman.speed
         text_show_collision.x += playerman.speed
         text1.x += playerman.speed
         rop1.x += playerman.speed
         text22.x += playerman.speed
         text_show_collision2.x += playerman.speed
         portal1.x += playerman.speed
         base46.x += playerman.speed

         for shot in shots:
            shot.x += playerman.speed
         for collid in collids:
            collid.x += playerman.speed




            

    platform_rect_list = [p.get_rect() for p in collids] # get_rect()
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)

    playerman.y = py
    cI = player_rect.collidelist(platform_rect_list)
    if cI >= 0:
        # undo movement of platforms dependent on the direction and intersection distance
        dx = 0
        if move_right: 
            dx = platform_rect_list[cI].left - player_rect.right
        if move_left:
            dx = platform_rect_list[cI].right - player_rect.left

        for collid in collids:
            collid.x -= dx
            collid.get_rect() # update rectangle

        base1.x -= dx
        base2.x -= dx
        base3.x -= dx
        base4.x -= dx
        base5.x -= dx
        base6.x -= dx
        crate1.x -= dx
        base44.x -= dx
        end1.x -= dx
        base45.x -= dx
        stopmove1.x -= dx
        text_show_collision.x -= dx
        text1.x -= dx
        rop1.x -= dx
        text22.x -= dx
        text_show_collision2.x -= dx
        portal1.x -= dx
        base46.x -= dx
        for shot in shots:
            shot.x -= dx
        

标签: pythonpygame

解决方案


很可能是按下leftright同时短暂地导致问题。

按下两个按钮时根本不要移动播放器。清理您的代码,使其更易于维护和理解:

object_list = [
    base1, base2, base3, base4, base5, 
    crate1, base44, end1, base45, stopmove1,
    text_show_collision, text1, rop1, text22,
    text_show_collision2, portal1, base46]

def move_in_x(dx):
    for obj in object_list:
        obj.x += dx
    for shot in shots:
        shot.x += dx
    for collid in collids:
        collid.x += dx
# sides for player and player screen movement
platform_rect_list = [p.rect for p in collids]
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)

playerman.y = py
if player_rect.collidelist(platform_rect_list) < 0:
    playerman.x = px

move_right = keys[pygame.K_RIGHT]
move_left = keys[pygame.K_LEFT]

if move_right and not move_right:      # <--- move to the left, but not to the right
    move_in_x(-playerman.speed)
    
if move_left and not move_left:        # <--- move to the right, but not to the left
    move_in_x(playerman.speed)

platform_rect_list = [p.get_rect() for p in collids] # get_rect()
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)

playerman.y = py
cI = player_rect.collidelist(platform_rect_list)
if cI >= 0:
    
    if move_left != move_right:        # <--- move either left or right
    
        # undo movement of platforms dependent on the direction and intersection distance
        dx = 0
        if move_right: 
            dx = platform_rect_list[cI].left - player_rect.right
        if move_left:
            dx = platform_rect_list[cI].right - player_rect.left

        move_in_x(-dx)
        for collid in collids:
            collid.get_rect() # update rectangle

推荐阅读