 Welcome back to space arena the ultimate python turtle graphics game tutorial part 14 in which we make a radar So basically what we want to do Is get started? So we're gonna have our basic play screen and what we want to do is we want to add over here a Section where we can put some information and we're gonna have a little radar down here that kind of shows what's going on in the game So let's go ahead and do that. So if you've been following along which I assume you have We're gonna need to create a class So I'm gonna go ahead and scroll down here and I'm gonna start creating class So I'm gonna say class radar no surprise there and I'm gonna make my constructor remember to underscores Before and to underscores after and to set up our radar. We're gonna need four pieces of information It's X and Y coordinate of the center of the radar the width of the radar and the height of the radar What's nice about doing it this way is that if we decided to change the the size of our screen So you have a really big monitor you want to make a larger screen We can also change the size of the radar very easily to match that So just like we did before we do self dot X equals X self dot Y equals Y This should be be pretty, you know, I don't know natural by now If you can if I'm following along and doing it step by step And that's basically all we have to set up at this point, which is which is quite nice And then our radar is gonna have one other method, which of course is gonna be render and In this method, we're gonna need to send a few different things Of course self because it's an object we always use self and we're inside of a class definition So it's an important piece of information because I do get that question from time to time We're gonna need a pen to draw with and in this particular case Remember the pen is a turtle object. So that gives us all the different things a turtle object can do And then we're gonna need a list of sprites That need to be drawn on the screen or drawn in the radar so What we'll do is we will go ahead and just kind of draw the circle for the Radar so I'm gonna go ahead and say draw a radar circle And So let's say I'm gonna say pen dot set heading to 90 I'm gonna do pen dot go to self dot x plus 100 and 100 comes from the fact that the width is 200 and Self dot y And I'm such put that there And I'm gonna do pen dot pen down Because now I want to start drawing And then pen dot circle there is actually a circle man And again, I'm using hard-coded numbers here just because it's easier But what I should have done or what I could do I can still go ahead and do that is to do I say width divided by 2.0 and here I could do width divided by 2.0 that would make it easier if we decided to change that later and then I'm going to go ahead and Do pen dot pen up because I no longer want to draw Now I'm just gonna go ahead and run that and see what happens Of course, we're not gonna see anything here because we haven't actually created the object yet So that's our next thing is we're gonna create the radar object Okay, so it'll probably come as no surprise if you again if you've been following along. I'm just gonna do radar Equals radar remember it's x comma y and this little pop-up there tells you what you need And I've already mapped it out. I've kind of played around with it before 200 and 200 and 200 and then what I need to do I've created the object But then I also down here is in my well-true loop is I need to render the radar It's not gonna go radar render and remember I have to send a pen and sprites Okay, so if you remember up here, we created a list of sprites, and that's what we're using as well Yes, I'm gonna go ahead and run that and see what happens Okay, we got an error. Okay radar object has no attribute width Okay, so that was just a stupid mistake on my part because as I mentioned I am inside Well, that's a dude. That's a really dumb one So that with equals height and I realized that I should put self that width here and self that with there So yeah, that was me talk at the same time. So you can see down here. I've got my radar now Of course, it's off the screen You know, this is not where it should be and you know, it's it's kind of interfering here And that's that's that's to be expected. This is this is what I expected would happen So the first thing I'm gonna do is I'm going to Make this Screen wider. Okay, so I have some room for my radar so to do that I'm gonna go up to Where I set up the screen basically way up here and Where is that? Screen with a screen height. So when I set up the screen here, I'm gonna add to 20 and For the height, I think I just added 20 Okay, and the 200 comes from the fact the width of the the radar is 200 and the 20 I added I just found that like through experimentation that stuff was getting cut off And so by adding an extra 20 pixels, I could see everything on the screen So it's not not super scientific, but it does work. Let's go ahead and test that and that's about what I expected to see Okay, which which is good so far Okay, now you can see there's a little bit overlap and I'll get to that probably in another video So what we got to do now is we need to start drawing the sprites so I'm gonna go find my radar and I can just start going do it like that and then I say draw the sprites Okay, so watch what I do here. So for sprites in sprites So the first thing I got to check is if the sprites States equals active. So I only want to draw active sprites You know, there's something else I got to add there later, but I'm gonna leave that like it is for now so What I got to calculate is the radar x and The radar y so it's gonna be self dot x plus Sprites dot x What happened there minus? Minus player dot x Times there's a little formula came up with self dot width divided by game dot width and Then radar y is gonna follow the same pattern self dot y plus Sprite dot y minus player dot y times self dot I think it might be height height divided by Game dot height and then I'm gonna do pen dot go to Radar x radar y and just to make it easy. I'm gonna say pen Dot color. What's do? We just do I guess doesn't matter. What's do wait for now and then I would say pen dot What we do here shape size and I'm just gonna make it really small like 0.1 0.1 and None because we don't want a border and I say pen dot stamp I'm gonna update this a little bit, but this should give us a rough idea what this it's gonna look like and see if we're kind of on the right path Okay, and it does you can see so here's the player okay, and then So you see how the players moving here relative to The other game objects, so let me see if I can see if I fire Now you'll see now. This is off the screen Okay, so we don't want it. We don't want to show sprites that are too far away So we're gonna deal with that eventually but you can see now it is kind of working Okay, now we don't see the border, which is you know, okay? I guess we live with that But you see how it basically works. It was pretty simple So basically what we're doing is we're taking the center of the radar we're adding The distance from the player to the sprite and then we're multiplying it by The width of the radar divided by the game width Okay, so that gives us basically the whole How you put it like The whole width of the game in the radar, which we might not want later, but we can do that for now so Now watch what we do here. What we want to do is we want to make an actual nice little radar thing So I'm gonna do the following So the pen color Is gonna be the sprite Color Okay, and for now the pen dot shape is gonna be the sprite dot shape So let's go ahead and run that and see what happens Okay, so you can see here we got a little we got a little triangle for the player at the bottom and I've got you know a small little square and a circle in the color for each of the Sprites here, which is pretty pretty cool. Actually, okay Um a couple of things we've got to do here. You probably couldn't see it, but we need to also Set the heading before we do that. So we say So pen dot set Heading so it's the same shape heading as the sprite Dot so is it's heading I think that should do it And let's go ahead and just test that It's a little hard to see because these are really small Um, but they are that one is circular that one is square And it's hard to see but that is rotating when I rotate The player as well. So that's pretty cool. And you can see the missile Is going towards Yes, now you can see as it's adding stuff that the radar is doing what it needs to do Okay, so yeah, it's kind of it's kind of getting there. It's getting getting to where we want it to be Now let's go ahead and kind of update this a little bit better And see if there's anything else we need to do with that I mean actually to be honest, we could kind of leave it there And just have everything be really small like that and that might actually be Simpler I had I had a bit more complicated piece of code previously But actually now that I I look at this I actually quite like it and it's really simple So let's go ahead and keep it like that for now and see if we have any other errors But the one thing that we do have to fix Is that now this is Well a couple things we got to fix this is kind of overlapping. We don't really want that And the other thing is So what we're going to have you remember from the the video is we're going to actually have a whole block here of information So we'll have some text up here one of the radar down here So what we want to do is we want to move this over a hundred pixels And the easiest way to do that that I can think of right now Is to and again the hundred comes from the width of the The radar basically So what I'm going to do is in my Rendering let's look for our render Camera x let's say minus 100 Render the border minus 100 So everything has to be minus one right now. I could put uh, I probably should but we'll keep it easy I should put a radar dot width, but let's just go ahead and make it minus 100 And this one, I'm not sure about let's try it see what happens And I think we're okay there. Let's try that I might do it. Wait. No, we'll see Okay, all right Okay, somebody said the world be right back Okay, and I'm back and that was nice delivery from a very nice Japanese delivery lady. So, uh, basically here is what we've got So let's take a look at that real quick. Okay, so we've forgotten probably you haven't because you've been watching the video but I've kind of forgotten and Now I'm trying to get this to work and it's been Okay, so it's a little bit off and basically I realized that The reason it's off is That the camera Doesn't need to be added 100 the camera's fine the camera needs to be on the player So let's go ahead and do that So now you can see how there's an equal distance here To about here and again, we're we're going to put something in there later So we don't see some see the overlap. So that is the game now. What's what's cool So let's uh, oops, let's go ahead and do that and oops way. It gets really fast I gotta fix that later, but uh, let me uh, let me go ahead and just kind of make a little quick change here to the game size Wrong one. It's gonna be a little bit long. So it's hard to find stuff. So let's go ahead and let's go ahead and triple it Let's say 2,100 and 1500 And run it and see what happens Okay, so you can see how we've got items way off in the distance because now this is the whole thing, right? so Thanks, there's some weird Uh, it's drawing a little bit weird, but that's okay. Um, we'll fix that later Um, and there's no bad and there's no background, but it kind of works But you can see how the radar is still working as we expected it to. Okay, so that's Basically what we've got now the thing I want to do with the radar. I'm gonna put this back to 700 and 500 Is there's one more thing is we don't want to see everything on on the screen and the reason is that We want to assume that there's a certain level of radar That the player has like the player can only see on the radar Say a certain distance and that makes the game a bit more You know challenging and you know things will come on the radar and come off of the radar So what we got to do is we need to measure the distance between each of these sprites Okay, so what I'm gonna do is I'm gonna put this here and say So I say make make sure the Let's see how you say it the sprites is close to the player So I'm gonna give my player Actually, I'll give all the sprites a little radar just just make it easy. So I'm gonna say sprites So say self dot radar just for now will say equals 200 So that means they can see 200 pixels in either direction and So back to here And what I'm going to say is I'm going to calculate the distance so distance Equals and this is going to be using the Pythagorean theorem So I'm going to say it's sprites. Let's see say player dot x minus sprites dot x And this has to be squared. So it's a square plus b squared equals c square and That's squared plus player dot y minus sprites dot y And get squared and what I'm gonna do is to make it easier is I'm gonna square it by 0.5 That's the same as taking the square root. Okay, so trust me on that and Watch your parentheses because I know I'm gonna get a billion people asking, you know, it doesn't work I'm getting some kind of weird error. Make sure all your parentheses are aligned. This is a set This is a set And this whole thing has to be square root. So that's a set as well So then I say if the distance is less than player Dot radar. Was it radar? I think it was radar And then we stamp it. Otherwise we don't stamp it Okay, so let's go ahead and try that Okay, so okay, so it's working so you can see how this is close There it comes up on the radar Okay, so you have to So you can see how it gets closer, but then it disappears once it gets off the edge of the radar Even though I can see it on the screen Okay, so we'll have to play with the numbers later once the screen is bigger But that kind of gives you an idea of how to make it so that we can't see everything On on the screen and right now you can see a lot because you know, everything's really close But once we make this big this box bigger, it'll it'll work exactly as we expected At least I think it will so I know that was a bit of a long one But I think you get the idea of how to do a little bit of a radar So let's go back and watch that carefully and uh, you know, thanks for watching. Stay tuned for more. Take care