Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

python - How To Stop Player From Bumping On A Moving Platform

so I am having this problem where when I go on my platform thats moving up everything is alright but when I am on the platform and it starts moving down the player keeps jumping up and down ALSO when I am on the platform and the screen is scrolling up and I am on my platform when its moving up it starts bumping my player basically now both up and down start bumping

as you can see here VIDEO when I am moving up everything starts working until I my screen starts scrolling upwords

same thing for going down VIDEO it will just keep bumping my player

I am not sure if this has anything to do with my scroll up and down but here is how it looks like in my main loop

this part here scrolls my platforms up and down if my player y position is at a certain point

# scroll up
    if playerman.y > 550:
        py -= playerman.fall
        base1.y -= playerman.fall

        
        for collid in collids:
            collid.y -= playerman.fall


# scroll down 
    if playerman.y < 250:
        py += playerman.speed

        for collid in collids:
            collid.y += playerman.speed

here is how my elevator is moving up and down rop1 is my elevator I made it if my screen is not on a certain point then move it up and down +2

    if current_switch == 2:
        if switch1:
            if rop1.y < 200:
                rop1.y += 2

            else:
                rop1.direction = "nomove"


    if current_switch == 1:
        if rop1.y < 300:
            rop1.y -= 2

        else:
            rop1.direction = "nomove"

on my player key movement I made it so when my player is moving right it should move the platforms to the left side and if my player is moving left then it should move platforms to the right side

        
    if keys[pygame.K_RIGHT]:
        if playerman.x > 300:
            px -= playerman.speed
            for collid in collids:
              collid.x -= playerman.speed


            

    

    elif keys[pygame.K_LEFT]:
         px -= playerman.speed
         if playerman.x < 500:
             for collid in collids:
                collid.x += playerman.speed

heres everything in my main loop I have 5 platformers that the player can collid with my player is currently colliding with the lever platform

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                
                if current_switch == 0: 
                    current_move = 1
                elif current_switch == len(switches)-1: 
                    current_move = -1
                current_switch += current_move

                
    # telling what to do when we say the word 'key'
    keys = pygame.key.get_pressed()

    # Shortining playerman.x and playerman.y
    px,py = playerman.x,playerman.y


    
    
    if current_switch == 2:
        if switch1:
            if rop1.y < 200:
                rop1.y += 2

            else:
                rop1.direction = "nomove"


    if current_switch == 1:
        if rop1.y < 300:
            rop1.y -= 2

        else:
            rop1.direction = "nomove"





    # this is our laval
    lava1.x = base2.x - 40
    lava1.y = base2.y + 350
    # this is our laval
    lava2.x = base2.x + 69
    lava2.y = base2.y + 350

    # this is our laval
    lever.x = rop1.x 
    lever.y = rop1.y + 190

    
    # this scrolls our jumping hitbox with the player
    crateco.x = crate1.x + 20
    crateco.y = crate1.y + 60


    # this scrolls our jumping hitbox with the player
    collid4.x = end1.x + 20
    collid4.y = end1.y + 220    



    # move our projectiles up and down
     # Making bird move left and right
    for shot in shots:
        if shot.direction == "down":
            shot.y += 3
        if shot.direction == "up":
            shot.y -= 3
        if shot.y <= 200:
            shot.direction = "down"
        if shot.y >= 700:
            shot.direction = "up"
                

    if textup:
        if playerman.rect.colliderect(text_show_collision.rect):
            text_show_collision.x = playerman.x
            text_show_collision.y = playerman.y
            if text_timer < 100:
                text_timer += 1
                text1.y -= 1
            text1.visible = True

    
            
    if stopmoving:
        if keys[pygame.K_w]:
            if text_timer2 < 250:
                text_timer2 += 1
                text1.y += 2
            
            crate1.x += 1
            for x in range(3):
                x, y = crate1.rect.center
                particles.append( particle( x - 156, y - 60 ) )
                
    if crateco.rect.colliderect(stopmove1.rect):
        stopmoving = False
        







    
    if playerman.direction == "right":
        playerman.direction = "Idle"

    if playerman.direction == "left":
        playerman.direction = "leftid"

