Side scroller world


Joined
Jan 4, 2021
Messages
3
Reaction score
0
I'm starting out with my own Pygame project and I want it to be a somewhat platformer type game. And I was wondering how I could make an infinite side scroller, meaning I can go back and forth as the code generates the world for me to walk on. Does anyone have any idea of how I can maybe do that?
 
Ad

Advertisements

Joined
Feb 8, 2021
Messages
8
Reaction score
1
Here is a very basic script to illustrate the tecniques for moving the background in order to create the illusion of player movement in a given area. Use the cursor keys to move around. Good luck.

Python:
import pygame as pg

FPS = 40
pg.init()
clock = pg.time.Clock ()
WIDTH, HEIGHT = 900, 600
screen = pg.display.set_mode ((WIDTH, HEIGHT))
pg.display.set_caption ('Scrolling Background Tester')
background = pg.Surface ((1000, 1000))
background.fill ((200, 200, 200))
bg_x = 0
bg_y = 0
for x in range (100, 1000, 100) :
    for y in range (100, 1000, 100) :
        pg.draw.rect (background, (0, 0, 0), (x, y, 20, 20))

while True :
    for event in pg.event.get () :
        if event.type == pg.QUIT :
            pg.quit ()
            sys.exit ()
    keys = pg.key.get_pressed ()
    if keys [pg.K_LEFT] :
        bg_x += 10
    if keys [pg.K_RIGHT] :
        bg_x -= 10
    if keys [pg.K_UP] :
        bg_y += 10
    if keys [pg.K_DOWN] :
        bg_y -= 10
    screen.fill ((0, 0, 0))
    screen.blit (background, (bg_x, bg_y))
    pg.draw.circle (screen, (200, 0, 0), (450, 290), 20)
    pg.display.update ()
    clock.tick (FPS)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top