Friday, 5 February 2021

Scrolling Down Teleports My Player Pygame

so I am having a problem when ever I have my scrolling for up and down for my player and I go on high ground and then jump down with my player it will glitch my collision logic I am not sure why its doing it heres a video for example VIDEO < as you can see I am on high ground then when I jump down for some reason I glitch everywhere

my scrolling up down I made it so when my player y is at 50 it should scroll my player up and when my player is at 550 it should scroll my player down with the fall speed of my player but when ever I have this code in my main loop it will cause a glitch with my collideable rects even though I made my collisions for all sides to stop the player.

the collides are the rect the player will be colliding with


    if playerman.y > 550:
        for obj in object_list:
            obj.y -= playerman.fall
        for shot in shots:
            shot.y -= playerman.fall
        
        for collid in collids:
            collid.y -= playerman.fall
        tall11.y -= playerman.fall



    if playerman.y < 50:
        for obj in object_list:
            obj.y += playerman.speed

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

        tall11.y += playerman.speed

            

my collisions part up down left and right for the player

    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_d]
    move_left = keys[pygame.K_a]
    if move_right:
        for obj in object_list:
            obj.x -= playerman.speed
        for shot in shots:
            shot.x -= playerman.speed
        
        for collid in collids:
            collid.x -= playerman.speed

        tall11.x -= playerman.speed

        
    if move_left:
        for obj in object_list:
            obj.x += playerman.speed


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

        tall11.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()
        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
        tall11.x -= dx

        for shot in shots:
            shot.x -= dx



    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
                playerman.JumpCount = 10
                right = True
                left = True
                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"

                        



        if collide:
            playerman.isJump = False
            playerman.fall = 1
            

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

    ##  playerman.isJump = False  <--  omit this line.  already False
        # What will happen when player jumps
    else:

        collideBottom = False
        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":
                playerman.direction = "jump"

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




        else:
            playerman.isJump = False
            playerman.JumpCount = 10


    # redrawing the window
    redraw()

    # updating the game
    pygame.display.update()
# quiting the game
pygame.quit()

you can run and test this with rects no images it works the same way jump on top of the the platforms that are on the sky and then jump down and you will start glitching for some reason even though the collisions work for both sides [script](https://pastebin.com/CCcAv3Hm)



from Scrolling Down Teleports My Player Pygame

No comments:

Post a Comment