 Part 11. Now this part is actually being recorded in April of 2020. The original Space Invaders series was programmed in 2015. It was posted in November so it's been almost five years and even to this day this is still my most popular tutorial. If I had known it was gonna be so popular I would have done a better job with it. So what I want to do is kind of update it for Python 3. Just do some basic updates based on a lot of the questions I've gotten over the last five years. So let me show you what I have so far. I'm gonna run this and this is what it looks like. Okay so we've got our player at the bottom. I have to hit the arrow key each time to move which is not true to the original. The Space Invaders are kind of all kind of randomly if I push a space bar. Okay it shoots which is what we want to happen. This is working fine. You can see how the invaders kind of reappear on the screen. It was really just a demo. It was one of the first things I'd ever really done with the Python Turtle module. Yeah again if I'd known more or I'd known better or I'd known that this was gonna take off so much I would have done a much better job. It has almost a half a million views right now. Just this part of the tutorial and the next one has a couple hundred thousand. Crazy popular and so first let me apologize for you know not finishing it and not doing a better job with it. So what I want to do today is with this one is to yeah move on. Do a little few updates based on some things I've learned since then and kind of get it looking a little better. So why don't we get started. So what I want to work on today is the Python 2 to Python 3. Now on my computer it works perfectly fine. Python 2, Python 3 it doesn't matter. It is actually compatible with both versions but I will show some of the changes. There's another little thing that I'll show you a little formatting thing. It's just now it drives me nuts but I didn't know back then. I didn't know any better. And what we'll do is we are gonna change the movement of the player so that it moves more like space invaders. You push the right arrow it just keeps moving. Okay so let's get started. The first thing I want to talk about is this. The kind of preferred formatting for a comment is hashtag space. You don't need it but it drives me nuts now that I look at it that I've been coding for much longer in Python. So I'm just gonna go ahead now I'm using something called BB edit. So your editor whatever you're using will have this function as well. Now this is something it's Mac only. I don't actually use this anymore but since I use it in the original tutorials I thought I'd use it in this tutorial as well. So I'm just gonna go to where is it search. I'm gonna click find and that's it'll be a little different on yours possibly for not using BB edit. And I'm gonna search for hashtags and I'm replacing with hashtag and a space and I'm gonna click the replace all button. Okay so you can see here 34 occurrences of hashtag were replaced with hashtag space. So I'm gonna go ahead and close that. Now I've made a change and this is something I always tell my students you made a change and 34 lines were changed. Let's make sure we didn't screw anything up. So I'm gonna go ahead and run it again and you can see it's still running you know exactly as we expected oops I missed that guy exactly as we expected before. Okay so next up is Python 2 to Python 3. Again this works fine on my computer in Python 2 and Python 3. What I found from viewers is that this part here where I register the shapes has been causing problems particularly on pie charm. It's giving them warnings and saying this is not working blah blah blah you know it does work just fine. So the reasoning is that from Python 2 to Python 3 there was a change in the turtle module and these certain methods that were part of the module itself originally are now part of the screen object. Okay so I use WN here I should use screen now that you know looking back but it's too much to change. So when we register the shapes we should use the window object the WN in this case and register the shapes then. We should also down here for the keyboard bindings same thing WN and WN WN WN. Also I should note that in Python 3 it is now on key press even though on key still works but it is on key press. Okay so so I made a bunch of changes I'm gonna test it hopefully it will work the way it did. Okay so so far so good so again there's there's really no change in functionality but it is updated a little bit more Python Python 3 style it does get stuck there sometimes I never really figured that out but you know that's that's life. One more Python 3 thing actually couple more Python 3 things one's very simple it's way down here at the bottom I had put this in originally in the code before I knew about win.main loop I didn't know anything about that method five years ago now I do you could put win.main loop but if you notice because there's a while true loop here that runs forever it's an infinite loop this actually is never reached so you can just cut that out and yeah you don't really need it anymore so again I'm gonna test it make sure just in case okay so everything's still working oops missed that guy I do want to like got me on that one yeah I'm very terrible at my own game okay so got one die anyway and the last Python 3 thing I want to show you is with the format method this was added I forget when so we have this here this score string and this again it still works there's nothing wrong with doing it this way but it's kind of you know I'll pass a for lack of a better term so what I need to do now instead is I'm going to put here I'm gonna put some curly braces I'm use dot format and I'm gonna put score there okay and I think there's another place in the code I'm going to copy that there's another place in the code where we have that where is it just kind of scrolling down here missed it didn't I either score string equals and again this is the current well there's actually now a what there's now f strings or something like that but this is a bit more of an updated thing so this might even be going on the way out I'm not 100% sure on that one and by the way I am running Python 3.8 so we run that and again just making sure nothing changed okay so score is there and the score is updating so no errors we are happy also something I've noticed people always ask me about this when they close it and then you get this error there I think there's a way around that I'll put that into a future video because I'm not sure how to do that but it's not it's not a big deal it's you know the program still running don't lose any sleep over it yeah I get a million questions about that and the program's over it's okay so the last thing I want to do so that was the Python 3 stuff the last thing I want to do is to change the way the player moves like when I first made this again I did that stupid thing where you have to push the arrow key each time and I don't want it to work that way so what I'm going to do is here where we got create the player turtle we've got this thing player speed and again I didn't know any better back then but now I do so what I'm gonna do is I'm gonna change this to player dot speed and I'm actually gonna change that to well even I'll leave it 15 now for that what I want to do now watch what I do here is when I push the left arrow okay I want to change the player speed to let's use minus 15 okay when I push the right arrow I'm gonna change player dot speed to 15 so that's positive player speed is negative if you're moving left and now what I need to do is make a new method called move call it move player and define that tab over and back then also I was using tabs now I use spaces but since it was already in tabs I'm gonna keep it as a tab so what I need to do is I'm gonna cut this out of here and I'm gonna put it here now watch this carefully because if you don't you're gonna get an error I'm gonna get rid of it I'm gonna cut that out also actually I'm just gonna cut this part out here the border checking and again this is you know you should have gone through the whole tutorial first before you got to this one this is this is in addition to a 10 part tutorial so if you're just tuning in now go back to number one so move player so here's what we want to do is we got to change that to plus so what will happen is when we push the left arrow it changes the player speed to minus 15 when we push the right arrow it changes it to plus 15 and then what we want to do is we want to move the player every single time okay so we take the x coordinate and add if it's right plus 15 if it's left it's gonna end up being minus 15 and then setting the x but what we got to do different in this one is we need to put here move player and we could put this at the end of this but I just put it there for now so I'm gonna hit I'm gonna run it and see what happens okay so now I've got an error okay so again this is the other thing with people that watch my channel is they say I've got an error or my say my program is not working okay I got an error well what error do you have there could be a hundred different errors okay so it is telling you where the problem is in this case line 104 it's telling you what the problem is player speed is not defined if you recall I just changed it to player dot speed so that's line 104 so I'm gonna go 104 and there we go so it's player dot speed so we go ahead and run it again okay it's running okay so you can see how it went all the way over and push the left arrow okay right arrow left arrow so it's moving a little bit more like space invaders did back in the day the only thing I didn't like was when the game started it was already moving I don't remember if the original game was like that it may very well be but what I want to do is I'm gonna set the player speed to zero when the game starts okay now I'm gonna run it and this is what I'll see okay so so I can go ahead and push all right oops missed it okay so again it's a little bit more you know accurate to the way the original game was played so that is that for this particular video so this was part 11 we changed some things from Python 2 to Python 3 we added a bit of an updated movement so just a quick review we changed turtle to WN for register shape we changed turtle to WN for the keyboard bindings as well we changed the see what else the score string we use the format method rather than the percent method again they both work but this is a bit more updated and there's other things you can do with the format method and then we changed the player speed variable and we made it actually a part of the player object if you don't know what that means that's okay just a quick thing this player speed is a method this player speed is a variable so this just changes the built-in animation speed it's kind of hard to explain but just you know you need to put that where it runs really really slowly and then this basically gets rid of this whole method is no longer accessible so be careful here you're calling the method and here you are making a variable so this has to come first and then you can do this so I think that's it for this one so stay tuned and what we're gonna do is we're gonna add a bunch of aliens and make it look a bit more like the old-school space invaders in video 12 thanks for tuning in there's yeah I'll talk to you later bye bye