In an effort to raise awareness in additional to full code-along tutorials on my big pygame projects I will be rolling out project showcase videos as well!
Jetpack joyride is a classic phone game and a lot of fun to play, and just as fun to build! In this video we review the highlights of making the project and playing the game!
Don't forget to leave a like on the video and subscribe to the channel for more content!
Consider becoming a super supporter of the channel at my Patreon: https://www.patreon.com/LeMasterTech And thank you to Patreon supporters Dale Topley and Philip Nunez!
Thanks for watching and see you next time! Drop a comment letting me know what we should build next!
what's going on you guys welcome back to
the channel and today's video is going
to be a project Showcase of Jetpack
Joyride as well as a quick code showcase
it's going to be a little different than
our long form code tutorials because I'm
not going to go line by line through the
code I'm more going to explain that if
you're the type who grabs the GitHub
link and wants to sort of know what's
going on with the code I'll explain my
thought process behind how this was made
and you don't have to watch a multi-hour
code long if you don't want to
oh
so I have wanted to make Jetpack Joyride
for a little while
um it was essentially the runner-up
project for our 5000 sub special I did a
poll on the channel and Donkey Kong Won
and in hindsight like I'm pretty excited
that we got to build out Donkey Kong
that was an insane amount of work but it
came out really really cool but Jetpack
Joyride had a lot of votes as well and I
finally got around to making it
um and something that the uh actually
the subredditic uh our PI game has said
a few times when I've shared some of the
bigger projects is that you know they
think the projects are really cool which
I appreciate
um but that sometimes like they don't
feel like they need a four and a half
five hour video or even a two hour video
to know what's going on because most of
them want to grab the code from the
GitHub dig into it themselves play the
game a little bit and then sort of
enhance it from there and so what I
thought would be a good idea is to sort
of when I do the long form code
tutorials which we rolled out earlier in
the week is to also to include a video
where we just sort of play it talk about
the functionality of it and then
hopefully there's a somewhat lower like
barrier to entry for people to find the
content and find the channel because
ultimately the goal of my channel is to
get people excited about stem Concepts
and engineering and to feel confident
programming and building cool stuff so
if rolling out a two-hour video gets all
the people who are really gung-ho about
like programming line by line and
getting it then maybe rolling out a 10
or 15 minute video where we showcase it
could get a different group of people in
the door so uh without any further Ado I
guess let's talk about the code and I'll
just say if this is your first time on
the channel uh maybe consider
subscribing to the channel uh we do a
ton of cool python projects as well as
Arduino and other stuff
um so just be sure to stay subscribed
for all the new content and to talk
about the code really quickly this whole
project only ended up being around 270
lines of code and I say only despite the
fact that that's quite a bit of work I
say only because uh some of our bigger
projects like Pac-Man I believe was over
a thousand lines of code and I think
Donkey Kong was close to a thousand
lines of code uh and so for a full game
that has a lot of functionality in it I
was actually kind of impressed that it
stayed down to 270 lines of code
um but so basically I'm not going to
dive too much into the specifics of like
setting up our pie game a lot of this is
boilerplate and if you grab the GitHub
Link in the comments below you don't
really need to know what every single
thing in here is doing if you're an
absolute beginner to pie game I suggest
you check out the like intro to pie game
playlist on my channel I'll probably
leave a link to it somewhere above but
that I cover the basics of setting up a
pygame window and all of these things
mostly
um the first 50 lines 40 lines of this
code are initializing variables that we
need later on when we build it
uh and then this little block of five
here in the beginning is interesting and
worth talking about this is actually
everything you need for when you boot
this game up in the very beginning to
read the high score and the lifetime
distance run that you've played so this
uh for this I drop a text file called
player underscore info dot txt into the
same folder as my main dot Pi file
and the only thing I put on there is two
lines
um of of numbers so the top one is your
individual
run high score and then the bottom one
is the lifetime distance that you've run
while you play Jetpack Joyride so that's
a really kind of cool thing to keep
track of and this functionality just
extended out would be how you add
progressively more essentially player
history and player tracking to your
programs so that's just a cool little
bit of code it's really as simple as
read the lines from this text file and
then assuming you know what order they
come in then you can just store them in
the variables and since we later in this
same project we Define what gets stored
in that text file we can be pretty
confident that we know what we're
reading out of it there are ways that
you could add error handling and make
this a little more robust but we
literally we write into that file later
on two rows of integers with the high
score and the lifetime distance ran so
we can be pretty confident about the
content of it and then just make sure
you close it out to essentially release
the file and then after that we set up
the main game Loop which is just
basically saying run equals true wild
run I want to timer.tick which controls
the speed of the game and then you start
drawing it you start
um defining the different like
functionality that needs to happen and
you can do this in sort of an abstract
way and say like okay if I need a new
laser obstacle generate the laser um
every iteration of the loop I need to
draw the screen onto the screen if the
pause functionality has been selected I
need to draw a pause onto the screen if
there's no rocket active and it's not
actively paused then I need to start
counting towards triggering a new rocket
so a lot of this just sort of it you
code it because if you're saying it out
loud that's the next thing you have to
do so if you watch the full code along
you'll see I'm basically like I think
the next thing we need to do is Draw
Something on the screen and then we make
the draw screen function Audi and
they'll be like okay it's time to draw
the player and we'll draw the player on
the screen and I'll say okay we need to
check if the player is colliding with
the lasers the Rockets or the top and
the bottom so we make a check colliding
function
and so that's the beauty of
object-oriented programming is you can
essentially say I need to do X and then
you make a function to do X and then
your code that does that function is in
there and it's a very logical way of
making a program I'm not perfect at it
I'm self-taught and a lot of the time
some of my viewers will throw in hey
this would be more pythonic or this
would be a little more object oriented
and you know the beauty of python is
that for beginners once you get your
code working that's kind of the goal
right and then once you're sort of in
that intermediate range it becomes a lot
of clean up enhance and make better
um but anyways that's a little bit of a
tangent I want to talk about the game
handling because in pi game you set up
this four event in pygame.event.get
essentially this nested series of for
loops with if else if if if if if but
basically the first one you always need
to set up
is if the event.type is pygame.quit set
run equal to false even if you don't do
it exactly this way you need to have an
event from the first time you boot this
function up that will set run equal to
false because otherwise you will have an
infinite while loop this while run you
have to have a way of exiting that while
loop so that is why this is the first
thing that we always do and you can
actually see the modify player info is
when we save information right before
quitting out of the function so that's
how we capture like if you quit if you
press the x button but you've run 10 000
meters we still want to count that
towards your lifetime running so that's
something that we do in the modified
player info function we'll talk about in
a little bit later
but then the actual game mechanics of
event handling for Jetpack Joyride are
very simple it's basically the space
button being pressed down makes me fly
up makes me activate that booster and
then when I release the space button it
sets booster equal to false and then
beyond that everything else here has to
do with the pause button so like
pressing the Escape key is what I picked
to bring up the pause menu and release
the pause menu
and then when I'm on the pause menu
there's a button on there that lets me
restart my game and quit my game and
that is it for event handling basically
the buttons on the pause menu triggering
the pause menu and then the space bar to
be able to fly up and down a lot of the
times these famous iOS apps had a very
basic game mechanic and so there's
nothing particularly complicated going
on in Jetpack Joyride and then down
below this is where I will admit my code
maybe looks a little bit like spaghetti
code because a lot of this could have
probably been extrapolated abstracted
into a function but I felt like it was
pretty straightforward and it had to get
called every function and it's pretty
easy to read but basically if we're not
paused we want to add the game speed
which gets progressively harder the
longer we play
to our total distance and if we're using
the booster we want to move upwards
change our y velocity towards the top of
the screen
which actually is somewhat
counterintuitive it's subtracting
gravity out
um but otherwise we want to fall which
is adding gravity because the higher
numbers in pi game are actually the
bottom of the screen and zero is the top
of the screen
um we want to fall and then if we're
colliding with the floor or the ceiling
um and we're we're supposedly falling or
boosting then we need to set y velocity
equal to zero and not let the player
change anymore and then just modify the
player's y coordinate by that velocity
parameter so that happens as long as
it's not paused and then down here this
was a little bit of like guess and check
what Max Speed feels good how often do I
want to spawn a laser uh what how often
do I want to change the background so
really these things if you were looking
for an opportunity to sort of tweak the
game tweak the parameters of the game
and see how that changes things you
could spawn lasers less frequently I
essentially spawn a new laser as soon as
the previous one clears which I thought
was actually pretty good and then I let
the game speed get to a Max of 11.
you're pretty much flying at 11. so I
felt like that was pretty good and if
you manage to get to 50 000 on this game
that I built like you're you're pretty
good um
and then every 500 pixels I just
randomly generate a new background so or
a 500 meters run I generate a new
background so there's just a fun little
game mechanic and then I put the various
times that we could restart so we want
to restart if you hit an obstacle um we
want to restart if you click the restart
button from pause and then we want to
restart when the game boots up so I put
all of that just into a spot that says
if restart command do this series of
things and then the last thing I just do
is I check if the current distance is
greater than the previously stored high
score and I override the high score at
the distance so this is while you're
actively running if you are beating your
previous high score it'll immediately
show on the screen that you have the
high score and then this
pygame.display.flip just goes on the
bottom of your Loop but still inside the
wild run and then outside of the wild
run you do this pygame dot quit so that
when you press the x button the game is
over
um it's pretty simple there's not a lot
of crazy stuff in here I think maybe
let's dive into the sub functions very
quickly um the first one that I set up
was draw screen and this really it it
looks like there's a bunch of things
going on but they're all simple so we're
filling this overall screen with black
then I draw a semi-transparent uh that's
why I draw it onto the surface because
surfaces except an alpha parameter I've
talked about that in the previous two
videos on the channel pause menu and
um uh and how to draw transparent images
but we use that background color with
the transparency setting and then BLT
that onto the screen we Define the top
and bottom rectangles that we check for
collision with the player to see if
we're able to keep flying up or keep
falling and then these for Iron Range
Line list all of this code is just
drawing those vertical lines uh
essentially every quarter of the window
to give the illusion of the overall
board moving to the left so basically
they will speed up with game speed and
all they're really doing
is sliding to the left so that we feel
even though our players x coordinate is
not changing we feel like we're
continuously moving forward
um so that's what we're doing at the
line list and then we're checking if
they go off screen just reset them over
to the right we draw the laser in this
function uh so essentially this is the
main line of the laser and then these
are just those end caps those circles on
either side of it and then we display
the distance and the high score that's
all that's going on in this draw screen
function so even though it's Twain lines
of code it's pretty basic functionality
drought player is actually 30 lines of
code it's even simpler it is defining a
rectangle for the Player's hitbox and
then there's some checking to do what we
should do because we animate it with pi
game rather than animating it with
different images that we just split onto
the screen so that's why it looks like
there's a ton going on here but to break
it down really simply
regardless of what state the game is in
we draw the white rectangular jet pack
the orange body the orange head and the
black eye every time we always pass the
player's hitbox back and then the only
things we're checking for in here are if
the player is not on the ground and the
game is not paused
um then or or the game is paused then we
just draw the feet vertically and then
if we're actively boosting using the
space bar we draw a flame which is red
orange and yellow and so if I can just
show that really quickly when we're in
the air
we just draw these two uh orange and
yellow stumps for legs straight up and
down and then when we're actively flying
we we draw these three colored ellipses
on the background for a flame effect
and then the rest of the code is just
checking to see if we're on the ground
and then every actually it's like it's
like more than once per second because I
used a counter variable of 40.
but so basically every sixth of a second
we change frames and all that's doing
this is saying we have a yellow leg and
an orange leg
and it's just changing their legs in
their four positions so the draw player
function looks like it's 30 lines of
code and you could boot it up and be
like oh my gosh what is going on here
it's really pretty simple when you break
it down check colliding is actually the
simplest so we have a collision list
just just with two parameters in it to
check if we're colliding with the top
and colliding with the bottom and then
to check if we're colliding with the
laser or a rocket
um we just we just set restart to true
we don't have to do anything complicated
like we need to know continuously if
we're colliding with the top and the
bottom because that's how you know
whether you're able to keep flying or
keep falling but if you hit a laser you
hit a rocket just reboot the game so the
check colliding function is very simple
generate laser I'm kind of proud of this
basically we randomly decide if the next
laser is going to be a horizontal or
vertical and then we also put a little
element of Randomness to see how far off
the screen to the right we should draw
it so it's anywhere from just barely off
screen to the right to quite a bit off
screen to the right and then I did put a
little match case in here just to show
you can do this instead of an if else if
by saying I want to match on the value
of laser type if it was a zero that
means draw a horizontal if it was a one
draw vertical and then we Define a new
laser just by saying it's X position and
its y position and that's all that we're
doing and then we return the new laser
function and that's it for Generate
laser we then draw that on the draw
screen function we already talked about
draw a rocket
is a little bit more complicated because
the rocket has two modes the rocket has
before it has spawned and then it has
wallets actually on the screen and so
I'm kind of proud of this as well we
keep track of the mode inside the main
game Loop we basically say when it's
time to spawn the rocket give about a
second or two of warning with this dark
red rectangle with an exclamation mark
and so to boot it up and refresh
everyone's memory the rocket in mode 0
is when we get this this little red
rectangle with the exclamation mark and
then it's sort of able to move into a
certain level of tracking the player's y
coordinate and that's what we do with
these four lines we're just saying if my
y-coordinate is above or below the
player essentially that I'm targeting
then I'm going to move up or down but
then once it's time for the rocket to
launch we don't change its y-coordinate
anymore we change its x coordinate and
we move it by a speed of 10 and actually
I'm looking at this now if you've
downloaded uh the GitHub yeah yeah 10
plus the game speed so it flies to the
left across the screen pretty
aggressively
um and basically we draw it in two
different ways two based on the mode so
this is the warning the dark red with an
exclamation mark on it and then this is
the rocket it's actually the red
rectangle with an orange flame behind it
and we do all of that inside the draw
Rocket function
um draw pause is not particularly
difficult uh once you understand kind of
the drawing semi-transparent so the
first thing we do is we draw that dark
gray rectangle with transparency on over
top of the entire screen and then we
just put some text and some rectangle on
there that says hey you paused the game
but you can resume with the escape
button
then I draw a restart button and a quit
button onto the screen as well as the
lifetime distance that you've run just
because that it makes a little more
sense in my mind to show that on the
menu than to show it constantly on the
screen but reasonable people could
disagree there and then I returned the
restart button in the quit button
because we need to do that in our event
handling so that's kind of all that's
going on draw pause and just to remind
you guys what the pause menu looks like
uh you're in the air it's paused you
have this semi-transparent gray mask
over the whole screen it tells you that
you can resume with Escape or you can
click restart or quit and it shows you
your lifetime distance and then the last
sub function in this whole thing is
modify player info which is where we
check if we have a new high score and we
add our lifetime uh distance we add the
last run to it and then we save it using
uh write so file equals open playerinfo
Dot text right which is the opposite of
read that we did to pull that
information in
and then we write the new high score to
the function with the new line character
and then we write the lifetime distance
run and we close the file and we call
modify player info every time we restart
so that when it's a new run we go ahead
and add the previous runs distance to
the player.info and then we also do it
when you quit so if you press the quit
button or if you press the red X we call
modify player info first so that is all
that is going on inside of this code
I think it was a ton of fun to build and
I'm really thankful so many people vote
on and give their thoughts on kind of
the direction of the channel and what we
should do next and I really appreciate
everybody watching this I hope if you
guys are interested enough that you'll
follow along with the code along
tutorial and kind of understand how the
overall project uh goes but be sure to
let me know in the comments below how I
could have made this better what you'd
like to see next on the channel if you'd
like me to do more of these kind of
walkthrough code alongs in a in addition
to the long forms let me know that as
well because I may Circle back and do
one of these types of things for like
Pac-Man and Donkey Kong to potentially
make a shorter form video to expose a
wider audience to that content so again
thank you everyone for watching a
massive thank you to my patreon
supporters uh and as always good luck
with your code thanks for watching see
you next time bye