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 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
okay part two in our halo space invaders
tutorial series here is gonna be uh
getting the player drawn on the screen
and setting up the player controls so
just like before how we grabbed a
background image for the game
grab your
player image for the game
and i
am going to copy the whole thing we had
with the pygame.transform and scale and
everything because if you grab an image
from the internet odds are it won't be
exactly the size that you want
if you built custom graphics then maybe
it is but in any case uh grab your image
my uh mine is called chief.png i typed
in 8-bit master chief to google images
downloaded it went to a website called
like remove
bg.io or something like that i typed
into google remove background from
images did that and i got this image
which is huge but it looks sweet it has
no background
and so
it's just in the same directory as
before with the halo bg but it's called
chief.png and then this one we obviously
do not want to scale it to the size of
the entire screen that would look pretty
ridiculous so what we are going to do is
we are going to scale it to a smaller
size i've played around with it a little
bit i like the look of 80 by 90.
it fits pretty well in the screen
and again user image scale it however
you want and uh this is just something
that i found works for me so if you want
to follow along pretty exactly with this
then uh that that's how we're gonna be
doing it
but
okay so that's the image that's my spiel
about the image and what we're gonna do
now is we're gonna create a new class
because uh we want i want the um
enemies and our player to kind of use
some common things like being drawn onto
the screen movement there are things
that uh i are going to be not just
specific to the player they're going to
be specific to the player and the enemy
and so you'll see this a lot more once
we do go and we add
um enemies to the game as well as just
our player it's going to be useful to
set it up this way so even though it
takes maybe a little bit more time on
the front end
we're creating
and it doesn't need parentheses like
that by mistake but we're creating a
code that's going to speed up the
definition of everything else for the
rest of the game so we create a class
and it looks like it thinks i've made a
typo here
i don't think i have
and we're gonna call the i think it was
just because i
had those parentheses in the beginning
but what every enemy and our player is
going to call in common we give them an
x and a y coordinate and they're all
going to start with
health of a hundred so even though we
want them to have health
we can just say health equals 100 right
in there we don't need to
just pass it in as a variable with no
default value
so
then we're just going to do some self.x
equals x and if you're unfamiliar with
unfamiliar with object-oriented
programming in general
there are some tutorials on this channel
that can help get you a little bit
caught up to just why classes work this
way
why functions work this way but i'm
going to assume you have a little bit of
knowledge
we're essentially taking the variables
we pass in and assigning them to
variables that we can use inside of
functions inside of our class
so health is going to be equal to health
for now we're going to say self.image
equals none because i'm not typing
self.image equals none because for the
player and for the enemies this is where
it's going to differ a little bit
and then we'll say self.shot is also
equal to none so we're going to create
some code later for tracking
if they've shot and then create a
library of all of their shots
and uh to just to start those are both
going to be empty but we're setting
ourselves up for creating code that's
going to have multiple projectiles
coming from one character
and then we'll also create a cooldown
variable
tracking how long
between shots and we'll set that equal
to zero initially
and then the only other thing we're
going to do for all characters
is we're going to draw them and so all
we're going to do when we call a
character is we need to say
you know instance of the character dot
draw and then say draw it onto the
window
and so this is where we can actually
just say
blit it and then use image and then for
the coordinates self.x and self.y so
when we get into uh creating the player
and actually creating the instance of
this class that will make a little bit
more sense but essentially all we have
to do now to get the image onto the
screen
is create our player and then pass in an
x and a y coordinate and
call the dot draw function inside the
character class and that's going to take
care of everything for us
so now because the player is unique
we're going to create a sub class and
this class character is going to or the
class player is going to inherit all of
the properties of the class character so
that's why when defining this
we say okay this is going to be the
player class but right here we're saying
inherit everything that the character
class has already
and then in our init function
our initialization of this
we're going to do just the same self
whoops self x y and we'll bring in
health default to 100
and uh we're gonna just call like so you
use super dot in it when you want to
bring in the init function of a previous
so this is pretty nice um we're just
saying x y and health
and then we don't have to type out
self.x self.y self.health all this same
business we're just calling this whole
init function inside of this one so
that's nice it saves us some lines of
code saves us some typing
and now we're actually going to instead
of self.image equals none
we're going to say
player so before we created this the
static constant all caps player
and now self.image equals player even
though we said none up here this comes
after calling the init function so this
is going to overwrite it and give it the
player
and then for self.shot this is going to
be where we say what image and if you uh
think back to the tutorial we had the
player shooting bullets right because
master chief he shoots like the human
looking guns
so um
i have an image for a bullet i think i
called it bullet and you can see it's
facing vertically right now in the game
we want to play horizontal so um it is
going to use essentially this same
transform
but we're going to do even another
transform after that so let's go ahead
and
bullet equals pie game dot image dot
load
and it's going to be images
backslash
bullet
dot
png just like that
i think that's good
i'm not sure it wants to format it all
weird oh it's because i didn't put an
equals in there alrighty we're going to
play around with transforming it once
we're actually shooting it for now i'm
just going to leave it like that
but that's what we're giving in the shot
here is what
that character's projectile is going to
look like
so let's leave it as that for now and
then um
we're gonna use something called mask
this is how you get really accurate hit
boxes
if your image or character is really
close to a rectangle you might be able
to use like pygame's default collision
detection
but it converts your image no matter
what into a rectangle so if you have a
shape like a person or an alien that's
not perfectly rectangular you'll get
really frustrating points in your game
where it's not actually detecting to see
if there's a pixel in that image it's
just taken the outer dimensions of your
image and said okay this is the
rectangular hitbox if you use
pygame.mask
dot from surface you can actually tell
it okay look at
an image and in this case it's
self.image and create a mask which is
going to be equal to
um the actual
pixels the the part of that rectangle
it's pick
it's grabbed that are occupied by pixels
so mask is a lot more accurate way of
doing a hitbox in pi uh pie game so
that's why we're doing it this way we
say self.image and then we're going to
say self.max health because part of the
game is drawing a
drawing a health bar underneath the
character that's going to show how much
they've been damaged and for that reason
we're going to have you know a red
max health bar and then the part that's
actually occupied by their current
health will be green and laid over top
of it
so these are things that just the player
need but now to actually get one on the
screen we just come down into
underneath where we defined main text
and we define our player and so player
is going to be equal to player class
and then
we
are just going to give a starting x and
a starting y and we'll do a hundred and
six hundred that looks pretty good
and then we're going to down in the
update window
this is where we put player
dot draw
and we give it i believe the only
variable it needs is window and so we
say window
all right and let's just run this real
quick
actually this needs to be above the
update function sorry
if you uh draw anything
after the update function it's not going
to show up on your screen so let's take
a look here
what did i do
okay sorry about that uh just go with
images forward slash bullet
the backslash is the new line character
and it's also inside of a string it
interprets the next character literally
so just use images forward slash bullet
and i know i used backslash up here as
well but you can swap them all out with
forward slash you see the way up here in
the caption we have backslash and then a
parenthesis that's saying interpret the
net or apostrophe that's saying
interpret the next character literally
in defining a file path you can use
forward slash or backslash but it gets a
little goofy in strings so my mistake
sorry um but moving on
uh let's see so we've got our bullet and
now if we run this
we should see our chief on the screen
there he is a little guy in the bottom
left but he can't move yet because we
haven't set up player controls so that's
what we'll do next and that'll be the
end of this video
but so this part's actually really easy
we can go down and since we want
um
we want to be able to move up and right
at the same time or down and left at the
same time we're going to keep track of
all the keys that have been pressed
and we're going to do that by creating a
dictionary so we're going to say active
keys equal and it's going to be pygame
dot key dot get pressed and this
function is getting everything that's
been pressed on the keyboard right now
and we're going to start looking at
individual characters to see if they
exist in the dictionary so we're saying
if active keys pygame.k and i'm going to
make this a wasd game you can use the
arrow keys if you want just instead of
key
k underscore a you're going to be using
k underscore up underscore down
underscore left underscore right
and then we're also going to check and
make sure that the player is still on
the screen so the player has the
function x and y right um and we're
going to check to see if i'm letting you
move to the left you need to still be on
the screen
so we're going to create a new variable
called player speed
and this is something you can tweak to
kind of set how powerful you want the
player to be how quickly you want to be
able to maneuver five felt pretty good
when i was making it before and so what
we're checking to see is if and the
player's current position
after we change it so if you're using a
you're going to left so you're saying x
minus the speed needs to be greater than
zero if that is valid and you are
pressing the a key then that means we're
going to let you move so we're going to
just take the x position and we're going
to subtract it away from the player
speed
and i'm going to copy that exact
little routine here for um four times
and we're gonna do a and then we're
gonna do d so that's left and right and
then we're gonna do
w and we're gonna do
s and that'll be up and down
and then this one we're going to check
that player speed
and player x
so player x now it's plus player speed
but we also have a wide
player so we need to add some width and
this will change a little bit depending
on your
character that you're using if you are
following along with my dimensions as
well plus 70 looks pretty good for the
right edge of the screen but this may be
something for you to just
to just tweak but obviously it doesn't
need to be greater than zero now it
needs to be less than the width of the
screen
and if that is valid then we're going to
move it to the right
so now for up and down instead of
looking at player x we're looking at
player y right so player y player y
player y
player y
and what we're gonna check for w is if
you can go up and so we're gonna check
that if player y minus player speed is
greater than zero just like before
because remember in the pi game
coordinate system the top left corner of
your game is zero
so it's actually kind of
counter-intuitive but if you're going up
you're decreasing the your height in
terms of pixels so that's why you
subtract player speed from player y
and now we're going to take a look here
at down and so it's going to be the
opposite it's going to be a lot more
like when we're going right
we're checking at player y plus the
player speed
plus 80.
again that's kind of because of the
dimensions i set my player up for and
this is absolutely something you can
kind of guess and check and then put it
on the screen
and make sure it's functioning okay so
right here i believe we have four
directional control that'll stop at the
bounds of the screen and it should work
for our player so let's go ahead and run
it
all right so we got the chief in the
bottom left and yep i can do up and left
up and right down
i can do multiple directions at the same
time if i hold left and down
still at the same time he doesn't go
anywhere obviously because there is no
um there is no uh control he stops at a
pretty good point on the right stops at
the top uh bottom and left
and so you can play around with your uh
kind of extremities just add and
subtract a few pixels in those um player
controls since the bulk of this game is
you hang out pretty far on the left and
you
and you shoot towards the right
this looks totally good for me but you
make it your own tweak it however you
want
in the next series um in the next
episode in the series we should be
taking a look at adding some enemy waves
and uh pretty soon we'll be adding the
shooting mechanism we'll increment the
score we'll add uh waves so
we're getting there but we've already
got a playable game with a character in
it so hopefully you're enjoying this
series if you are i appreciate a like on
the video a subscribe to the channel
helps me out a ton to create tons more
new content like this and as always good
luck with your code and thanks for
watching thanks bye