and hello everyone welcome back to
another
love 2d tutorial
so in the previous tutorial we worked on
the player collision so if a player
flies and they hit an asteroid then p
the player explodes
in this tutorial we'll be adding lives
to the player so once the player
explodes instead of the game just ending
they have a few more lives they can
spend before the game is completely over
so let's get started with that
first
i want to give you an example of what we
currently have so if we were to go here
and say love dot
that's what we currently have there goes
our asteroid off screen so it should
appear out about there somewhere yeah
and
boom
we explode
and we just kind of continue
just very boring
however when we explode that shouldn't
happen or when we hit something that
shouldn't happen in fact this should
happen
so this is an example of what we'll be
doing so as you can see at the top our
player lives are there when we hit an
asteroid the player lives they go red
they disappear and we start in the
middle of the screen again
and we can use up all our player lives
until we are no longer there
once we are no longer here
then
game ends the last asteroid does not
explode once we hit it that is to
prevent a bug but you don't have to
worry about that bug because i'm fixing
it here for you anyways
but let's get started
so
first we want to now take in the num
lives
so how many lives the players should
have
then down here we can just say
lives is equal to num lives or by
default they can be free so if you don't
pass any number of lives it will be free
by default
next up let's draw the player lives
so maybe right here underneath draw we
can say draw
lives
this is a function
that takes in self and faded
cool
we can copy a lot of things from this
player draw
so if you do not mind i am going to copy
basically everything from here because a
lot of this code is copy paste from
what we already do in draw because first
we set the opacity
depending on if it's faded the opacity
will change
and in here is where it fades a little
bit from what we do
usually against what we
want to do
we'll still keep a lot of this for
example this right here we want to keep
this
so let me just delete this part here and
this laser part as
well we can keep this polygon
and this color
we can remove the debugging because we
will be doing debugging on it
and we can also remove all of this
and now we have a bunch of the code
already given to us because we have
already coded a lot of this
nice let's just indent this to a
there that's much better
now
first we want to see what color or life
should be because if we go back to this
example i have
and i can just move it over
and as you'll see if we have three lives
then it's white the same color as the
ship
if we hit an asteroid then it becomes
yellow
and if we hit an asteroid again where we
only have one live lift if i don't miss
the asteroid which i did again
my aim is terrible
and now it will be red because hey you
should watch out buddy you only have one
live left
and that's what we want to do here so we
want to set the color depending on the
amount of lives the player has
so first we can say if
self.lives is equal
to two so they have two lives
then
we can copy this right here
paste it there and we'll modify it in a
second
then we can say else
and this will also contain an f and then
else so we're going to go like this
and then we can just copy this and
paste it here
and we can remove this
now if they only have two lines left we
want to make it one one and then 0.5 and
this will make it a kind of a universe
color
if they have one life left then we want
to make it red but not completely red
just a little red
so it will be red and then 0.2 green and
0.2 blue
and in here if they have three lives so
if they have three or more lives in this
scenario because this is two and a one
if you want to add like five lives you
can maybe add more colors or you can
just make them all white until it hits
two and they will change color
anyhow if they have one will be red and
if they don't have two or one if they
are free it will be white
now in the draw lives we need basically
to get the x and y positions or to give
it a default x and y position
you can make this like a more globally
state one like put it here or even put
it here
it doesn't really matter too much in
this scenario i just want it here at the
top of the screen so i'm going to do an
easy
local
x
plus
y
plus
is equal to 45 and 30.
so x would be 45 and y would be 30. so
45 from the side 30 from the top
then we can create a for loop so 4
i and 1
to self
dot live so the amount of lives we have
and we want to basically draw free lives
so you can just plop that in there
because the lives are the same shape as
the spaceship
and we can go
if we are exploding so if self
dot
exploding
then
and then
if
i is equal to self dot lives so the
amount of lives you have so we don't
want this to happen if we have more less
lives on the screen than what we
currently have or more you know
they want to basically make the
heart completely red
so
love dot graphics dot set
color
and then
one zero
zero so basically once we explode then
it should change the color of the
spaceship
to pure red until the explosion is over
in which case it will switch to
whichever colors are here
cool
the noun here since we can't just draw
the spaceship on the same position or at
the same position
everywhere we need to specify a few
things here to make the
the lives go on different locations
because if we don't
then the lives here there wouldn't be
free lives like i i wish i could show my
cursor here but there won't be like
three lives there
there will only be one and two others on
top of it
so we need to specify where to position
it
so instead of self.x here
we can go
and say
i so the current index we are on or the
current position
times x position and this will
give it its wanted x position
and then as for the self.y since the y
will never change unless you make it go
in the x direction servo or in a y
direction instead of an x direction then
the y will never change
so why
pause
but if you wanted to you could make the
hearts
go from top to bottom instead of from
left to right it depends on what you
want personally i think going from left
to right is perfectly fine
but once you have done that
then
boom
we are basically ready to go
now we also need to remember
that we don't want these to change angle
with the spaceship you could
there's nothing wrong with it it's just
if you make a change angle with the
spaceship
then that it could
kind of make the player a little bit
confused about like while he's playing
and suddenly he sees the spaceship
hearts moving as well it could be cool
but it could be very distracting
personally i think it's too distracting
and i'm going to remove it but you don't
have to if you think it's a cool feature
instead we can go here select all of
these
and remember this view angle we created
here at the top
that is the default angle or spaceship
is always at so we can just specify
view angle instead
so now it will always face 90 degrees
and never change but if you want it to
change then you could then you can 100
do that
i just don't care to make a change
cool
next before we continue i would like to
go to globals
and make sure this is here
just to make sure that is there so if
you forgot about this just put it there
nice so next up we can just go to the
main
file
and let's draw our lives so let's go
here
and we can say
player colon
draw
lives
and if the game is paused so game
dot state
dot paused because if the game is
supposed to want it to be faded
and here we go that shouldn't be an end
symbol my bad that should be eight times
okay now let's try it
there we go so now our lives are right
up here
as you can see
so that's nice
currently if we hit an asteroid
nothing's going to happen we're just
going to kind of die and respawn and
then hit them again and again again we
don't want that to happen anymore
because we now have lives we want the
player lives to decrease and the player
to restart from where he was where she
was
so
let's go to the top
and let's see and this is not the right
file we go to menu and here it says
explode time and then what the explode
time does
we can go and say
if
player dot explode time
if that is equal to zero
then we want to go
if
player dot lives
minus one is less than zero meaning if
the player
doesn't have any lives left
then
game colon change game state to
end it because the player the amount of
lives minus one is less than zero
meaning they have like negative one
lives left
then end the game
and we should make this laser equal to
because
yeah just because
but you could make it less than one if
you wanted to because this city player
has one life left now they hit an
asteroid now one
minus or now the player lies i mean
changes to zero and zero minus one
that is negative one that's less than
zero
so in return it should change the game
state to end it because the player has
zero lives left then you don't want the
game to continue
and then we can just say return because
we don't want this to continue the loop
or to continue to continue this loop
nice
otherwise just always go player
is equal to and then
a new player
and in the player
dot lives minus one
and there we go so now the player will
always have one less of a life
once they hit this or once they finish
exploding
and i should save without formatting
now if we run this
and let's see what happens when we hit
this asteroid
we lose the life
and we start back at the screen
the reason we started back at the center
is because we create a new player and
when we create a new player
the player by default is in the center
of the screen because that's what we
hard coded here in a sense we basically
said hey once we create a new player put
them in the center x and y and that's
what's happening here we're creating a
new player and storing that player in
the old player variable
so don't get too confused here we're
just creating a new player object
storing it in here but it kind of has a
lot of the old player stuff
my dad i just mean the lives
so yeah now if we hit an asteroid we go
we lose a life now we have less lives if
it's another asteroid we lose life
let's hit another asteroid here
and there we go
now the game has changed to ended state
okay meaning it's basically over
now i still want to do a few more things
before we continue
so here if
destroy asked
we can now go
if
and then else
now inside of the else we can now put
this
and in this if statement can check
player dot lives minus one if that is
less or equal to zero
then
we want to go
if
player dot explode time is that is equal
to zero as well
then you want to set this
here as well
so basically if the player lies
is less or equal to zero
basically the same check we did here
however this check will not execute
quite as quickly because this one will
execute after this check so this check
may be false then this executes where
this check could be true but it executed
after
that so see this executed after just
right here so after the check so player
lives here could still be two then we go
here and then player lives is one
because remember code goes from up to
down
they're doing the same thing here but
now it's here at the bottom and since
it's going up to down it will trigger
this
and then we check if the explode time is
equal to zero
then just do everything as normal
however
if the explode time is not equal to zero
then it just don't do anything at all
the reason for this is once we basically
finish the game yeah once we basically
get to the last asteroid and we hit that
asteroid for player
it can cause a few issues
and this will just cause or this will
just fix a bug in the future
but you don't have to worry too much
about this just put that there
save our game we run our game and now
we fly
we hit
we respawn
we wait for our targets to come back
we fly
we hit we respawn and now for the last
one we fly
we hit
it doesn't get destroyed immediately and
that is exactly what we want because
that could cause errors because let's
say that's the very last asteroid before
we go to the next level and then wants
to go to the next level once we hit it
but once we hit it we and we have our
last life there it's not going to be a
good match
because let's say we only have one life
left and there's only one asteroid left
once we hit that asteroid and we lose
our one life it wants to go to the next
level and this is in the future once we
implement levels
but if it wants to go to the next level
and we just lost our last life that's
going to cause a few errors and so we
say if we hit against an asteroid with
our last life
then it's game over
then we don't get that point or anything
like that
and cool
so that is that for adding player lives
thank you for watching i hope you all
enjoyed
and i will see you all again in the next
love 2d tutorial
you