0
\$\begingroup\$

I am trying to figure out how can I make a side-scrolling view in Pygame.

If I am moving right or left, I want the portion of the level shown on-screen to shift to follow the player character.

Here is an animation of my game:

Platformer animation

import pygame
pygame.init()


window = pygame.display.set_mode((500,500))
pygame.display.set_caption("GET LOAD OF THIES")


# Character class
class players(object):
    def __init__(self,x,y,height,width):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.speed = 5

# enemy class 1
class enemys(object):
    def __init__(self,cordx,cordy,cordheight,cordwidth):
        self.cordx = cordx
        self.cordy = cordy
        self.cordheight = cordheight
        self.cordwidth = cordwidth

# enemy class 2
class enemyss(object):
    def __init__(self,xs,ys,sheights,swidths):
        self.xs = xs
        self.ys = ys
        self.sheights = sheights
        self.swidths = swidths

class lava(object):
    def __init__(self,lavax,lavay,lavawidth,lavaheight):

        self.lavax = lavax
        self.lavay = lavay
        self.lavawidth = lavawidth
        self.lavaheight = lavaheight



# enemy class 3
class enemysss(object):
    def __init__(self,velx,vely,velheight,velwidth):
        self.velx = velx
        self.vely = vely
        self.velwidth = velwidth
        self.velheight = velheight


# Colors
NiceBlue = (0,214, 82)
NiceGreen = (214,0,82)

# FPS
FPS = 60
clock = pygame.time.Clock()

# Defininition for the class player/enemys
playerman = players(50,390,20,20)
enemy1 = enemys(150,390,100,10)
enemy2 = enemyss(320,320,100,10)
enemy3 = enemysss(120,250,100,10)
enemy4 = lava(0,459,500,40)


# Main loop
runninggame = True
while runninggame:
    # Frames per second
    clock.tick(FPS)
    # Exit Event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False
        #Draw The enemys and Main Characte
    window.fill((0,0,0))
    player = pygame.draw.rect(window, (NiceGreen), (playerman.x,playerman.y,playerman.height,playerman.width))
    enemy = pygame.draw.rect(window, (NiceBlue), (enemy1.cordx,enemy1.cordy,enemy1.cordheight,enemy1.cordwidth))
    soenemy = pygame.draw.rect(window, (NiceBlue), (enemy2.xs,enemy2.ys,enemy2.sheights,enemy2.swidths))
    dudeenemy = pygame.draw.rect(window, (NiceBlue), (enemy3.velx,enemy3.vely,enemy3.velheight,enemy3.velwidth))
    thisanotherenemy = pygame.draw.rect(window, (230,4,231), (enemy4.lavax,enemy4.lavay,enemy4.lavawidth,enemy4.lavaheight))


    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        playerman.x -= playerman.speed
    if keys[pygame.K_RIGHT]:
        playerman.x += playerman.speed
    if not playerman.isJump:

        playerman.y += playerman.fall
        playerman.fall += 1
# ----------------------------------------------------- # enem1 collisio
# both of my 2 enemy squares collisions push me back when ever I Jump on the top of them on there sides but when I jump on the middle of of both of them it seems to work if I just want it so when I jump on both of my squares I just don't get pushed back 
        player.topleft = (playerman.x, playerman.y)
        collide = False
        if player.colliderect(enemy):
            collide = True
            playerman.y = enemy.top - player.height
            if player.right > enemy.left and  player.left < enemy.left - player.width:
                playerman.x = enemy.left - player.width
            if player.left < enemy.right and  player.right > enemy.right + player.width:
                playerman.x = enemy.right

        if player.colliderect(soenemy):
            collide = True
            playerman.y = soenemy.top - player.height
            if playerman.y > soenemy.left and player.left < enemy.left - player.width:
                playerman.x = soenemy.left  - player.width
            if player.left < soenemy.left and player.right > soenemy.right + player.width:
                playerman.x = soenemy.right
        if player.colliderect(dudeenemy):
            collide = True
            playerman.y = dudeenemy.top - player.height
            if playerman.y > dudeenemy.left and player.left < dudeenemy.left - player.width:
                playerman.x = dudeenemy.left  - player.width
            if player.left < dudeenemy.left and player.right > dudeenemy.right + player.width:
                playerman.x = dudeenemy.right
        if player.colliderect(thisanotherenemy):
            collide = True
            playerman.y = thisanotherenemy.top - player.height
            if playerman.y > thisanotherenemy.left and player.left < thisanotherenemy.left - player.width:
                playerman.x = thisanotherenemy.left  - player.width
            if player.left < thisanotherenemy.left and player.right > thisanotherenemy.right + player.width:
                playerman.x = thisanotherenemy.right





        if player.bottom >= 500:
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = 500 - player.height

        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0
    else:
        if playerman.JumpCount > 0:
            playerman.y -= (playerman.JumpCount * abs(playerman.JumpCount)) * 0.3
            playerman.JumpCount -= 1
        else:
            playerman.JumpCount = 10
            playerman.isJump = False



        # update winodw event
    pygame.display.update()
# quit event
pygame.quit()
\$\endgroup\$
0
\$\begingroup\$

You'll basically want to define a Camera class. In this case you could make it as simple as being only a screen offset, but a zoom factor might be nice too. This camera could then have a function that converts world coordinates into screen coordinates, which you would call when drawing objects.
The position of this camera could then be changed based on the position of the target it is tracking.

As a side note: You may want to use Rects for your collision detection, that would be provided by your player and enemy classes. Rects have convenient functions like clip, colliderect and collidelist that could make your code a lot simpler.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.