In Honor of HALO Infinite Releasing this week and celebrating 20 years of playing Halo this series of Python and Pygame Tutorials covers how to create your very own Halo themed space invaders style game!
We start the video by demoing the end result full functionality of the game and then dive into this video's specific content!
Episode 1 covers how to pull in outside images for your game to set up a full screen background, how to initialize pygame and how to draw lives and level text labels onto the screens using f strings to format strings with variables!
This game will be rolling out in a six-part tutorial series so be sure to check back over the next five days for the entire series! Leave a like sub or comment if you like what you see here and be sure to check out the channel for tons of other great content!
Thanks for watching, good luck with your code, and give the covenant back their bomb!
what's going on you guys welcome back to
the channel and i have a super unique
one for you today uh as a lot of you
probably know halo infinite uh early
released their multiplayer recently this
past week marking the 20th anniversary
of halo and if you're like me you
probably grew up playing these games you
know on this channel we do typically
python and other software tutorial
series we've done apps we've done
widgets and we've done just base python
programming as well as a few games and i
thought what would be super fun in honor
of the halo release as if we did a
little bit of a
halo kind of space invaders style game
i'm gonna start every video in the
series by kind of demoing the game
so that you guys see what the end goal
is and hopefully think that that's
something worth building to
so i'm gonna demo the game that we built
here a little bit and i am pretty proud
of it you can hear just kind of the epic
soundtrack in the background i've left
instructions for how to play and said
you know click anywhere to begin so
let's get into it we've got a little uh
some soundtrack playing in the
background i've got some sound effects
the enemies in each wave are
automatically spawned in
and they kind of randomly pick heights
to spawn in from and i've set them up
with a specific speed to come in at
and i've added some impact sound effects
that maybe aren't the greatest
i've also made it to where each wave
increases the number of enemies so you
can see we're on level two now and we
should have more enemies
i got these little uh 8-bit halo
icons from the internet
and in the series in the actual
tutorials i'll talk through kind of how
to grab images how to scale them all to
the size that you want
as well as there's a free tool on the
internet for removing the background
from images so
if you've dealt with kind of the super
annoying like you pull in an image you
want to use and you have that like white
rectangle around it no worries uh we'll
go over how to do that too
all right so there we go so that's the
game demo and whatever stage of the
project you're at and following along
with uh hope you're enjoying it be sure
to leave a like on the channel a
subscribe to the channel if you are
enjoying it and uh good luck with your
code
all right so let's get started with part
one of this halo space invaders pi game
tutorial um with python and really to
get started we're just going to be um
getting the board set up because you
want to kind of create the world in
which you're playing before you start
adding too much of the functionality and
so pygame is a module inside python
which means we need to start by
importing it
and i'm going to start the game by
importing a few other things that we're
going to need as well
typically if you're creating a program
from scratch and you haven't done it
before
then you'll kind of just import things
as you realize you need them while
you're writing your program i've built
this before i know we're going to need
the time the random and the pie game
modules for it um so if it seems like
you know i i'm kind of doing this with
four knowledge of how it's gonna work i
am because that's you know the better
way to do a tutorial series than just
make it up as we go along so there is
going to be text in our game as well so
we're going to set
fonts up at the beginning here and it's
kind of weird with pie game but you do
this pie game dot font dot in it and
you're just kind of initializing pie
game and saying i'm going to call fonts
at some point in this game
and then we're going to create some
window variables so that if we decide we
want to
if we decide we want to change up our
screen size later we can and we're going
to do width and we're going to do height
and it's a pretty normal python um
convention that if you're using
constants
then uh your you want to make them all
caps because in python it's very normal
to have all lowercase for all your
variables and so if you see something in
all caps you can be pretty sure it's not
meant to change
and so now we're going to actually
create the window so i picked a width of
a thousand and a height of 700 you can
play around with that as much as you
want for your window if you want to make
it full screen go for it
i found 1000 by 700 as a fun little like
pop-up window
size game and it works pretty well for
what we're doing here
and so to create an actual screen you do
pygame.display.set mode and then in here
you put in a tuple or tuple whatever you
prefer
and you give it the width and the height
so if you're
comfortable with it if you know where
they're coming from you can put these
integer values right in here and that's
totally fine completely valid uh it this
is just a little bit easier for me if i
decide i want to backtrack and and
change the screen later it's nice having
them in a variable
and then the last thing we'll do up here
is we'll actually set the captions so i
like to get a little funny with these if
they're not like if you're not releasing
it on the app store or anything like
it's not for commercial use or a
customer or something uh you just call
it whatever you want so i said halo
please don't sue me
because uh
this is obviously i'm taking a lot of
images that aren't
um aren't mine and um you know sounds
and stuff but that's the fun part about
building personal games like you can rip
off copyright material as long as you're
okay with the fact you're not gonna be
monetizing it or anything um which if
it's just for fun then you shouldn't be
worried about that anyways
and so uh the way the the way this is
gonna work is we're gonna have this like
um
running variable that basically says
when the game is or is not running and
while it is running
um right that's that's where we're gonna
stuff kind of all of the operational
game code and we use that running
variable as a good way to like reset the
game or stop the game when you die
so we're gonna do a few things while
running and one of the first ones is
we're going to do uh timer.tick
at a specific frame rate and so if this
is your very first intro to pie game you
might want to consider going back and
checking out like the my tutorial series
on on the very basics of pi game that
that's on this channel i've created a
playlist for that but um the the reason
you want to do set up a timer to like uh
be a frame rate to timer.tick.framerate
is so that the game plays the same speed
on any machine you're running because a
really fast really new age computer
might be able to process it super fast
and a slower older computer might have
trouble running it as fast but if you
use a frame rate
you're saying like well okay run in a
fixed 60 frames per second no matter
what or whatever frame rate you want
we are gonna set up a frame rate of 60
and we're going to define a timer
variable that's why we're getting the
red line before and that's going to be
pygame.time.clock
and so hopefully that makes sense if you
have any specific questions about why we
did that just let me know about in the
comments below i'll flesh it out for you
in a little more detail
but so that's gonna be
timer.tick.framerate
and then one of the first things you
want to do is create some event handling
so you say for event
in pygame dot event dot get
and this is really important because
right now obviously we have an infinite
loop and that's gonna cause your
computer to kind of fuzz out and crash
pretty quick
and so all we do this is really the
first thing you're going to set up
anytime you build a game in pi game
we're going to say if event.type equals
pygame.quit
and if that's the case we're just going
to set running
equal to false okay
and this is really all you have to do to
get a window to pop up so let's go ahead
and run that and i believe
that we'll just get kind of a blank
nothing yep at our size but you see our
caption you see our window size has been
set so now you can play around with
screen dimensions if you want to change
it around but i'm going to start
fleshing it out a little bit i'm going
to create a background
which will be
will actually come up i put a section in
here called load images so let's start
with the background
that's going to be the first thing that
loads in and everything else gets drawn
on top of it and we get introduced here
into pulling in
images so that the
minimum for grabbing a image from
somewhere pygame.image.load
and then the file name when i make a
game that has a lot of audio a lot of
images a lot of any sort of assets i
always create that folder inside of my
like project
window my project
um
what a second folder directory in python
because you can you can come up to your
game and you can say new directory and
you can drop copies of all those files
in there if you don't do that just
understand this is where you have to put
down the whole file path
so it's a lot easier if you do
pygame.image.load and they're all in
this directory because then you can just
do
images backslash and then the name of
your background so i pulled in a lot of
crap for this game but i think halo bg
is what we're going with yeah and i just
pulled this from google images guys
since this is not like meant to be
released for commercial use so grab a
background you like from from google
images make it your own
make sure you like it and then we're
going to pull it in and you actually
don't need to worry if it's exactly the
right dimensions yet i'll show you why
in a second but so type in
type in the name of your file right
there and do pygame.image.load
and then the reason i said you don't
have to worry about the dimensions too
much is we are going to scale it to make
sure it fills our image perfectly
and so that's a separate function that's
pygame dot transform
dot scale
and then everything inside the brackets
the pi game.image.load you're saying
what image we're scaling and then do a
comma and then you give it another tuple
saying what size to scale it to and
since this is the background we want to
fill the image so we want width and then
we want height
um
okay so that should do it
let's see
we'll see when we load it in but then to
actually get it on the screen we want to
add a function to all of this and
actually i'm going to put everything in
here that's supposed to kind of happen
every
iteration i'm going to create a function
called main loop and i'm going to put
everything that we put here inside main
loop and that might
it might not make sense to you if you
haven't built a few games before why we
do it that way it's also not the only
valid way to write code but what we're
saying now is um we're actually going to
call a function that's going to do all
of this
that that we kind of set it up to be
just static initially now it's only
going to be called while main loop is
getting called that's sort of the way
you wanna do a game where you're gonna
have like an exit menu or a start menu
or any kind of like second menu you want
everything in your main game to sort of
be operating under a function so that's
why we're going to set up this main loop
and inside the main loop we're going to
create a new function that we're going
to call draw window
we'll call update window actually
because it's going to draw initially but
we want to keep updating as things are
happening
and so inside of update window the first
thing we want to do is window so we're
defining what screen we want it and then
blit
which is like pi games block transfer it
essentially means actually draw it onto
the screen
and then we give it the object to blit
and you gotta be able spell but we're
gonna put background on the screen
and then we define a tuple for
coordinates and the pi game uh
coordinate grid starts in the top left
with zero zero and as you go left to
right on the screen it goes from zero up
to whatever your width is and the top of
the screen is zero and the bottom of
your screen is whatever your height is
so that's why zero zero is where we want
the top left of our image to be drawn on
and um the other thing that we have to
do is we actually have to call that
update window function
and we'll call it right beneath the
clock so now what we're saying is
every tick of the clock which is 60
times per second
update the window with whatever we're
drawing onto it so that should be the
minimum to get the background on let's
run it just to make sure
and we don't have a background yet hang
on let me see we might not be
just updating the display that's right
so we also need in our update window
pygame dot display dot update
and that's what's actually telling your
your window to check and see if things
have been drawn onto it so there we go
there's the background and let's uh
carry on with loading in some of the
other images
so uh the next thing i want to do is
actually a little different it's going
to be the text
and
that's because we're setting up kind of
the static background and in the next
video we're going to be putting the
player and some of the enemies on screen
so let's get the
like lives in the top left and the uh
level in the top right let's do that
next and to do that we're gonna create
two new variables that we're gonna call
just level and we're gonna start at one
and
lives and we're gonna start that at four
and
then we're gonna define
a font so
main text we already did that pygame dot
init for the font but now we're actually
defining one so that when we draw text
onto the screen we've already defined
like
font size and what font to use
so there are a few built into pi game
you can also go grab like a dot ttf
which is a font
file and you could have another asset in
here called um you know fonts or just
additional assets or whatever and you
can reference it just like we did a file
load
i'm not that fancy
so i'm just going to use one of the
built-in system fonts but even if you
had a different font you would just put
a directory here like
if you had a fonts folder you would do
fonts
you know slash whatever whatever
whatever and point it to it right there
but i'm going to use free sans bold
because that's a built-in font and we're
going to say font size 40 because it's a
big game big screen
and then true and then false for bold
and then italicized you actually don't
need the true false here if you just
want to leave it at 40 that'll get text
on there i like it bold i don't need it
italicized right now but you can play
around the style of it if you want
and then what we're going to do is we're
going to create the actual text so
live's text now
is going to be main text dot render
and then we're going to use f strings
which is uh essentially a formatted
string version this does only work i
believe in python like 3.0 and on so if
you are still coding in
old school python then you need to use
like a string concatenator in version 2
of python but if you're using version 2
of python you probably know how to do
that anyways
and so essentially an f-string for those
of you who maybe haven't seen this
before allows you to combine um
formatted like variables and strings
without having to do like uh
concatenation of like plus str
so we're putting an integer variable
inside of a string and because we have f
there it lets it it takes care of
formatting for us
and then true
and then you give it a color we're going
to go with green for a lot of stuff in
this game because halo's kind of got
that iconic green to it
um so we're gonna do two we're gonna do
live lives text and then we're also
gonna do
level text
and we're gonna put them in opposite top
right screens and you probably saw i put
a space between every
character it just kind of looks cool
when it loads in i've i've made this
game before and i like the way it looks
but you don't need those if you don't
want them for sure
and then similar to before we're going
to do window dot blit and then we're
going to say okay the lives text so that
variable we just created we're drawing
that onto the screen and then we give it
a position
and this is going to be the top left
corner so we'll put in 10 10 and then we
do the same thing for the level text
excuse me
and this one we're going to put in the
top right so we'll scooch it over a lot
and we'll do 810.
and that's probably all we need to get
those guys drawn onto the screen
alrighty so lives and level our caption
our background is loaded in and we're
ready to start kind of building things
into this game so you've successfully
created the framework whatever
background you grabbed whatever color
you went with for your text
you know hopefully your game is in a
position where you're excited you're
ready to start drawing like the
functional piece of the game onto it
if you found this useful be sure to
check out the next couple in the series
as we're really going to be flushing
this game out and hopefully if you have
been enjoying the series leave a like on
the
video subscribe to the channel it helps
me out a ton and just leave me a comment
below with any questions you had or what
you'd like to see next and good luck
with your projects and thanks for
watching thanks bye