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 3 shows how to spawn waves of randomly generated enemies off the screen to the right and have them come across the screen during attacks. This includes using the random module with the rand range operation as well as the random choice from our list of possible enemy units! We load in the images of two different 8-bit Elites and a pixelated Grunt for our enemies and assign them to different colored projectiles as well.
Episode 2 covers how to pull in a player graphic, which in this case will be master chief, and then setting up four directional controls for the player! Additionally we set up bounds so the player cannot move their character off the screen in any direction! This is essentially creating edge collision detection and we do a deep dive into some of the themes of pygame as well as how to create and call classes and subclasses!
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 for part three in our halo
space invaders uh python pygame tutorial
series here we're going to be taking a
look at adding the enemies to the screen
so we've already created the player who
can be controlled with the wasd
four arrows and right now nothing
happens because the enemies don't spawn
in they don't come across the screen or
anything so what we need to do next is
we actually need to take this class of
character and just like we created a
specific class for the player before
we're going to be creating a class for
the enemy which again
since we created the character to be
fairly generic as a parent class we can
do a lot of the same inheriting that we
did as the player you do need to spell
it right
it's always kind of my achilles heel
but um what we can do just like when we
did the player class i'm actually going
to copy this we can pull in the super
for the init function
and so just like before we're doing that
but what's different now is we'll be
using a different
image and a different shot so i create i
i got the images for three different
8-bit halo enemies i got an elite with a
sword an elite with a gun and i got a
little grunt um so i like all three of
these images i thought it'd be fun to
have those as the enemies you can
certainly pull in more you can make your
own however you want to do that
but let's go ahead and
create those so just like before we're
gonna have to give them um
we're gonna have to load their images in
and we're gonna have to scale them down
so we need i called them
i'm actually just going to pull them in
here
but i called them grunt elite with a
sword and elite with a gun
and then i gave them these scalings i
wanted the grunt to be a little bit
smaller than the elites but these are
the images for the three of them and
then i want to keep it fairly simple i'm
going to give the three different uh
characters different colored lasers but
i just they're they're little
rectangular uh projectiles and so
um that's what pixel laser blue pixel
laser green pixel laser red are so i
called those red laser blue laser and
green laser and i'm gonna copy those in
here too as well you can see this first
one i put the rotate function on we are
going to need to do that to all of the
images all of the lasers because they
all came in as vertical rectangles
and so if you use this rotate it's just
like the scale function it just
manipulates your image and it's going to
turn them 90 and that's what we want to
do with the bullet as well except whoops
except with the bullet we actually need
to transform that one 270 degrees it's
not just a rectangle it has a point
and a butt you know it has a tip and a
rear end and so we need to turn it 270
or you can turn it negative 90 in pi
game those are the same thing hopefully
you're familiar with the fact a circle
is 360 degrees and that's what we're
giving it here as a value of rotation
in degrees
okay so we pulled in the three different
laser colors we pulled in the three
different images now in our enemy class
what we're going to do is we're actually
going to create a dictionary here of the
different units what we're going to call
them and then we're going to point them
to a few things so the first one we'll
say is going to be called grunt and for
a grunt we're going to give it two
variables in a tuple over here we're
going to say use the image grunt and
then since our grunt is red we're going
to give him the red laser
all right and we're going to copy this
uh kind of entry
and we are going to separate them by
commas and a space and then the second
one will be
sword
yeah that'll work and then we called
that guy
elite underscore sword
and we gave him the blue laser
and
there we go and let's do that one more
time and we called this guy the gunner
because it's an elite with a gun you can
name them whatever you want so if you
don't like my naming convention
move on with your day
so elite gun and then we gave him
finally the green laser so what we did
here is we just made a dictionary of the
three enemies that we're gonna allow the
game to choose from and i like the idea
of having a procedurally generated kind
of randomized waves of enemies
um get the formatting right
and so that's all that was as a
dictionary where now we can just call
grunt sword or gunner and that's going
to decide whether they should have
you know which image and then what color
projectile they're going to shoot you
can also use that if you want to to give
them different properties like you can
give them different speeds based on this
dictionary you could give them different
healths based on this dictionary so
really use that as a as a stepping off
point to make it your own but the reason
i think it's so useful
is uh now when we create an instance of
enemy we're going to have one more
variable that we'll call unit
right because it's deciding between the
three and we're going to call this
dictionary
units
so we'll say okay create a grunt create
a sword elite create a gunner whatever
and we pass that in as a variable and
then all we have to say for self.image
and self.shot
is going to be equal to whatever
its lookup in the units dictionary is
for that unit so again this requires a
little bit of knowledge of python's
dictionary and lookup style of functions
there are tutorials on the channel if
you need that and then the self.mask is
going to be just the same as it was for
the
player and then for the move the
movement for these guys is going to be
really simple because wherever they get
generated we just want them to move from
the right of the screen to the left of
the screen right so it's just self.x
minus equals speed and you don't need
self twice
okay
so essentially wherever they spawn in
we're also going to give them a speed
just like the player has a speed and
we're going to say just move at a
constant speed from the right of the
screen to the left of the screen
so that's cool we've modified the class
of character to include some special
functionality for the enemy but now we
actually need to generate our enemies
and so this is where
the uh the level and the lives are gonna
come into play a little bit because the
level is going to determine um how many
enemies and we're actually gonna step
this down to zero because we're gonna
use
seeing that there are no enemies left as
what increments the game so when the
game starts we're gonna have this uh
empty
empty list
for enemies
and uh
it's gonna work that's how we're gonna
increment the level and also spawn a new
wave of enemies is when the dictionary
is empty when the list is empty because
every time an enemy either gets shot or
hits the left edge of your screen that's
when we're gonna delete that enemy out
of the list and so starting with an
empty list on level zero is how we'll
kind of begin the game
and then let's give the enemy some speed
i'm gonna say three uh that's gonna be
fairly fast but for this stage of the
game it's actually more useful to just
get them moving across the screen until
we've put the like shooting and
collision
detection in because that's when we
might want to slow it down and actually
test that part of the game but for now
we're just trying to make sure they get
populated on screen uh appropriately
and then this is not wave speed this is
wave size but uh let's say that we want
every level of the game to spawn five
more enemies than the previous weight if
you can do whatever number you want
maybe five feels too easy for you you
could double it to ten
those are fun little ways to tweak it to
make this game your own
and so now we're gonna come down into
our
draw and um again we made the enemies a
list of enemies so what we're gonna say
is for enemy
in our list of enemies
again spelling is hard
um we're gonna do just like we did with
the player pie game.draw
or uh not by game enemy dot draw
and we're gonna put it on the window
okay so uh this is great except enemies
is an empty list right so where are we
actually creating them and that's what
we're gonna do inside of the running uh
logic so down here above our checking
for quit and checking for keys and under
update window we're going to say if
length of
enemies
is
equal to zero right so this is where
we're checking to see if every enemy who
was in your game has been deleted or
destroyed shot gone off screen whatever
you want to say
but that's when we're going to increment
the level
that's when we're going to
increase the wave size
and that's also when we're going to four
excuse me for i
in
range of
wave size
we're going to say enemy equals
and now we're going to make a class so
here we're we're using the or an in
instance of a class
um so here we're saying the enemy is
going to be equal to and this is the
first instance of like randomly
generated
uh enemies but we're saying in a random
range that is going to be they're
starting x coordinate so think about the
generation of these characters if
they're moving from the right to the
left we want them all to spawn off
screen
from just a little bit off screen to
quite a bit off screen and that's how
you get like a wave effect if you want
to do this um as a vertical space
invader style shooter i think horizontal
makes a little more sense for halo
but if you want to do a traditional
space and various type shooter just
change the variable that's
them spawning in from the x's that we're
going off screen in to the y's but then
also make sure to change their movement
from
a decrement in the y to uh
from x to the y
so
for uh enemy random range and then we
give it a range of two variables the
starting one will be width plus a
hundred and then the second one we'll
say width
plus one thousand so that gives them a
range of 900 pixels all off screen to
the right that they could spawn in on
but that's sweet because then they're
going to be crawling across the screen
to the left
and then we actually don't want that
second one there we're giving it that's
the x coordinate and then for the y
coordinate it's going to be random dot
rand range just like before only now
we'll go 100 so it can't be pressed
right up against the top of the screen
and then we'll say height minus 100 so
it can't be pressed right up against the
bottom of the screen so we're giving it
here an x coordinate
of offscreen to the right and then a y
coordinate of somewhere in our visual
part of the screen
but then we're also going to use
another piece of random which is called
choice and we're going to make it decide
what the unit is going to be so if you
remember when we were setting up the uh
enemy
subclass of character we also said
you're going to be getting a parameter
which will be a unit
inside here and so now we give it a list
of what it's choosing between
and it's grunt sword and gunner and so
make sure whatever you named your
dictionary entries whatever you made the
keys in your dictionary that's what you
give it right here
and you're telling it those are its
options as choice so we're going to
split this out into a few lines because
that's just getting a little hectic to
read
and you can see right here the three
things were
giving our every enemy is going to be
starting somewhere off screen to the
right somewhere from top to bottom on
the screen and then it's going to be one
of the three enemies we defined
and so we need the last thing to add
this to our list of enemies so
again
enemy is a single item enemies is where
we're keeping track of all of them and
so uh as we go through and use this for
loop to create all of them we need to
add it to the list
and so that should do it for creating
our enemies once the list is empty which
again happens at the very beginning of
the game
didn't mean to put break point in there
and then i believe what we need to do
the last thing to get these guys moving
across the screen will be say four enemy
in enemies
and for enemy in enemies
we want the enemy to move
at
enemy speed so we already defined enemy
speed and now i believe that's all we
need when we run this they should spawn
off screen to the right and move from
right to left they're probably going to
move pretty fast here
let's take a look so level and command
yeah you can see they're moving pretty
fast this would be a tricky game but
since we don't have the shooting or
collision detection in place yet i just
want everyone to see that
we did spawn a wave of enemies that were
randomly generated and if we stop it and
run it again they're gonna spawn in
different locations so let's see here
yeah and we got a lot more sword elites
that time and uh less grunts and gunner
elites and that's okay that's the point
of like random generation to your game
you can set this up as a for loop to
create like a row of grunts a row of
sword elites a row of gunner elites and
and look a lot more like a a designed
version of the game not so randomly
generated your call that's a fun way to
customize it
but that's going to do it for this part
of the video in the next part of the
video we'll set up
i think the shooting mechanism will
start pretty soon with the deleting of
enemies if they either get shot if they
collide with the player or if they go
off screen to the left which will lead
to levels being incremented the wave
size increasing and things like that so
we're getting pretty far into this game
hopefully you're enjoying it if you are
as always i appreciate a like on the
channel
a like on the video subscribe to the
channel and let me know with anything i
missed anything you did differently or
anything you want to see in the future
in the comments below and as always good
luck with your code and thanks for
watching thanks bye