 Okay, welcome back to space arena the ultimate python turtle graph is a game tutorial part 13 in this part We're gonna go ahead and set up the camera movement. We're gonna keep it simple for now We're gonna have the camera centered on the player. So let me show you kind of what I have in mind here This is what we currently have now when the player moves Nothing else moves. So everything is moving independently But the camera for lack of a better term is focused on this spot What we want to happen is we want the camera to be focused on the player So the player is always gonna be in the center Okay, at least for now. We'll keep it simple. So if the player moves up Then relative everything moves down Okay, so the player stays in the center But everything else moves relative to the player and it's actually pretty easy to do So I'm gonna I'm gonna use a class here I should say because later we want to make it a little bit more fancy, but for now, let's just keep it simple So here's what I'm gonna do. So I'm gonna go ahead and I'm gonna go ahead and create a Class and I'm calling it camera Okay, now the camera is not a sprite Okay, it is its own type of object and when we initialize it We're gonna give it an x-coordinate and a y-coordinate So I'm gonna say self dot x equals x self dot y equals y Now as I mentioned before it's gonna be centered on the player So we're gonna need to pass the players x and y coordinate now right now We know that x and y coordinate is of course 00 that could change at some point and Then we're also gonna need an update method and I Want to say x offset and y well, let's just call this let's just call this X and y that's a matter here and then also we'll say again self dot x equals x self dot y equals y and every time we update it basically it's gonna go where the player is Because the player is gonna be moving relative to everything else So I'm gonna go ahead and go down to my main loop I'm just gonna go ahead and put Camera update here, so updates the camera and I could put it up above I just put down here so camera dot updates and it needs a new x y you can see that here it pops up Now x y is gonna come from the player x and the player Y Now I'm gonna run this just to see if it's compiling and if it's working. There's no errors Okay, there's an error lovely camera is not defined. Okay. Oops So I need to go ahead and create that. I'm gonna go ahead and create the camera Actually got to do it after the player because we need to tell it where the player is So I'm gonna say create camera Say camera equals camera and it's gonna be player dot x and player dot y Yes, let's go ahead and try that again Okay, so it's running now Of course it's not doing anything, but what we need to do is When we render every object we have to render that object relative to The player which is the camera x and y Okay, so if I if the player is moving to the right its x is increasing So that means everybody else's x should decrease It's kind of your product a little confused So I'm gonna go up to my sprite class and unfortunately I want to do this in every one of my update methods I also have to do it for the border, but we'll do that one last So where we have render We now have to do an x offset and a y offset And then when we say pen dot go to we subtract the x offset And we subtract the y Offsets we could call this camera offset doesn't doesn't really matter. I guess but I Just call it x and y off that makes a little more sense to me Same thing with the health meter if we render the health meter We're gonna need this for we need this basically for every render method Okay, so I'm gonna go ahead and put that in there There's probably a better way I could have done this, but this is this is pretty straightforward So I'm gonna put minus x offset And I'm gonna put same thing minus Y offset Everything else stays the same we just add add or when the case of track the offset And I think that should be that for that player Player now we don't have to I guess we do have to worry about the player as well update self And again, we're gonna do an x offset and a y offset and Same thing. That's update. We don't need that update. Sorry. We need that in render my apologies And and render Same thing where we have go to we're gonna put minus x offset and Minus y offset So you can see the pattern now missile Where we have missile render yep same thing here offset And it's going down through and putting it everywhere. It needs to be like so. We'll do the border last Make it easier to make sure this works and notice how the enemy is just being rendered Through the regular sprite rendering so we leave that as it is and same thing with power up Okay, so now when we do our sprite rendering We need to put camera dot x Camera dot y Now for now, this is exactly the same as the player x and player y We could have just done this with a player by now later We want to change this so I'm just gonna do it this way for now. Okay, so let's go ahead and test that and see what happens You got an error All right, health meter missing to require positional arguments. Okay, so that is line to 45 Render Okay 405 See we've got pan x offset y offset. Okay. Ah There we go x offset and y offset That's that. Let's try that again. Okay, health meter pen 159 same problem 159 x offset Y offset And run it You know, just tell us where now watch what I do here. I'm gonna move and did you see that see how the players moved? away from me, you know, it's weird because I Think I got this working right Because the border is not moving. It's looks a little weird. So let's go ahead and fix the border Where is the border rendering that game render board there we go So again, I'm gonna have to put x offset Y offset and And So left So I'm gonna go ahead and put left and right are both going to be minus x offset minus Yeah, it's minus because we're moving in the opposite direction of the Player and top is gonna be minus y offset And minus y offset Let's go ahead and try that see if that works Fudge Yeah, game that render border pen for 07 This is something that like because of the way we're doing it We can actually have it set up so that it follows an enemy or we have it follow the missile when it fires It's that's what makes it kind of cool, but it makes more sense. Of course to have it follow the player So this is gonna be camera x and camera y Let's try that again Okay, so I want to move and notice how the border is moving and Then I bounce off the border Okay, because again, if you remember that the point of this game was to have a larger arena than the size of the screen Okay, oops So you see how when I bounce everything bounces relative to the player that gives it a bit more of an interesting I think feel to the game and Again, the fit you start to feel the physics a little bit more of how that's working And yeah, so that's it. So I want to keep it at this size for now, I guess but we could well It's just just for fun. Let's go ahead and just make it big to see what that looks like and where did I do that at? Yeah, so we said 700 by 500. So let's go ahead and make that 1400 by 1,000 And if I go ahead and run that Now we can't see the border because it's off-screen, but if I go this way And I get to the border and now you can see how the playing field is now bigger Than the display field, which is which is a pretty cool effect And it only took just a real little bit of code to do that There's just a simple offset You notice how this is still staying in the exact middle of the screen for display purposes But it's moving within the larger game world, which is which is pretty funky. Okay, so I'm gonna go ahead and put that back to 700 Just for testing until we get that sorted out. We get everything else sorted out. Just leave it like that. Okay, so quick review We have a camera class That is initially set to the location of the well wherever we want the camera to be we could set it elsewhere And in the update method what we're doing is we're just putting it wherever the player is So the camera will follow the player later We'll have the camera trail the player it gets a little bit more elastic which is pretty cool And that should be 500 s and then we created the camera again set it to the players X and Y coordinates and when we update everything oh We render everything we have to send the camera X and Y And that includes the border and that is subtracted from the normal coordinates and that gives us the relative Coordinates and then again, we have to update the camera with the players X and Y Coordinates there as well. Okay, stay tuned for more keep on coding