This tutorial covers how to add edge limits inside of endless scrolling python platformer or scrolling shooter style games using the Pygame module! This is a great tutorial for programmers of all skills levels, beginner, intermediate or experienced and just new to the Pygame python module!
If you have a specific question or want to see something in a future video be sure to let me know about it in the comments! And if you enjoyed this content or found it useful please subscribe to the channel and leave a like on the video!
Thanks for watching and good luck with your projects!!
Chapters: 00:00 Introduction and Background 1:50 Tracking Total Distance Traveled 6:30 Creating Edge Limits and Finish Line 10:40 Stop Scrolling Near Boundaries 18:00 Outro
what's going on guys welcome back to the
channel in today's video i'm going to
show you how to create left and right
boundaries to your infinite scrolling
games in python using pygame
[Music]
after i posted my previous video on how
to create an infinitely scrolling game
with an endless background i had a few
comments saying okay but let's say you
have a game like mario where maybe you
have a repeating background but there is
eventually an end limit and you want to
limit to how far back you can go so
you're not just creating like a flappy
bird style game where there's no end and
you want to scroll infinitely so that is
not particularly hard to do i thought it
would be a great follow-up to the
previous video so if you have any
questions about what you see in this
video we are starting from kind of some
code that we already wrote so be sure to
check that video out if you need some
assistance or let me know in the
comments below what questions you have
and i'll get back to you as soon as i
can without any further ado let's get
into the tutorial okay so for this
tutorial we are starting with a program
that already scrolls to the left and to
the right infinitely
so again if you haven't followed the
previous tutorial i'll uh put a card to
it somewhere it may have already popped
up
and i'll leave a link to it in the
description of this video or you can
just pause on this screen and then pause
on this screen it's only about 50 lines
of code you can copy it down if you want
or if you have your own game already
built and you just need the concepts in
this video that's great you're all set
just understand we are starting from a
bit of a platform for this video so
now that we're getting into it um let's
say our overall goal is to add a finish
line somewhere in the far f distance of
your game and also a back wall so you
can't just scroll backwards endlessly
either
and to do that it's it's not too
complicated we just want to essentially
set up some overall position tracking so
we have this variable scroll that is
keeping track of how far in the left and
right we go but we're only using it to
reset how many panels we need to draw
and that's how we handle making sure we
have infinite
panels getting drawn in front of us and
behind us but what we want to do is add
a variable that we'll call total
distance i think that's a logical name
for it and we will also add to that
speed times direction and you'd say well
why not just add scroll to it because
it's already getting done here but
scroll is going to be this same variable
but once it gets to background width
which in this case it's hard to say it's
however why your images but mine would
be like 400. so we'd be tracking up to
400 and then and then we would have to
move on whereas this variable we're
never going to reset it to zero so we
have this now that's going to constantly
be incrementing how far we go
and then uh to make this a little more
useful we'll put it on the screen but to
do that we have to just quickly set up a
few things that we didn't do in the
previous game so we'll call this episode
2 of this tutorial series and we're
going to create a few things so we'll
create that variable total distance
because you have to initialize every
variable that you want to use and when
the game loads in it'll be zero so
that's perfect
and let's set up a font um to do that
that's what you have to do to use text
in your games and that's going to be
pygame.font.font
and i'll make the text a little bit
bigger because i get a lot of comments
saying this the text is kind of hard to
read i'm sorry programming it's not
normal to have a huge text but so
pygame.font.font
and then you need the name of your ttf
file so
i've covered this in previous videos
there are a few built-in fonts so like i
use freesands bold ttf because that's
built into my system and pi game
but if you need a font then just drop a
ttf file into your folder they're free
online and then you just give this a few
other arguments
true and then
uh font size so we'll do 24 that's big
enough to read but not like
uncomfortably large
okay
and now we'll put this total distance on
the screen so to do that it's
font.render
and then
you give it what text you want
and we want this to so we'll do a
formatted string that says
distance traveled
and
then we'll in these curly brackets put
total distance in there and if you have
units you can put something here meters
feet um really any units because it
doesn't matter um so you know you could
say we've traveled uh that many
elephants so we'll do that and then
there's a two times um there's a
two-step process so step one is define
the text and then step two is and you
gotta call this something we'll call it
distance text
and then step two is you actually draw
it onto the screen so uh screen.blit
distance text and then you give it
and in the font.render you also need to
give it uh oops this is where you give
it um an alias and then a color
and actually i don't know that we needed
this true up here i think we just need
font size sorry i think i got those
backwards
um
okay and then down here all you have to
do is give it the text name and then
where you want to be drawn so we'll make
it uh x and y starting position of 50 so
that'll get off the left edge a little
bit and then we'll do
a height minus 50. so that'll get it up
from the bottom a little bit but this is
just how i'm going to show you what our
total distance variable is doing so now
if i run this you'll see we have
distance traveled we've gone zero
elephants again units doesn't matter i
just thought that'd be funny and
remember the scroll variable is maybe
backwards from the way you would think
it is because we're using it to move the
background so as i go to the right it's
actually creating a negative variable
and so what you would probably want to
do what i'm going to do here i'm going
to get rid of elephants i'll put feet in
here
sorry if you guys are metric just put
meters
but what i'm going to do is i'm actually
going to use total distance times
negative 1 because i want it to count
up as i go to the right and i want to
count down as i go to the left all right
but now you can see we have this counter
that's pretty cool it's counting how far
we've traveled in total but we haven't
put any limits in overall so let's say
that you wanted to add an ultimate
barrier and a back uh so like a a
finishing barrier at 3 000 feet in in
front of you and a back wall a thousand
feet behind where you start well let's
start by just calling it back wall
and we'll give it a variable actually
we'll say back distance
of negative a thousand so a thousand
feet behind you and we'll say finish
line will be 3000 in front of you okay
now we're going to use those variables
to
track
overall how far the player has gone and
you'll see how we'll do that so
what i'm going to do is i'm going to
come down underneath the player position
and i'm going to put in two new
walls i guess i'm going to make them
rectangles because collision is really
easy in pie game with rectangles
but you can you could make them images
if you want so pygame.draw.rect
we'll do screen and i'm going to make
the back wall red just so you can easily
distinguish between the two in the game
and we're going to talk about the
position for it but the x and y starting
position for the back wall should be
relative to that back wall and the total
distance that we've traveled because
you're not just going to draw a wall at
negative 1000 you're actually going to
draw a wall that moves as the player
moves until you get close enough to
finishing the game alright so think
about your x starting position it should
actually be
back distance plus total distance
traveled so again remember total
distance is going to be a negative as
you move forwards so
if you want this wall which is initially
going to be drawn at negative 1000
to
appear on screen as you travel backwards
then you're going to add the total
distance and once we've traveled a
thousand feet backwards then it's going
to appear on the left edge of the screen
this will be a little easier to just
show it
and then we'll say we'll just make it a
vertical line that spans the whole
screen we'll make it six wide and we'll
make it uh the whole height of the
screen so this should be all we have to
do to see the back wall i'm going to go
ahead and draw the front wall right now
too so i can demonstrate them both and
we'll call this finish wall
and instead of red i'm going to make
this one green
and then
for this one we want to use finish
finish line distance plus total distance
again because we're using that total
distance variable so it's going to be
like negative 2000 when the finish line
which is getting drawn at 3 000
is is actually going to come onto screen
or 1400 whatever it's going to be
so but this one because this is an x and
y starting position the only way we
would see it at the exact time we want
to is if we also subtract the width of
it
and maybe that'll make a little more
sense once we've stopped creating the
scrolling but let's just go ahead and
see if that works so
we're going backwards and at a thousand
i would hope to see this rectangle okay
and there we go that's cool that's a
finish line but we're not actually
finishing the game yet okay you
understand so now i'll go ahead and go
this way
and we're gonna hit the
front what are we calling finish line so
we hit the back wall here's the finish
line and bam and you might say well we
made the finish line 3000 why is it uh
right here but that's because the player
is being drawn at approximately 500 and
we're adding
we're adding uh the distance traveled so
it when we hit exactly 3000 that's
actually when the line leaves the screen
so there's ways to compensate for that
if you want like you could say finish
line equals three thousand and then plus
width divided by four but to be honest
what you're probably going to do is you
probably have a set
game in mind you've probably created a
platformer or something like that and
you're probably just going to do this
with trial and error so there's your
finish line but now we have to actually
um handle how to stop scrolling at the
edges when we
we have to handle stop scrolling at the
edges when we get either of those in
screen so let's go ahead and add a new
variable that i'll call
boundary limits
and it's going to be equal to false when
we start
but we're going to come down here uh to
the bottom
it really doesn't matter where you put
this code i'm just putting it all at the
bottom so i'm differentiating between
the old stuff we already did and the new
stuff
and what we're going to say is we're
just going to set this boundary
limit based on the total distance so
we'll say
if
total
distance
total distance is and now we have to be
careful with sine here because it goes
positive as we go backwards so we'll say
greater than or equal to 1 000.
yeah 1000 which is when the back line
comes onto the screen
or
total distance
is going to be less than or equal to and
then 1400 because we've moved forward
1400 and remember the screen is 1600
wide
the finish line is being drawn at 3 000
plus total distance so it's gonna come
on to screen as soon as we hit 1400 and
if these conditions are true then we
want that boundary limits equal to true
and then else
boundary limits
bound or bound
shouldn't have picked a variable is so
hard to spell
boundary limits equal to false okay and
so but we're not doing anything with
that variable yet we set this variable
up that checks whether or not boundary
limits have been met but what we need to
do now is we need to come up to our
scrolling functionality that checks
whether or not to move the background
and we just want to add an if statement
that says if not
boundary limits okay
and if not boundary limits we're going
to do all of this but then what we'll
say is else
so this means we have hit the limits
then we're going to
move the player's position now so player
x plus equal to
speed times direction so when we're not
at the limits of the screen the player
is going to essentially stay in one
place and the screen is going to move
behind him that's how mario works that's
how platformers typically work you move
everything behind the player but the
player more or less is in one spot
that's the easiest way to do like motion
control and then once we get to these
boundary limits we're going to move the
player's position the only problem is we
are currently not drawing the player
um based on a variable so we want
instead of that x width divided by four
we want to get drawn based on a variable
and in the beginning we're still gonna
set player x equal to width divide by
four so that's where we want the game to
start but then we get to the edges of
our game and we'll start moving the
player
this is pretty close to done already
actually
but what you'll see when we load this up
is we still have some issues with this
code so let's go ahead and just run it
real quick let's go backwards because
it's faster
oh there we go
and we're gonna hit a thousand so
there's our line you can see it but now
what you'll see is kind of weird uh one
we definitely did that backwards but two
we can still travel so hang on i did do
that backwards though this should be
speed times
uh the opposite of direction not that
the player's movement should be speed
times the opposite of the direction
because the direction is for the
background so instead of creating new
event handling we'll just do the
negative direction
all right so now i'm going to go back
and what you'll see what i was trying to
show is the wall will actually keep
moving towards you because the wall is
looking at your total distance traveled
and your total distance traveled is
still going up so what we're going to do
it's actually a really easy fix we're
just going to create a second variable
that's going to be almost exactly almost
exactly the same as total distance but
we're going to call this one player
distance
and we're going to have one of these
change based on
based on the
boundary limits variable and the other
one will update no matter what so what
we want to do is say if not boundary
so if not
boundary limits
then we want to update the total
distance because total distance controls
where the x and y variables are but then
we want the player distance no matter
what to update by speed and direction
and so what we want to show on screen is
player distance because we want to be
tracking and this will be a little bit
if you're confused right now i think as
soon as we get to the actual
demonstration of what's going on this
will make more sense
but i will try to explain it best i can
so we have these two things player
distance and total distance and they're
very close but they are slightly
different so
let me load in um we went pretty quick
there let me just load this in and see
if that fixes it
okay
so we're gonna hit a thousand there you
go and the wall stops moving at this
point but the player distance does not
which is good because we want that
distance traveled to count overall like
the player is still moving and should be
able to move now we're gonna go right
and
i had hoped the wall would disappear
okay so let's take a look at what we're
doing i think i know what we did
uh the so we're checking total distance
for boundary limits yeah and we want to
check player distance okay
so you need to think about this as two
separate things going on we use player
distance to determine when we need to
stop moving the boundaries and then we
use a different variable total distance
to control the position of the walls so
that's why
let's go ahead and run this now i'll be
able to show what we want
so right now player distance and total
distance are exactly the same and as
soon as we hit a thousand there we go um
the wall stops moving i can still move
the player and that's still updating our
total distance traveled and then once i
go past a thousand the wall stops which
is exactly what we want so let's go get
to it'll be like 1400 when we see the
right wall
and should be there now
um
where to go
okay well it made me a liar um i mean
you can see the background stopped at
right around this right spot which was
1400. oh we got a little green there
okay so i think what we want to do is
maybe just move this back a little
further so finish line plus total
distance minus six i thought that'd be
enough but we'll go we'll go minus eight
just because i
want to be able to see it when i get to
1400
okay there you go so we get to 1400 and
uh it stops and then we can move i mean
the thing is we haven't done anything to
handle like actually ending the level
like victory um where if you collide
with the finish line you win the game or
something um but that's a little out of
scope of this video it's already getting
up to like a 15 minute tutorial i hope
you found this concept pretty simple
pretty clear if you enjoyed the video
please leave a like on the video
subscribe to the channel for tons more
content in the future
just like this and i really appreciate
all the support if you have any
questions about what you saw here feel
free to leave me a comment asking about
it or if you want to see something in a
future video be sure to ask that in the
comments as well and i'll get back to
you as soon as i can as always thanks
for watching and good luck with your
code thanks bye