 Okay, welcome to space arena the ultimate python turtle graphics game tutorial part six in part six We're going to go ahead and make some health meters above our sprite objects Okay, so yeah, let's get started So basically what we got to do is We're essentially we're just drawing a line Okay, and based on whether the the player is healthy or not healthy We're gonna be changing the color and the length of that line So and and the enemy is not just the player So I'm gonna go ahead and put this into the sprite render method because I want to have this for all Sprites that have health now that may be a little tricky because things like power-ups won't have health But I think I know a way around that so let's give it a shot So the first thing we need to do is we need to give each sprite health so self dot health and let's just make it easy make it out of a hundred and self dot max health Equals 100 now these should be the same and the reason I'm using these is because let's say that as the game progresses I might want to make the enemies stronger or if I get a power-up I want to make the player stronger So their health goes up from 100 to 200 or if I end up putting a boss into it the boss might have 500 health So we really want to make this something that can be changed and then that actual health is the current state So once I get hit by a missile or I get hit by an enemy my health goes down until I'm destroyed And again, I'm just using a hundred to make the numbers easy So in my render method I'm gonna go ahead and do something like this Now this is might not quite work. I want to come back to this one, but what I want to do is is So draw a health meter so if you remember I mentioned that the Sprites by default are 20 by 20. So we're gonna go ahead and use that in our program Now we might want to change that later But just keep it consistent for now. So I'm gonna use my pen. Oops. Don't I forgot the pen Argument here. So I'm gonna go. Oops. Shouldn't be an update. That's why that's why I didn't have it This is what happens when you talk and type It's like I always tell people what's harder than it looks. So my render method not my update method That was silly. So draw a health meter Okay, so I'm gonna take my pen. I'm gonna say pen dot go to Actually, I'm gonna do I'm gonna make a new method for this. I'm gonna say deaf Render Health meter. Let's let's go ahead and do that. Do it that way self comma pen and So I'm gonna say pen dot go to and it's gonna be self dot x so it's the x-coordinate of whatever object it is Minus 10 because remember it's 10 wide and then self dot y plus 20 Yes, it's gonna be floating above the object So basically what I'm doing is if the object is a square when we're going the top left corner and up 10 pixels I'm gonna set my pen dot width 23 and I'm gonna put the pen down So that it will draw again This this method will only be work consistent with the turtle module And that's what this is designed to work with if you were doing this in pie game You have to use a slightly different method But the the rest of the program would work the same the rendering would be a little bit different So I'm gonna do pen dot set heading and I want it to go from left to right. So I'm gonna set the heading to zero now Let's go ahead and try this. So what I want to do if my health Divided by my max health Is less than 0.3. So if I've got 30% left or less I want my pen dot color to be red L if self dot health Divide by self max health Is less than 0.7 and you can change these numbers. These are just what I chose pen dot color is yellow So as my health goes down, I want it to change colors Otherwise I'm gonna make it green Okay, so green means you're pretty healthy So now watch what I do here pen dot FD 20 Times because it's gonna be 20 wide all together self dot health divided by self dot Max health So let's go ahead and try that see if it works So render health meter. So what I'm gonna do is in render. I'm gonna do self render health meter and I got to do pen there and Then in my player because I it has a separate render method. I'm gonna have to do self dot Render health meter And again past the pen as the arguments. Let's go see if that works. I have some problems with it But yeah, we have some problems Self dot mouse self underscore max health is not defined. I wonder if anybody caught that while I was typing it 74 self Dot max health So let's go ahead and run that Okay, well it kind of works We've got the lines there. We forgot to do pen up. So After this is done we're gonna put pen dot pen up because it'll keep drawing fill the pen up It's been run that again Okay, so now we've got some health meters Above each of these objects Alrighty, which is kind of cool Now what we got to deal with is what if Now this is the case. It works if the health is a hundred percent Let's go ahead and change the health to let's say 50 just to see what happens So it still works, you know, we notice it's yellow now because we we set it up to be yellow I don't really like that. I don't like the lack of balance. So what I did was After we draw the the colored part of it We're gonna draw a little gray part of it So I'm gonna say Say pen dot color gray And then what I'm gonna do is I'm gonna do something similar here. I don't want to go As far across as I did but if it's 50% So what I'm gonna do is self dot This actually took me a long time to figure out when I did this self dot max health I don't know how I screwed up the math, but I screwed up many many times So max health minus the current health Divided by the max health. So for example, if I've got 50% health So max is 50 or max is a hundred minus 50 over hundred That's 50% if my health is 30. Let's say my health is 30 It'll draw 30% across and then 100 minus 30 is 7 70 divided by 100 is 70% and I'll draw the rest 70% in gray. Let's go ahead and run that Something went wrong Let's make that self dot max health self health Probably it's a One of these types of errors I needed to put that around there. Yeah Multiplication and division take precedence over subtraction So I need to put an extra set of parentheses over that. Let's go ahead and try that again There you go. You can see each of the objects has 50% Health now again, I mentioned I didn't want the power-ups to Have health, but I guess we can address that some other time. So I'm gonna go ahead and Put that up to a hundred so that each of our object starts out By default with a hundred. Oops. We've got a little bit of extra gray there. No, why is that? health max health And 20 times max health Kind of weird There's a little just a tiny little bit extra gray there and I'm not sure why it does that But I'm gonna have to leave that for now. I don't have time to look into that one right now But it's basically the same code as I had previously. So I'm not sure why it's doing that, but That's one of the things we're gonna have to live with for now. So if anybody figures that one out Let's try this 19. See what happens. It's kind of weird No, we still have it 20 so max health minus self-health divided by max health such to be zero That is a mystery, let's put I'm just kind of curious try this out put this over I bet Let's see here That is a weird one But I'm gonna waste your time trying to figure that one out We'll have to live with that one for now if it comes to me later. I'll pick it up. Okay, so there you have it health meters Pretty darn cool the end