24 Video Views·Apr 22, 2025
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 4 shows how to handle the shooting action for the player, as well as setting up collision detection between the enemies and the player's bullets and then we create randomly generated enemy firing to attack the player, followed by collision detection set up to check if the player was hit, at which point we decrement his health by 10%, and if an enemy unit goes off screen left we lose a life!
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!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
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 good luck with your code
all right part four in our halo space
invaders pi pi game python tutorial
series and uh this episode is coming in
the the enemies are already spawning in
the players already built with four
directional control
and at the beginning of a level the
enemies spawn in and they kind of rush
at you but you can't shoot at them yet
nothing happens when they go off screen
to the left which means the level never
increments you don't lose any lives you
don't lose any health
nothing's happening and to kind of get
this game to the next step we have to
introduce projectiles both the players
and the enemies bullets so we'll say uh
we're going to create a new class called
projectile
and if you think about what we have to
give the bullet what's going to be
passed in in this init function it's
going to be an x and y location just
like with the player and there's going
to be an image passed in just like with
the the characters with either a player
an enemy um
okay and so we're going to start with
that for our init and just like before
we're going to use self.x equals x self
dot y
equals y and again what we're doing here
is we're
creating internal variables that we can
use that reference those things that are
getting passed in in the beginning
and since we are going to be checking
collision just like with the players
we're going to want to set up a mask so
we'll say self dot mask equals pie game
dot
mask
underscore from
mask
dot
from underscore surface it definitely
would have been faster to copy this at
this point but from surface
and then uh the surface that we're
checking against is self.image so it's
just like the character and the enemy
that we created before
and then
that's going to be it for the init and
then the draw function obviously we need
to put the
thing on the screen so just like the
character again we're going to use a
draw function that's just going to be
window dot blit and then we're going to
be putting the image
on
at our x and y coordinates so that
should feel really familiar if you've
been following along with the series
we've already got um
you know that exact same draw function
inside the character class but because
the projectiles are going to behave
quite a bit differently it doesn't make
sense to
use the
uh character class as a parent class for
these guys in the same way it did for
player and enemy because like so for
movement um on these right it's going to
be
all we have to pass in is speed because
wherever a projectile gets created
regardless if the player moves up or
down or the enemy keeps moving to the
left all we're going to do is take where
the
character was when it shot and we're
going to move the projectile
horizontally based on that
and so we're just going to do self.x
plus equal speed and the way we'll
handle the fact that the player bullets
move to the right and the enemy bullets
are going to move to the left is we'll
just use a negative speed for the enemy
bullets and a positive speed for the
player bullets and that way we have two
directional control all we have to do is
pass the correct um whether it's
positive or negative we have to just
pass that in
and then we're going to check for the
bullets if they're off screen or not
because that's when we're going to get
rid of them and so we'll just set this
up to be a boolean so we're going to set
this up to check true false whether or
not a projectile has gone off the screen
and all we have to do there is we have
to pass in the width of the screen
um okay and so we could call it global
and just check that variable
but it's a little easier to pass it in
this way and we're actually just going
to return true false true if it is off
screen and so we're returning
not and then we're checking to see if
the self.x right so the lower left point
of the image is between zero
and if it's less than or equal to
the width of the screen
and so essentially if it is between
these values right zero and the width of
the screen that means it's on screen but
if it's not so
this is super useful even if you have
projectiles that spawned off screen to
the right you don't want them to be able
to shoot yet so if it creates a
projectile while it's off screen it's
going to immediately get deleted because
we're going to see that's off screen so
that's the check off screen function and
then the last thing that we're going to
create
inside of our projectile class is going
to be defining
what a collision is so it's going to
just need to know what its target is
right
and this is going to be useful because
for a player's bullet
the targets are all the enemies but for
an enemy's bullet the target is the
player so it's going to be useful
to know what the target is and
to do that we're going to create a
function outside of any of our classes
that's going to be called collide
because we need to check if the players
collide with each other we need to check
if the projectiles collide with the
player if they collide with the enemy
if they go off screen so we're gonna
just return the um
result of collide between itself and the
target and so let's actually create that
collide function that we just talked
about
essentially it's just going to be
checking for collision between two
objects object one
and object two
um all right and then
inside we need to check to use the mask
function properly we need to know how
far apart their top left corners are
right so if you think about the
coordinate system in pi game how an
image starts getting drawn from the top
left we need to know the gap between the
x's and the gap between the y's and then
check if the masks collide
so maybe that'll make a little more
sense once we get typed down but
essentially the gap of the x is going to
be equal to
object
minus
object one dot x okay
so think of like a
um
i think it's the pythagorean theorem but
we're actually checking the linear
distance between these two points um
but we need it for the mask overlap
function in pi game so once we get
through it it's going to make a little
more sense
okay so there's our gap in the x
direction gap in the y direction is
going to be just the same
except it's going to be dot y instead of
dot x
object to
object one dot y
okay and here's why we actually needed
it because now we're returning a true
false to see if there's been a collision
and what we'll be saying is object one
dot mask and that should work just fine
because everything that we've created
that we're checking for collision for
has an attribute that is dot mask and
now we're checking
dot overlap so this is the pi game built
in function where it needs to know what
the second mask that it's checking
against is and that's gonna be
object2.mask
and then it's asking for the uh
difference in their coordinates so
here's why we need that gap x and that
gap
y okay
and then uh let's go ahead there and
there
and now we're gonna say is not equal to
none so um what's important here is
we're actually just checking to see if
there is an overlap we don't really need
to know what it is
and that's why we do not equal to none
because this is actually going to return
an x and y coordinate of where the
overlap is that doesn't really help us
for this game so essentially
if the point of overlap is anything if
it's not non-zero so if if you have the
very top of it colliding or the very
bottom of it colliding we don't care so
we're just checking a true false to see
if they collide
and so we're just checking to see if
it's not equal to none and if it's not
equal to none then we'll get true back
and so this is useful this is actually
something that we're going to use inside
of our player and enemy class now
so let's go ahead down into our
character class where we had um just the
init and the draw defined before
and we're going to start um
we're going to start adding some stuff
here we're going to add a shoot to it
so let's go ahead and
create the shoot function because the
player and the enemy is going to have
shoot ability
and so we'll say shoot self and um what
we're going to do
is say if self dot cool down time so we
need to obviously we need to
set up the cooldown timer because we
don't want you to be able to infinitely
shoot right so i'm going to change this
self.cooldown that we threw in before
i'm going to change that to cool down
time
so right when the game loads in first
you're going to be able to shoot
but then once you've shot we're going to
create a timer that's going to say until
you can shoot again the enemy's going to
have this as well but if the cooldown
time is equal to zero
um and then the next shot is something
we're going to load in so essentially
we're going to uh create
the next bullet and as you're shooting
we're going to keep track of all of your
shots in a list for an enemy or for the
player because when an enemy gets
destroyed we also want all of their
bullets to go off screen
so next shot is going to be equal to
that class we just made of projectile
and we're going to give it self.x and
then
self.y
and then self.shot that's going to be
the image right we already decided
inside of the character class
what the shot image is going to be
and so yes we will need to add this
inside of the player in enemy class but
all we're going to do is pull in the
init from the shoot as well
okay so then we're going to do
self.shots
dot append
the next shot
and then we will set self.cool
down
time
equal to one all right and uh let's
create a little function that's going to
use that cooldown so we will just call
this
cooldown
and what we're saying is this is how
we're going to track whether or not the
player or the enemy can shoot again so
if
self.cooldown
time
is greater than or equal to self dot and
now let's create
the variable that's actually going to be
how frequently you're allowed to shoot
the maximum time
and so we're going at 60 frames per
second so if i use a cooldown of 20 that
means you can shoot a maximum of three
times per second there's another great
place to fiddle with
and see how it affects the difficulty of
your game
this is all relative to your fps so
remember we have 60 fps
this will be incrementing once per scan
and so that means it'll update 60 times
per second so if you're just holding
down spacebar to shoot
you'll be able to shoot a maximum of
three times per second with a cooldown
of 20. making that 30 would be twice per
second if you lowered that to 10 you'd
be able to shoot six times per second so
play around with that a little bit um
see how you want to do it i'm going to
leave it as 20. i kind of like the way
that plays
but what we'll say is if cooldown time
is greater than the cooldown then we're
gonna set cooldown time equal to zero
meaning you're good to shoot now and
then we'll do an l if and if self dot
cooldown time is greater than zero
then we're going to increment it until
we get to that cool down time so
essentially what we're saying is if it's
greater than zero which is what happens
when you shoot we set the cooldown time
equal to one then just increment it
until you get to the point where you're
at the cooldown or higher than it if if
you've scanned twice and haven't
executed this
and then as soon as that happens you go
ahead and set the cooldown time back
equal to zero
okay and so that will handle cooldown
and then the next thing we're going to
want to do
inside of a character or a player is we
want to move the shots and the reason
we're going to put this inside of the
character class is because the direction
is going to be different if you're a
player or if you're an enemy right
so we're going to say self we're also
going to give it a speed and a target
because for the enemy the target is the
player for the player the target is the
enemy we'll give it a speed that we'll
call it projectile speed but you could
make it to where the player shoots
faster than the enemy if you wanted to
okay but in either case we're only going
to move the shots
as often as we check the cooldown timer
so
this will be where we call the cooldown
function it's nice to just have the cool
down only execute if there are shots on
the screen so
there's no need to move shots or check
for a cool down timer if you haven't
shot at all so this is a good way to
limit the processing power of your game
but we'll say for shot in self.shots
which is our diction which is our list
of shots right
um
we're going to do
shot dot move so in our projectile class
we set up that move and so we're doing a
lot of self referencing of functions
that we've already created but this is
way easier than
doing it all outside of the class for
player for enemy for every single
projectile
so if it seems like we're stepping on
our own toes a little bit it's actually
because we wrote the code
in a pretty smart pretty sleek way
so it's going to work pretty well but
we'll do shot.move at the given speed so
again we're going to pass projectile
speed in here
when we create these
but then we need to check if it's off
screen right because that's when we get
rid of it so if shot dot off screen
and we can just leave it like that
because it's checking
um to see if it is off screen and it's a
true false that it's returning
so if it's true then what do we want to
do we want to self dot shots
remove
that shot so once it's off screen get
rid of it there's no need to keep it on
and then we'll check l if
shots
dot
let me see shot dot collision so now
we're checking for collision with the
target
collision spell it right
and if the shot has hit the target then
what we're going to do is say target dot
health
minus 10.
so uh this one is going to get tweaked a
little bit for and we're gonna remove
the shot just like before just like up
here
um
this one's gonna get tweaked a little
bit for the player class uh because
we're not just gonna remove 10 from the
health we're actually going to have the
player when he hits the enemy kind of
one shot the enemy
you could leave it this way and make it
to where it takes multiple shots to kill
your enemies
that that's definitely like the way you
would handle a boss fight or something
like that
but this is going to be for all of the
enemy shots that hit the player
and we're just going to modify it
slightly
inside of the player class so i'm going
to copy this whole moves shots move
shots function that we just made i'm
going to come down inside of our player
class and i'm going to copy the whole
thing in here and we're just going to
start taking a look at how we need to
tweak it so it's true just the same that
we need to move every shot that you've
shot we want to check if it's off screen
and remove it if it is but then we'll do
else
and we'll say for target in targets
because the target is actually going to
be a
entire list of targets for the player
whereas all the enemies are only
shooting at one target the player it's
just singular but in here it's actually
going to be targets and we're gonna pass
in the whole list
okay so it's gonna be four
target in targets
if shot dot collision and so we're
checking for collision with every
individual target in a for loop
um but if they collide then what we want
to do is remove that target from
targets so target dot oops remove
targets dot remove that individual
target
and then we are going to also remove the
shot
so hopefully that makes sense i know i
went through it kind of quick
but the biggest difference between the
player shooting is when it hits an enemy
we're gonna immediately remove that
enemy whereas if an enemy hits the
player it's just gonna do 10 damage off
the player's health which we made a
hundred
you can tweak with how much damage it
does and make the game a little more
unique if you want to change it up but
that's what we're going with here
for the start of it
so okay that was a lot of writing code
to get to this point but now we've set
up the projectile class and kind of the
functionality behind it
so let's go ahead and
actually create some projectiles okay
so while running let's go to all of our
main loop functions
i'm a little too low here
window.blit up here wave size equals
five and let's make projectile speed a
little bit faster than the player or the
enemy moves and we'll say projectile
speed equals six
we'll bump the enemy speed back down to
one
now there should be shooting and
actually clearing out of the map we
don't need them to move quite so fast
and let's go ahead and update
if game over
while running
here we go okay so let's go down to
where we create our enemies and let's
handle we already have a 4i in range of
wave size where it creates the enemy
and then we add them all to the list
and then down below we say for enemy in
enemies move the
move by whatever speed we gave them
let's also do enemy dot
move shots
and then we'll say
move those shots at
minus the projectile speed so if you
remember
we said when we were creating this i
know that was a while ago now but we
said the enemy shots are going to need
negative speed and they're going to need
to be targeting the player
and
that's because they're moving from right
to left so we need to be subtracting 5
per scan or 6 per scan whatever we made
the projectile speed
but then let's make it a little bit
randomized for when the enemy is going
to shoot as well and we'll say for
random range 0 2 and here's where again
the frame rate is going to matter i'm
going to say 0 to 180
which is essentially picking a different
number in that range per scan and i'm
going to say equals to 1. so in theory
this should be shooting the every enemy
should shoot at some random point
approximately every three seconds
okay
and so we'll say if uh that's true then
let's do enemy.shoot okay
and then we'll also say if the enemy
dot x if they go off screen
so that'll be enemy.x less than or equal
to zero then we're going to subtract one
of the lives and we'll also
remove the enemy from the enemies list
okay
so now we're handling actually getting
rid of enemies in the various different
ways
and then the last thing we're gonna do
down here
is we're also gonna move the player's
shots and here we'll give it positive
projectile speed
projectically
projectile speed and we're going to say
the target is going to be the list of
enemies
and the last thing we need to do is we
need to include the space so if
active keys just like before and now
we're checking for pi game dot k and
this is going to be k space
and if that's true all we have to do is
player.shoot so you see we wrote a lot
of code on the front and it did a lot of
thinking and designing on how do we use
the similar code between the player
between the enemy and everything but
then when it comes time to actually
assign it it's relatively simple code
so we stuffed a lot in here really
quickly but finally let's run this and
see if we're shooting if we're able to
get rid of the enemy
let's see
pie game mask dot surface
argument one must be surface let's take
a look at what we've done
it's a bit of troubleshooting there but
the problem is we said self.sht which
could be any sht word which is not good
but that needs to be self.shot that's
why it was saying that when the enemy
was shooting it said uh that its type
was none because we never defined it so
you can see now
we are
able to load it up and well but we
spelled something else wrong we said
kalison
so uh
you know this uh i've made a few jokes
about while i'm typing when i'm doing
these tutorials i try to go pretty fast
and i talk a lot which
leads to some typos so
that's the sort of thing you run to do
your troubleshooting and let's see now
so i can hit the spacebar
um and i can actually shoot the guys and
i can get them deleted off the screen
but we don't see the bullet so let's go
ahead and take a crack at what's going
on here
okay so the reason the shots were
actually
active and uh taking out enemies but we
couldn't see them we have to add a spot
to actually draw the projectiles onto
the screen which we missed when we were
doing all our initial code and what
that's going to be is four shot in
self dot shots which is the list in
which place where we're keeping track of
all of our shots
we actually need to call the dang draw
function and put them onto the screen so
i think that's all we missed let's go
ahead and take a crack at it now
and we can see the bullet i'm gonna go
ahead and hold down the space and we can
see yeah it's shooting at our uh at
about three shots per second which was
our cooldown
let's see real quick if i can
get all of the enemies that have spawned
and let's see if our level incrementer
is working properly and then the next
wave that spawns should have five more
enemies
try to dodge their bullets too all right
so we're on level two let's take a crack
at
this and see how many
i'm gonna go ahead and get hit a few
times and we can see it deletes the uh
bullets off the screen but we haven't
put any like game over conditions in yet
so
in theory i'm going to be able to take
so much damage that my health goes
negative and it's not going to do
anything but you can see we lose a life
when an enemy hits the left side of the
screen which is great again we haven't
put the game over checker in yet so
lives can go negative if i were to leave
this playing it would just get up to
infinite levels and spawn more and more
enemies and i would get ridiculously
negative scores my health would be super
negative so
there was a ton of content in this video
i really appreciate you guys baron with
the tutorial bearing with me through the
troubleshooting and we're getting really
close to a full functioning game so i
hope you've been enjoying it if you have
please leave a like on the video
subscribe to the channel it helps me out
a ton and let me know what you want to
see next in the comments below be sure
to keep a look out for the final
episodes in this tutorial series and as
always good luck with your code and
thanks for watching thanks bye