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 5 covers checking if the player's health is gone or if all the available lives have been used and ends the game based on those conditions. However we implement a restart menu to allow for replaying even after dying and that requires separating the main menu functionality into a separate functional loop from the main game functionality. This allows for a start menu/restart menu between runs of the game. We also add collision detection between the player and enemies!
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!
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 five of our halo space
invaders with python and pygame tutorial
series and so far in the first four
parts we built a pretty sweet game you
have this little master chief we control
them with wasd for directions and uh we
shoot the enemies shoot at us at uh
randomly generated intervals and uh we
can delete them they can they can do
some damage to us and we're not
displaying player health yet so in this
video we'll be doing a health bar as
well as taking a look at game over
conditions and actually like restarting
so let's get into it um the first thing
is i don't really love that the player
player's a little bit bigger than the
enemies i think he looks a little too
big for the screen so i'm going to cut
it down to 70 by 70.
and i just like the size of that a
little bit but again you can leave it as
is if you want
it certainly is up to you that's a very
customizable thing in the game and it's
just totally up to you so let's go ahead
and take a look at actually creating the
um players health bar on the screen so
it's going to be a new function that
we're just going to put inside of the
player class because
you know inside of the enemy there's
really no need to put a health bar since
we're making it to where if they get
shot once they're done
so let's go ahead and define a health
bar and all we have to do is pass in the
window that we're going to draw it on
and it's really going to be surprisingly
simple we're just going to draw two
rectangles on the screen we're going to
draw a red one
in the background that's showing
[Music]
max health so we will do uh two five
five zero zero which is the rgb value
for red right it's red green blue
um
and then it needs some coordinates and
this part might seem a little confusing
it's we're going to start it at the x
which is the left side of the
image and then we're going to do self
dot
for the x coordinate but then for the y
coordinate we're going to draw it at the
y coordinate plus
self.image
dot get height and this is going to get
the height of
our player and it's going to shift it
downward on the screen by that height
and then since we don't want touching
the character's feet we'll just scooch
it five more down
and then self.image dot get
width and that's how wide we're going to
make the bar so it's going to go from
our left side to our right side of our
image
and then let's just make it a
height of 10. so we did x y
width and then height so starting x
coordinate starting y coordinate and
then how wide we want to be and then how
tall and that's our red rectangle i'm
going to copy that whole thing for our
second rectangle and you'll see we only
need to modify it slightly now because
that's representing the maximum health
bar this second one is going to be green
and it's going to actually be our
current health bar
so we're going to
keep the starting point at just the same
spot but what we're really modifying now
is the width so instead of just
self.image.getwith
it's going to be get with times
self.health
and then it's going to be divided by
self.max health right and i'll talk
about that in just a sec i can't really
talk and type as we learned in the last
video
so let me just talk about this real
quick the the get width is the maximum
width of the bar so when you have full
health right health and max health are
are going to be the same variable so
this self.health divide by
self.maxhealth is going to be equal to
one
and so we want the green bar to fill the
screen when you're at max health but
then as you're losing health this
variable will go down and down and down
and this will become a smaller and
smaller fraction and so this bar will
keep incrementing down
so that's the idea behind the health bar
and then the last thing that we need to
do is we need to modify the draw
function so i'm actually going to steal
the draw function from
the character class and we're going to
modify it for the player now
we're going to come in here and we are
going to say yes we want all of this
same stuff so we can get rid of all of
this and instead of retyping all of it
which would still work technically it'll
be easier just do super dot draw at
window and then we'll add self.healthbar
on the window as well and so that should
be all we need for a health bar let's go
ahead and run just that stage real quick
let's see what we did
super has no attribute draw that is
because i forgot parentheses
in my haste
here we go all right so we got our green
rectangle i'll go get shot real quick
and see sweet so we're losing you know
10 of our health every time just with
that quick function
um so let's move on the next thing i
want to take a look at is going to be
checking for game over
alrighty and so i'm going to create a
new variable with that i'm going to call
gameover and we'll set it equal to
false to start the game
and
what i'm going to do is i'm going to set
the conditions that will set game over
equal to true and we're going to do that
inside of wall running because it's part
of kind of the game logic so
what i'm going to say is while running
if lives is less than
or equal to zero
or the player's health
is less than or equal to zero right and
i just do less than or equal to in
theory the game will end when they equal
zero but we should do less than or equal
to if you get hit by two projectiles in
the same instant or two enemies hit the
left edge and your lives goes negative
temporarily
it's just better to do it this way
so we'll say at that point game over
is going to be equal to
true and then uh what i think would be
useful is we have it sit on a game over
screen for a little bit of time
and they'll take you back to like the
restart menu so we're going to use
another variable that i'm going to say
game over counter
and this is going to be very similar to
how we did the cool down scenario and
i'm going to set it plus equal to one so
the only way we can use that variable
is if we come up here and we actually
initialize it and say game over counter
equals uh zero to start the game
and then we'll come down into our
game over counter if lives are negative
or the player's health is negative
now we'll come in here and say if game
over
and we're just kind of separating out
functionality there's no reason you
couldn't put some of this together but
this is going to be a little bit easier
so we'll say game over text and we'll
create some new text that's going to be
equal to main
text
just like we used for the
for the lives and the score and it's
gonna be maintext.render and then we
give it the text we want and uh it's a
little
alien reference but um
i always instead of just saying game
over i like to say
game over man
um but whatever you want your game over
text to be and then uh put it on the
screen and then i think um we'll do
green again like i said it's halo
green looks pretty sweet and then let's
get that drawn so window
dot blit just like always and then game
over text
game over text and then we're gonna give
it its coordinates for drawing it on and
this we're actually going to want to put
it dead smack in the middle of the
screen so you could find this same value
by doing some guess and check
but there's a kind of an elegant
solution to make sure it's in the middle
of the screen and that's going to be
using that get width just like we used
for the health bar
it's going to be
gameovertext.getwidth and then divide by
2 right because
we want in the middle of the screen
so then we'll put it at 350. it's just a
good bit down on the screen and let's
see
if that does it
and actually we're going to want this
part in update window because it's a
piece of drawing and we've been putting
everything involving drawing
in update window so we'll throw it in
there but we are also going to
use some functionality if game is over
in our running loop so if game is if
game over i like the idea of pausing on
that game over screen right
and we'll do
a counter check so if game
over
counter
is greater than and here's where we'll
say um i'm going to put it in terms of
seconds so frame rate times five that's
that means we're gonna leave this text
on for five seconds but then once it's
over we're gonna set running equal to
false so we'll actually close out of the
game
and otherwise we'll say continue so run
the loop again and
that's how we're going to be
incrementing this counter when the lives
are done
and then once it's been over for five
seconds we're going to set the uh we're
gonna set the
um running variable equal to false and
that's how we'll end the game so let's
take a look and see if we can get game
over conditions real quick shouldn't be
too hard i'm not that good at this game
i'll run into some guys
oh that reminds me before we test the
game over i want to really quickly add
collision for uh the player with the
enemy because it should delete an enemy
it's going to be a really easy add here
we'll come down to where we're checking
for collision with the
enemies and and for shooting and
everything and we'll just add
for enemy in enemies
another if statement and we'll say if
collide
and then we'll check between the enemy
and the player
and then this is where we'll do
similarly to when he gets shot by a
bullet player.health
minus equals 10 but then we're also
going to delete the enemy so enemies dot
remove
that enemy and that should be all we
have to do and then we don't need to
check if it's off screen if it's
collided so we'll make that an l if
statement and now it should be a lot
easier even to get game over so let's
see we just added enemy collision
detection
and see i'll get shot i want to run into
this guy see if he deletes yep nice and
you can see my health is going down so
next time i get hit here
um
should
give us game over and then end the game
so there we go game over man i can't
move any more and in a second or two
here there we go the game ends
so that's pretty cool um but obviously
it's not ideal if you want a game that's
really replayable then you don't just
want to close the game every time um
every time that you die you want to give
them the option to play again so we're
gonna do something i'm gonna create what
i'm calling a start menu and this will
pop up right at the beginning of the
game so you're not just launched into
the action and then it'll also be what
pops up after you die
so we'll say we'll set a new
font and we'll
make it bigger because this is going to
be on the start menu now so pygame
dot font dot sis font if you want to
keep using the same font just make sure
you copy in the line from before we're
going to make this bigger
let's say 50 and we'll do true and then
we'll do
false okay
and then in here we'll say running
equals true just like our other loop and
we'll say wall
running
and we're going to copy in the
background
window
dot blitz so essentially what we're
doing is we're building the skeleton of
the game but we're not having it play
yet until we do something to initialize
it so we're going to draw the background
in just like before but now we're going
to create a start label
and we're gonna
do text dot render
and we're going to have it say
click
anywhere to begin
okay
and uh
let's say true for alias
two five five
fifty
twenty just kind of a good mostly red
color that uh shows up nice on my
background
that's
just pick whatever color you want it
could look good in green again
and um
we're going to draw it onto the screen
window.blit
at location of
start
label
comma
sorry that's the text
and then the location is going to be
equal to
width of the screen divided by 2 minus
again start label
dot get width you should be familiar
with this one by now divided by two and
then we'll drop that at 350 as well
and then we're gonna do
pygame.display.update just inside of
here
and then we'll say for event
in pi game dot event dot get
right
and we'll say if event dot type is equal
to pi game dot quit and it may seem like
we're recreating some of the stuff we
did very early on and we sort of are but
it's because we want
um this menu kind of layer to run when
the game is over and kind of to be a
jumping off point for each iteration of
the game so then we add a second one in
here which will be pi game
dot mouse button down
and so this when we say click anywhere
to begin this is how we actually handle
that we say if the mouse button is down
then run that main loop function that we
did before
so we're going to get rid of just
calling that by default and instead
we're going to
and back here you want to throw down um
just one level in
pygame.quit that'll help you not have a
infinite loop
and then instead of calling the main
loop at the root level we're going to
call start menu at the root level and so
this should give us um
a menu that's going to say click
anywhere to begin when the game loads in
perfect and then if i click this i'm
hoping the game is going to
start and it's not so let's take a look
at what uh
what we need in here so
it's gonna be this is indented one too
many times let's try that
click anywhere to begin there we go
we're playing now let me die and let's
see if that menu pops back up
shouldn't be too hard i'm not
particularly good at video games
of course if i try to get shot then i
probably will accidentally miss there we
go game over so when this finishes i'm
hoping it takes us back to our menu
there we go click anywhere to begin and
we're back to the start so just like
that we've put in some restart logic we
added a health bar and the last thing
that's really going to take this to like
a fun replayable game that also just
like gets you excited to play it is
going to be adding some sound effects
and um
kind of taking it to the next level so
we'll mess with some style things in the
next game but that's also really a great
place to kind of make the game your own
so if you have any questions about what
you've seen so far be sure to let me
know about in the comments below but i
hope you're enjoying the tutorial series
if you are be sure to 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 in
a future video in the comments below and
i'll get back to you as soon as i can
and as always good luck with your code
and thanks for watching thanks bye