This video uses Python and the PyGame module to demonstrate how to create an endless infinite game background that will scroll forever in your Python games!
Pygame is a great module for building simple games in python, and makes having an infinite background really easy! This is great tutorial for python and PyGame beginners or people familiar with Object oriented programming already, but new to the PyGame module!
If you have any questions or want to see something specific in the future, be sure to let me know about it in the comments below and I'll get right back to you! Be sure to leave a like on the video and subscribe to the channel to stay up to date with all our future content!
Thanks so much for watching and good luck with your code!
Timestamps: 00:00 Introduction 01:31 Pygame setup 5:00 Loading an Image and Drawing it 11:37 Moving the Background Image 17:20 Outro
what's going on guys welcome back to the
channel in today's video i'm going to
show you how to create an infinite
scrolling background for your game using
pygame and python
[Music]
so uh there are a few different types of
game where it would be useful to have
essentially an infinite background i
mean there's definitely some games where
you're going to want to pre-define the
whole background
but a lot of times if you're doing like
an infinite runner game like the type
that we've done on this channel a few
times we did a like a flappy bird style
game and then we also did just an
endless like jumper where infinite
enemies come at you and in both of those
scenarios it could be really useful to
have an image that would make your give
your game a really cool background and
it would essentially
replicate itself infinitely until the
game was over and i'm going to show you
how to do it going forwards and
backwards so um there's a few times that
you would want this sort of thing
but you would want it not controlled by
a player you would just want the map
constantly moving past you whereas
something like if you think of the
classic game like jetpack joyride or
like flappy bird
the background is moving unrelated to
the player you just control like the
y-axis movement
and then there are a lot of games like
side-scrolling shooters or platformers
where you're in control of the movement
and you want that to also control where
the background is i'll show you how to
do both in this video all right so let's
quickly set up a pie game and
start by importing pygame and since i
want to use
some of the built-in like math module
functions i'm also going to import math
there is a way that you can
do it without importing math i'll talk
about it when we get there but i'm going
to import it make our lives easier
so then if we do pygame dot init
that initializes the pi game module and
you want to start that right in the
beginning
and then always set up a timer so
some people call it clock some people
call it timer
it doesn't matter i usually use timer so
to be consistent on this page i'll use
timer as well
but you could call it clock as well and
then create a frame rate variable we'll
have it run at 60fps
set up a width and a height for your
page for your screen so we'll do width
of 1600 so that we can really see like
multiple
panes of our photo
and then height 400 and then
let's just go ahead and start there
we'll do screen equals and then pie game
dot display dot set underscore mode
parenthesis and then in square brackets
give it your width and your height
variables and that's going to set uh
your screen and then um if you want you
will go ahead and do this set caption
and that's going to be the text that
pops up on the top menu bar and we'll
just call this side scroller
and
let's go ahead and just do kind of the
minimum we have to do to get the window
to open so we'll say run equals true and
then wall run
and then this in here is where you put
your clock.tick fps i always call this
the main game loop i got some extra
extra colon here i always call this the
main game loop i think that's a good way
of thinking about it like everything
underneath this wall running is what you
want to have happen every iteration of
your game
so that should actually be timer.tick
i'll go ahead and make my text quite a
bit bigger i know these uh photos can be
or these uh videos can be hard to follow
when the text is real small
okay so it's timer.tick at your frame
rate and then uh the next thing you have
to do just to get a window to open is
going to be for event
in
pygame.event.get
and if you've seen any of our past game
videos you know the first one that you
want to put in is if event dot type is
equal to pi game dot quit all caps and
then run
equals false and you can call this
running you can call this like
level or whatever you want but um the
basic what this is doing is saying okay
the only thing that's going to let us
exit this game loop is hitting the quit
button but that's actually super
important because uh if you don't do
that you'll have an infinite while loop
and that will crash your computer it's
well not necessarily crash to your
computer but crash your program for sure
okay then
pygame.display.flip and then pygame dot
quit and i'll go ahead and run this
we're not actually putting anything on
the screen so i don't know that we're
gonna see yeah we just get a window
we're not technically filling it with a
black background this is just its
default but the reason i made it so wide
will be pretty clear once we load in
this background image so that's just
this is the basics of what we need to
get pygame initialized let's go ahead
and get our image in here so i'll pull
it up this is all i'm going to use for
my background i literally just went to
google and i typed in
like infinite looping background you can
create one if you want there's also
tutorials for how to do that but this is
a code channel not an art channel
so i trust you can figure that out i've
but you can kind of see like where this
branch hits the right edge of the image
is also like right where it's going off
screen on the left so they split this up
to be an infinite image which is pretty
cool
all right so let's go ahead and
load that image into the game so to do
that let's go down below where we set
the caption and i'll just
make a variable called background
and to get an image in here it's just
pygame.image.load
and then the name of your image and so
drop it into the same folder as your
game and that's going to make
referencing it um really easy
and so this is bg trees i have one for
space two but it's huge and it doesn't
really loop very well it's actually just
my desktop background
um but i was using it while i was
playing around with this so
okay and then let's get the width of
that which will be background width and
we can use um a built-in get width
function so we just do background dot
get
underscore width and the reason we want
to do that is that's going to tell us
how many we can fit in our window that
we made
doing some math which everybody loves
math
and then we're going to set the
background equal to
something that we're going to
essentially scale to be the right height
so this might not be
something that you have to do if you've
picked a background image that's already
400 pixels high
but you can see that i mine is not just
from looking at it it's
360 by 270 so if i were to load this in
i would still have 130 pixels um below
it that are just black so
this is an optional step depending on
the background that you're using but
this will make sure that it fits
properly in my window so it you do
pygame dot transform dot scale
and then uh
the
image so background and then the size
you want to be which is going to be
background width comma
height so just like that and this is
going to take the same image we already
loaded in and it's going to scale it to
be the right height
and then we will go ahead and get the
rectangle affiliated with it just so we
can do some rectangle functionality so
it's going to be background dot
actually background underscore rect is
going to equal background dot
get rekt and so the get rekt
the pi game module gives you a lot of
built-ins that really make this pretty
easy and pretty fast and then we're
going to control our movement left and
right with a variable i'll call scroll
so we'll just initialize it up here
but then the number of tiles or i guess
one way to think of this would be like
panels so the number of background
images that you need to be drawing at
any one time is going to be equal to
math.seal and here's why i brought in
the math module because this is going to
be a division that will always round up
so we're going to divide the width of
the screen by background width and then
i'm going to add 2 to it
so it's always rounding up
and actually once i get these drawn on
the screen
i will come back to this
and i will modify this to show you
like
i i hope i will remember to come back to
this and modify this to show you like
how to uh um what this is doing and why
we want to add two to it but basically
if you divide the screen up and let's
say your image fits perfectly well as
soon as you go left or as soon as you go
right
you are going to have an image that's
not yet loaded to the right
and so when we do the drawing we need to
tell it how many different images to
draw and then what condition is going to
cause it to stop drawing one to the left
end and start drawing new one to the
right end
it's going to be easier to show than
talk about but just now i'm putting this
plus two in because we want one extra
behind us and one extra in front of us
so no matter where we're moving there's
always a little bit of runway
and then to actually get these on the
screen we'll come down just below our
timer.tick and we will set up a for loop
to draw them so we'll say for i in range
for i in range there we go
panels so this is going to loop through
however many panels the math determines
we need to do and it's going to be
screen dot blitz so that's block
transfer it's going to
transfer the screen the image onto the
screen
and that's the background image and then
we give it uh x and y coordinates and so
the x coordinate is going to be
i
um times the background width that we
got
um and then plus
scroll so initially this is going to be
zero so when it loads in it'll be
in the right spot but
but this is
going to be how we control the movement
and then once the scroll variable gets
larger than the width that's when we
reset things so
so essentially we want to draw it in i
times background width plus scroll but
this is going to start at zero so we
won't have anything to the left of our
image
so we actually want to do minus
background width and this is going to
shift everything one block to the left
so the first image will be drawn off
screen to the left and then the last
image will be drawn off screen to the
right
so that's what this for loop is going to
do and that should look pretty good let
me see so that's probably going to give
us the background
yep and we can't move it yet but that's
okay you can see just from one image and
remember this uh i think it's this
branch that's actually like where the
panels break but if you have a good
image a good repeating background
you can't even necessarily see the line
at which the next image is being drawn
and if you could see to the left and to
the right there's going to be one full
panel being drawn on both sides of it as
well right now
okay so let's go ahead and carry on and
now let's actually incorporate how you
would move so i'll show like if this was
just an endless runner your task might
be nearly complete because you'll just
put some variable that you want the game
to the background to move at
um so you'll just make it whatever five
and then you'll want to put in here if
absolute value of scroll is greater than
bg width and actually this isn't going
to be
5 this is going to be plus equal to 5.
but so um
if the scroll is greater than the width
and i might need to make that minus five
we'll see in a second let's see if this
works
but if it's greater than the width of
your image and i called it background
width
then that's when we want to set scroll
equal to zero so let's go ahead and
check what that's doing this may
actually move everything to the wrong
way yeah so this would not be the normal
way to run a game let's do minus equals
and that's why
absolute value is so important if you're
going left or right this will handle it
but i'll just go ahead and do this so
what this is doing is every loop it's
moving every image five pixels to the
left
and that's basically how
the continuously moving games are played
so games where the player is only
controlling one dimension like up or
down but that's actually i think a
minority of games i think what's more
common is you use the left and right
arrow keys to run around the map and
that's what should control what
background is iterating so that's pretty
cool you've got the concept now if
you're familiar with event handling then
maybe you're done with the video if you
are done with the video please subscribe
to the channel leave a like on the video
it really helps me out a lot and let me
know in the comments below what i could
have done better or what you'd like to
see in a future video
but if you are here and you want to see
how to add some player control to the
background let's continue
so if event dot type
is equal to pi game dot key down and now
we'll actually look at the arrow keys to
see if the player wants to move left
wants to move right and we'll say if
event dot key is equal to pi game dot
and then k underscore right so this is
you click the right arrow key then what
we will say is direction
equals minus one and the reason i'm
doing that is this is background
direction so if you go right you're
actually your player would be staying in
place but the whole background should
move to the left so you'll see why i do
do it that way in a second but if say
you had a player movement and uh
background movement you would probably
want this to be a separate variable
that's like um bg direction or something
like that
and then pi game dot left will actually
k left we'll actually make that
direction equals positive one
copy that in here
and what we need to do is i'm going to
basically take this whole thing and on
key up oops control z on key up we want
to reset direction to zero so you can
take pretty much all the same code key
up and then if pi game is uh if you go
up on left or right direction is equal
to zero
but you still want to add these because
you don't want to be like oh i was
holding down the a button for no reason
and then direction went to zero because
i let go of it even though i was still
holding an arrow key down so adding this
second layer of if is
important but now we're not using that
direction variable anywhere so what we
want to do is we want to come up into
scroll and we want to start by making
direction equal to zero and if you think
about what we just did well we want
scroll to be plus equals to now
basically
five
times
the direction so if we tell it to move
left then it's going to be adding five
to it every iteration if we tell it to
move right and this would actually i
think be better as a variable so we'll
come up here we'll make a speed variable
speed
equals five and in a lot of games
there's things like power-ups or speed
improvements all you would have to do is
temporarily make your speed higher while
you have that power up and that would
increase your speed or as the game gets
progressively more difficult as your
score gets higher you add one to that
speed every 20 points or something so
let's see if i press the right arrow key
you can see the whole background moving
to the left and i let go of it and if i
press the left arrow key it's moving to
the right so our our movement is working
perfectly um
now maybe just to give an idea like
where we are where a player would be
relative to this window i'll go ahead
and draw a quick shape on so i'll say
player is equal to pygame.draw.rect
and we're just going to throw it on the
screen i'll give it like a yellow gold
so that's some r and some g but no b
and then we'll put it on the screen at
about quarter width so it'll have like
three quarters of the screen in front of
it always and then we'll put it at
about three fourths down as well so
we'll do height times times .75
and then let's just make it a 50 by 50
rectangle and i'm going to make it a
hollow rectangle
and i'm gonna round it so that's what
that
five is saying border width and eight is
saying how rounded to make the radius
but now we should see no matter where we
move we've got the background moving and
our player stays in place and so this
will give an illusion of the player
actually like running forward even
though what we're doing is sliding the
background back and that's a really neat
trick that's super common in game design
um and so that's even like in the flappy
bird tutorial on this channel we just
move stars in the distant background to
the left slowly and it makes the player
feel like he's moving forward dodging
obstacles
so that's it that's the tutorial i hope
you found it useful um
you know if you have any questions about
what you saw here today why we did
things a certain way or you tried it and
it's not working for you just let me
know about the comments below i'll help
you out in any way i can and
i really appreciate likes on the video
subscribes to the channel it helps me
out a ton all of the support i've been
getting so far has been great and as
always good luck with your projects and
thanks for watching thanks bye