# ------------------------------------------------------------------

        
    if keys[pygame.K_RIGHT]:
        px += playerman.speed
        # screen movement
        if playerman.x > 300:
            px -= playerman.speed
            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
            for shot in shots:
                shot.x -= playerman.speed
            
            for collid in collids:
                collid.x -= playerman.speed



        if right:
            playerman.direction = "right"
            
            

    

    elif keys[pygame.K_LEFT]:
         px -= playerman.speed
         if playerman.x < 500:
             px += playerman.speed
             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
             for shot in shots:
                shot.x += playerman.speed
             for collid in collids:
                collid.x += playerman.speed

         if left:
            playerman.direction = "left"



    if playerman.y > 550:
        py -= playerman.fall
        base1.y -= playerman.fall
        base2.y -= playerman.fall
        base3.y -= playerman.fall
        base4.y -= playerman.fall
        base5.y -= playerman.fall
        base6.y -= playerman.fall
        crate1.y -= playerman.fall
        base44.y -= playerman.fall
        end1.y -= playerman.fall
        base45.y -= playerman.fall
        stopmove1.y -= playerman.fall
        text_show_collision.x -= playerman.fall
        text1.y -= playerman.fall
        rop1.y -= playerman.fall
        for shot in shots:
            shot.y -= playerman.fall
        
        for collid in collids:
            collid.y -= playerman.fall



    if playerman.y < 250:
        py += playerman.speed
        base1.y += playerman.speed
        base2.y += playerman.speed
        base3.y += playerman.speed
        base4.y += playerman.speed
        base5.y += playerman.speed
        base6.y += playerman.speed
        crate1.y += playerman.speed
        base44.y += playerman.speed
        end1.y += playerman.speed
        base45.y += playerman.speed
        stopmove1.y += playerman.speed
        text_show_collision.x += playerman.speed
        text1.y += playerman.speed
        rop1.y += playerman.speed
        for shot in shots:
            shot.y += playerman.speed
        
        for collid in collids:
            collid.y += playerman.speed


            
#--------------------------------------------------------------------------
 




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






    collideBottom = False




    if keys[pygame.K_RIGHT]:
        if playerman.direction == "jump2":
            playerman.direction = "jump"


    if keys[pygame.K_LEFT]:
        if playerman.direction == "jump":
            playerman.direction = "jump2"

    

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


    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 0.5
        playerman.isJump = False

    # For player to get on top of platform
        collide = False
        for collid in collids:
            if playerman.get_rect().colliderect(collid.rect):
                collide = True
                playerman.isJump = False
                right = True
                left  = True
                playerman.JumpCount = 10
                playerman.y = collid.rect.top - playerman.height
                if playerman.rect.right > collid.rect.left and playerman.rect.left < collid.rect.left - playerman.width:
                    playerman.x = collid.rect.left - playerman.width
                if playerman.rect.left < collid.rect.right and playerman.rect.right > collid.rect.right + playerman.width:
                    playerman.x = collid.rect.right
 
                if playerman.direction == "jump":
                    playerman.direction = "Idle"
                    
                if playerman.direction == "jump2":
                    playerman.direction = "leftid"
                
                




            # Making it so player wont fall out of the map 



        # Player jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
                right = False
                left = False


            playerman.fall = 0

    else:



        for collid in collids:
            if (playerman.get_rect().colliderect(collid.rect) and 
                playerman.y + playerman.height > collid.rect.bottom):

                collideBottom = True
                playerman.y = collid.rect.bottom
                break


        if not collideBottom and playerman.JumpCount >= 0:    
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
            if playerman.direction == "right" or playerman.direction == "Idle&quo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...