 Okay, welcome to space arena ultimate python turtle graphics game tutorial part 3 in this part I'm going to talk about how to make the sprites move and how to up use the update method which we're going to add to our Sprite class And so basically when you're playing a game or when you're programming a game I should say is probably better You're gonna be doing you're gonna have something called a main loop Go ahead and put that in here main loop And usually what we do is we do something like while true Or we could have make a variable called while running and we'll probably add that later But for now, let's just use while true and so here's the basic structure of our while true loop So I'm gonna go ahead and put clear the screen and So to do that I want to do pen dot clear again This is specific to the Python turtle module if you were using pygame You'd have to do something different, but the principle is basically the same and then in Here I'm gonna do my I'm gonna call it say do game stuff and I sure will help us to put it do game stuff and Then finally, I'm gonna actually do I'm actually gonna update the screen and to do that you use win dot update Oops, and I almost forgot to also you also have to do something up here called win dot tracer Zero and what that does is it shuts off the screen updates Until you actually decide that you want it updated. This is what's gonna speed up your program a lot So just trust me on that one. Now you get now if you get a question Let's just trace or do try just commenting it This is this is a good practice comment that out and see what happens and see if it makes any changes for you It might it might not but it probably will okay, so Now you notice this is a while true so it means this is going to run forever So this point I don't need this code You know now if you're using something like pie charm and you don't delete that you're gonna get an error It says unreachable code because you can never actually get to that line So it's good. Yeah. Goodbye. Good riddance. You could comment it out if you want to keep it So in here, this is where we want our game to actually do some stuff so the first thing we want to happen is We want to actually render all of our sprites Okay, so I'm gonna go ahead and put render sprites Because now if I run this watch what happens You don't see anything and that's because what happened is we rendered the sprite here and then as soon as we got here We cleared it. So we've got to put this part in the main loop So you can see how now we're separating out the creation part of it from the rendering and the activity part of it or the running part of it Okay, so we've created some sprites here and now down here. We're gonna render them each time through the loops Let's go ahead and run that make sure it works Okay, and there's our there are our sprites now This loop is running a certain number of times per second Okay, I don't know what that that number is again if you're using something like pie game You can actually just set that in pie game pie game takes care of all that for you Which is great One of the reasons I like to use the turtle module is because you can learn the real kind of a bit more low-level type stuff in game design where the The module or the library isn't doing all of the work for you. It's only doing some of it So I think this is a good learning experience, but once you get more experience You just want to go use an engine it didn't make your life a lot a lot easier. Okay So the next thing I'm gonna do is we don't want our stuff to just sit there We want it to actually move around so what I'm gonna do is I'm gonna create a new method of my sprite class and I'm gonna call it update Okay, and again You got to use self here because I want to update the player By itself. I want to update the enemy by itself and update the power up by itself But note what I'm doing so far is that they all have the same exact Methods there's no difference between the way they function yet, but we'll get to that later in a future video So how I'm going to move stuff is I want to create something called DX I'm gonna set it equal to zero and I use dy and set that equal to zero and D is just like I don't know if it's scientific terminology, but means Delta X and Delta Y Delta means change It's like it's from the Greek letters So if your Delta X and your Delta Y are zero, you're not gonna go anywhere You're gonna be sitting there exactly as you are okay, so What I need to do is in here and say self dot X plus equals self dot DX Self dot Y plus equals self dot dy So this is my Horizontal left and right speed. This is my vertical speed up and down So if DX is positive, I'm gonna move to the right if DX is negative. I'll move to the left Okay, if dy is positive I move up and if dy is negative I move down So what I'll do here is I'm gonna go ahead with the player. I'm gonna say player dot DX equals one I'll go ahead and say enemy dot DX equals negative one and I'll go ahead and make the power up power up dot D1 to dy equals plus equals plus one Now what I need to do But you're gonna run it and you'll see that nothing's gonna happen Okay, because I haven't called the update method Yeah, I have to call that down here. So I'm gonna go ahead and make this update Sprints, so I say player dot update and Notice here. I just have self Okay, so I don't have to put anything in here self comes from here for player. I'll say enemy dot update I'm gonna do power up Update, okay, let's go ahead and run that and let's see how they move. Okay, so you can see how DX it's gonna go off the screen And bye-bye, okay now we can of course there's no reason why we couldn't have player dot dy equals 0.5 and enemy dot dy equals negative 0.3 You can have and literally any number you want there power up dot DX equals 0.1 just make it move just a little bit Okay, so this is how we're gonna get our our objects to move around the screen So you notice we have right now a main game loop that is running It updates each sprite And then it renders each sprite So notice that the updating and the rendering are separate actions Okay, this is a really hard concept for beginners to wrap their heads around Updating it At this point, you know, the sprite is just a mathematical object It has numbers that represent its state. So it's color. It's XY coordinate. It's DX. It's dy They represent the current state of the object and until we render it We don't have to render it. We only render it so human beings can see it The rendering is a separate process Now you can see here how my my code is gonna start getting longer Let's say I have 10 power-ups and 27 enemies. I don't want to do it this way So let's go ahead and just shorten this up a little bit. So I'm gonna make a list sprites list and I'm gonna say sprites equals That's I'm gonna make an empty list Excuse me, and I'm gonna say sprites dot append Player sprites dot append enemy. I see what I did there And I'm gonna say sprites dot append Power up and then down here what I'll do I'm gonna say for sprites in sprites Sprite dot update now again, this is not a beginner tutorial So I expect that you understand what this structure does and how it works if you don't You shouldn't be watching this video yet Go and watch one of my other tutorials. They explained it very well for sprites in sprites I'm gonna say sprites dot render Okay, so that'll that'll iterate through that entire loop and it will render every sprite that is in it Now some of you are probably saying well, couldn't I just combine these two processes? And the answer is yes, you could but for now, let's just keep them separate I think you'll find that might help you later. We might need to make some changes Okay, so you can see how it's still working exactly as it was working before Okay, so yeah, I think that's about it for this particular one So what we did was we added the update method to the sprite class We added dx and dy to control the x speed and the y speed The update method just adds dx and dy to the the x and the y coordinates And then we also created a little sprites list so we can keep our code just a little bit shorter and cleaner And that is that stay tuned for more