 Okay, everyone welcome to game porting in this video. I'm going to take the platformer kind of intro video I just did in Pi game and attempt to port it over using the turtle module and see what happens I've never done this before, but I think it should be pretty straightforward and this was a request I wasn't gonna do it, but then it kind of got in my head. I was like, let's give it a shot and see what happens So let's see what let's see where the the coding takes us before I get to that just a quick shout out to my channel members Thank you so much for supporting the channel is greatly greatly appreciated If anybody else would like to join click join down below also subscribe and click thumbs up So let's take a look at what we have now if you haven't seen the original Pi game version of this This is it. So I'm gonna go ahead and run it and show you what it looks like or not And it is not running because Or is it running and I just don't have it open nope See here it was running a few minutes ago, of course now it is not And close that it's kind of embarrassing and I should probably just like restart the video, but that's just not my style There we go. Okay, so this is what it looks like and You can see I got the basic jumping mechanic and everything going so These are simply rectangles that I'm drawing with Pi game and so I'm rendering it with Pi game, of course the window is created by Pi game The title is created by Pi game. So everything that you see visually is from Pi game also the input So I'm using the arrow keys and the spacebar to move around all of that is coming from Pi game So that's the part that I'm gonna have to update. However, the rest of the code I Use my own kind of custom Classes so I made my own sprite class rather than using the built-in sprite class from the Pi game module So that's gonna help make it a little bit easier to port over So there's some stuff here. I have to say it's a change and some things I don't so let's just go ahead and get started So since we're gonna no longer be using the Pi game module. I'm gonna go ahead and Import turtle instead and I'm not gonna really be able to do a lot until I get rid of all the Pi game code So Let's just go through it line by line so with Pi game you have to initialize it, but in Turtle you don't have to do that But however the turtle you do have to set up a turtle screen I usually use WN So I'll stick to that because I saw it in a tutorial about five years ago So I haven't always been doing it that way I if I was thinking about it now I probably changed the SCR because it makes a little more sense because it's turtle screen the caption In pie in turtle would be win dot title and One of the things that Pi game does that the turtle module does not is that includes a clock It's very very handy to have the clock. We're gonna have some trouble later when we Run the program and we see that you know the clock isn't there to keep things consistent I've tried a zillion different ways to get a working clock with the turtle module and it just never works so if anybody has a good solution, let me know and Just real quick. I do have the turtle graphics Documents open over here. So just in case we need to refer to that. Okay, so we need to do a width and height So we need to set up the window size. So I think that's win dot set up and I'm just gonna go width and Height, but I'm not a hundred percent sure if that's correct. We'll find out later Now Pi game already has built-in colors So I don't need to use these, but I think I can use them So I'm gonna leave them there and see what happens. Okay, so I'm just gonna change everything that I know I need to change and then just start fixing bugs now one thing If you watch the other video, I probably I talked about a lot is that the Pi game window coordinates are very different to the turtle module window coordinates So the Pi game coordinates start in the top left at zero zero X is positive to the right X is negative to the left Y is positive going down Y is negative going up. However, the turtle module is more probably what you're used to Zero zero is in the center. So Y is positive going up negative going down But X is the same X is positive going right and negative going left So gravity is gonna have to be negative or we're gonna have to subtract gravity rather than adding to it So now you see here. We created create the screen now. We've already done that So I could have maybe brought this code down below and when we go ahead and do that just heck we just bring that down here put that in the same spot and What else we gotta do we gotta do set up a night so X and And I'm gonna get rid of this because I don't need it twice And just out of curiosity, let's go ahead and run it and see what happens. We'll probably get some errors, but Okay, well something happened. That was nice. Okay. We see what says Pi game is not defined So since we didn't use Pi game, let's pop Pi game it back in there and see what happens import Pi game Just kind of curious Again, I haven't really done this before So, okay, so stuff's not initialized. So yeah, we're not gonna be able to do anything until we get most of the code taken out Okay, so let's just keep going down through now the sprite class. I made again. It's not the one from Pi game So I don't have to really update this except for rendering Okay, so rendering in the turtle module Requires us to have some sort of pen So I'm gonna go ahead and create a Turtle pen now there's other ways we could do this we create a render object wall block But I'm just gonna do this this way because this is kind of how I've done it in other videos So pen equals turtle turtle Pen dot speed is zero. I Say this a million times and nobody gets this listen to me carefully. This is not the speed that it moves It's the it's the rate that it's drawn and the way the frames are drawn So don't do anything else with us. Just trust me on this one I'm gonna say pen dot pen up Can you tell I've answered that question million times and pen dot color? I'll make the default color I want to try white. I think that I can use this I've never tried it before if not I can use the built-in white for that And we don't want to see the pen age. So I say pen dot hide Turtle again the turtle module wasn't really created for making games, but lo and behold It's pretty awesome for making games. So who knew and so what I'm gonna do is I'm gonna go down here I'm gonna take a look at this render method and What it does it draws a rectangle on the screen Okay, and it's got a certain width. It's got a certain height now I'm trying to think of how we can do this today. I'm gonna try a different way of doing this. I'm gonna say It's gonna be a little bit similar. So I'm gonna say pen dot pen up I'm gonna say pen dot go to See sprites dot x minus sprites Dot width and I'll explain what I'm doing it why I'm doing it this way in a second. You'll see Can go to sprite zero zero and sprite dot y Now this is reversed to the It's reversed because we're gonna be top left. So basically what I'm gonna do is I'm gonna draw a square on the screen Okay, so sprite y plus sprite Dot height divided by 2.0, this is a little different to how I've done it in previous tutorials But usually I use the shape size, but this time I actually just want to draw the shapes And so I'm gonna start in the top left and go over to the top right. So that means plus for that one and then I'm gonna go to the bottom right. So that's plus X and it's gonna be Minus y and then I want to come back to the bottom left So that's gonna be minus x and Minus y and then I need to go back to the beginning. So I'm just gonna copy that And did I get the whole thing? No, control Z That's what we wanted And then I'm gonna go pen dot pen I did miss something here So we got to go to the start and then put the pen dot pen down So yeah, and this isn't really an explanation of how the turtle module works I assume that you already know how it works or at least have some idea and that this will make sense to you Yeah, I don't need to change this because it's not using any of the pie game Stuff we'll say a little bit that Again not using pie game now gravity is gonna be reversed as I mentioned earlier We'll come back to that once we get it running Okay, now here's another big difference. Um pie game has this Event get thing for you. Um, so all this keyboard stuff You know works this way so in Turtle you gotta actually use some functions And you have to use keyboard bindings and stuff like that. So let's do keyboard binding And So what what we got here? We got left. So I'm gonna say def player left And pass I'm gonna do def player right And I'm gonna pass on that oops And we also have player jump. So def player jump Now I could put this into the player class, which is probably what I should do Uh, and maybe we'll we'll change that in a bit. We'll see what happens Win dot listen So we have to listen for keyboard presses And then what we do Is win dot on key press Think it's going to be like this And I'm using the left arrow. So I'm going to type left and cap with first letter capitalize So win dot on key press Player right Right And then win dot on key press Player underscore jump Now I'm going to say this again Not sure if it's a capital s or a small s. We'll get an error later Notice I did not put parentheses here. Okay, again, I get this question constantly I don't know why it's this way. You just don't put parentheses here And I don't put anything in here either. There's a way around that but we'll leave it like this for now Okay, so now that means I can get rid of this because that's from the pie game thing I'm going to unindent a little bit here Okay, and basically All of this stuff is oh, I already have a player left methods. All right, let's go ahead and do that Um, sorry, I forgot about that player dot left player dot right And now this one's a little different because I have more than one piece of code So what I'm going to do is I'm actually going to stick with This player jump and maybe we'll put that up into the Class later, but maybe I'll just leave it like this. We'll see again. This is kind of an experiment I don't even know if this is going to work. Um, I'm hoping it's going to work But that is how we learn new things Okay, and player dot jump Okay, I'm Feeling good about this. I'm feeling good. Okay. We don't have too much else to change Um, that's kind of the interesting thing to me Um Again, all of this stuff here Has nothing to do with the pie game module Okay, so this is a lesson on you know abstraction and using your code Uh, properly, so if you have a separation between, you know, how are you using things? A lot of your code is reusable Okay, now it says render draw stuff fill the background color now in pie game You have to do this every time in the main loop. However For the turtle module, we don't need to do that. We would just do up here win dot bg color And I'm going to use this Tuple the black tuple. I again, I think it's going to work. If not, we'll fix that later Oh, I missed something there, but I'll leave it for now Um, so I don't need to fill in the background color Okay, in pie game, you need to do that in the turtle module. It just does it for you Um player render. I don't need to change that anymore because that's we already fixed that up above block render I don't need to do anything because we fixed that up above I don't need to flip the display, but I'm going to do something a little bit different We do win dot update And the companion code to this Is up here. I would do win dot Tracer zero Now if you mess this up Okay, if you don't put win tracer zero or if you put win tracer zero But you don't put win dot update your computer or your editor will probably lock up You'll have to restart that program as I mentioned earlier the frames per second does not Work in the turtle module. It doesn't have a feature for that. That's one of the great things about pie game So no matter what system you use it on it will keep you at a consistent frame rate Okay, so as far as I know We've gotten rid of all of the pie game code We've translated everything As far as I know into Turtle coordinates and such Oh, okay, I'm gonna run it and just see what happens and this is again. This is the process Okay, so you saw it flashed on we got an error Okay, so it says bad color sequence 255 255 Okay, I'm just gonna search over here for rgb Gb Um Eight, okay Pen dot color that's what we did. It's a color mode Um, ah, okay So Okay, so we have to use this color mode Command. All right, so let's go ahead and do that. So Again, we're trying to make this as you know compatible with The pie game module. So we have to change the color mode To 255 and if I'm reading this correctly what that'll do is let us use 255 255 255 Instead of 111. So that's the other option for that. Let's see what happens Okay, that's good So that we no longer got an error the original error now. We got a second error Bad event type or key symbol space. So as I mentioned earlier, I wasn't sure it was a capital s or a small s It's really annoying because left is definitely capital right is capital, but space is small. Uh, thanks for the consistency guys Okay, all right Going on ah duh. Okay says sprite is not defined. I wonder how many of you caught that earlier So up here I did sprite, but of course it's supposed to be self So i'm gonna copy self And now I gotta go through and put that way everywhere. I have sprite So again, this is object-oriented programming. So if you're inside of a class You need to use self That is a rookie mistake. I apologize But I'm feeling pretty good about this port Again porting is just the process of You know translating a program so that it works in another environment in this case. It's working with another Uh, you know module. Okay, this is looking good. Um Okay, stuff's happening. Okay, this is interesting All right, I'm I'm pretty okay. This this is good. This is good. I'm happy with this Um Ah, we also I also forgot to do Pen color So pen dot pen color Is self dot color I forgot to set the color Okay, so you can see the program's running. Now you can see stuff's off the screen And there is my uh player Again, we'll fix that in a second Or eventually Now you can also see things are upside down Okay, because as I mentioned earlier Um The turtle module uses a different coordinate system. Okay, so In the other coordinate system, it was zero to 1200 left to right. So 600 was the center So I'm going to switch 600 to zero Because that is the center of the screen zero zero now 1,000 would be Uh 200 from the right So that would be 400 And I think 200 is going to be the same. Let's go ahead and see if that fixes some of it Okay, so things are now centered Now this is we got to deal with a y coordinate So 200 is 200 from the top Which is going to be 400 minus 200 Well, that's the same Uh 400 should be zero 600 should be minus 200 And 500 should be minus 300, I think And these are the widths and heights, so they're not going to change I think Okay, almost got it. Um Okay, so these three platforms are fine But these two big thingies are off. So what did I do wrong there? Um Oh, okay, so 400 Okay Those are the big ones So 200 Actually, I should I want to undo that and see what the original values were Um, actually that part was fine is the x is the okay. I'm gonna redo it. Um, I'll just figure it out Okay, so let me go ahead and run that one more time And so this is zero zero is about here So this needs to come over to negative something Okay, so 200 that would be Okay, I got it. So that would be negative Let's try I don't think this is right, but let's try it That's got to close the old one Oh, sorry. Yeah, you couldn't see the screen there. It's it's on my screen So I want these to come up and over a little bit So I'm gonna make these Uh 400 and minus 400. Yeah, I'm just kind of playing around with the numbers I should I could have calculated it, but I didn't Okay, so they're they're about where I want them You can see the the player is still pretty far off and I want this to come up a little bit. So The y is off. So this should be minus Well, let's try minus Okay, it's almost right. Uh, again, it's I have two screens So I wish it I wish I could get it to stay on the same screen, but I can't Oh, let's see minus 100 100 And I am happy with that. Um, it's Pretty close. Um, I could move it up a little bit. Um, if these were solid, you wouldn't notice it Now you can see how the player is over here And it has fallen because gravity needs to be reversed and we need to start the player at Uh, we'll say zero And we'll make 400 And so we could just probably the easiest way to fix this Is to make gravity negative. So let's go ahead and do that Um We'll make it negative one Okay, so let's go ahead Okay, space isn't working. We'll we'll get back to the second. So you see how it's kind of working We're gonna we got a couple little artifacts there that things aren't doing what I want them to do And that's partly a result of You know switching the coordinates systems So you notice how It's leaving a trail Okay, so what we have to do Is once we update We're going to clear the screen Okay, now we won't see the next thing until everything is drawn. So this actually draws the display So I'm just going to do pen dot clear And that's a turtle command Okay, so now okay now you can see how fast it's moving Space doesn't work. I know why I'll fix that in a bit But this is what I was talking about earlier The numbers are very different In the turtle module depending on your system I'm writing this on linux right now and linux is the fastest of of the out of mac and windows And uh, I have a whole video about that actually and um So I'm gonna make this a much smaller number. I'm gonna say minus 0.1 Okay, uh, let's go ahead and run that And bring that up It's Well, that's left and right. So I'll leave gravity the way it is Um left and right have to be changed too right now. I'm using negative Jump I'm using oh, okay jumps got to be positive Um That's probably why it wasn't jump possibly why it wasn't jumping I'm gonna just change everything's like really smaller much smaller numbers And I'm gonna try one And uh, I shouldn't have hard coded this that was a programming error on my part But let's for now. Let's leave it the way it is All right. All right. Let's go ahead and run that again. Sorry. I'll bring it up onto your screen Okay, so I'm gonna go left a little bit still a little fast friction's not working very well Um stopping which is fine, which is good And I still can't jump the reason I can't jump I think Is again, if you watch the other video you really should Is I had to add this little one to make jumping work But since the coordinates are backwards, I gotta change that to negative. Okay um, I think Okay, it's still not jumping but All right. Well one problem at a time Okay, so I'm gonna go up to jump jump Player jump Okay, that's underscore. That's a good sign All right. You want to leave it at that for now? Um Well, anyway, just real quick, uh It's basically ported. Um the graphics are working um You know, it wasn't super duper diff super duper difficult to do that Uh, I think the gravity's still a little bit high, but uh, that's a different problem. Um Let's see. Let's say make gravity zero point zero one And again, you'll have to play around on your system to find like the right number For your particular system That that looks right Um, that's that's one of the limitations of the turtle module. You can't do that Oh, it's not coming back to the beginning because Uh, and again, you gotta you gotta test it. Uh, this should be zero and four, I think it was 400 Yeah, every time I'm making a change I'm testing it Uh, it didn't come back. All right. Thanks for nothing Uh, oh because the y coordinates are are reversed. So less than negative 400 There we go Okay, maybe we have to maybe we could increase the friction a little bit on this one But I still gotta figure out why jumping's not working. That's kind of annoying. Um I'm gonna change that number to like a lower number just to see what happens Um, I don't know if that's gonna matter, but Let's make that 12 Okay, so the jumping is not working. So what I want to do is I just want to test it Um, so I'm assuming that this part's working. So what I'll do is I'll go a print jump and then I'm gonna go ahead and print You know jump Two two So this will tell me which part of the code I've gotten to So watch down here. I want to press the space bar Okay jump so you can see how it says jump and the what's not happening is This collision is not registering Okay, so I got to figure that part out and that was actually one of the hard parts. I remember that now um so Okay Yeah, I got to reverse this Because the y coordinates. Yeah, this y coordinate thing is is a pain because I have this reversed So let me see if that'll work. Oh geez. Well, it kind of worked. I can't jump. That's going um Cricky, all right, so let's go ahead and think this through So if the player y is greater than block y that means it's below. Okay, so I reverse that that's good So player y is greater than block y means it's above Yeah so then block y Plus block height This player height And if it's lower than it He's got to be minuses Did I I don't know if I did that right or not, but let's try that Okay, that's a good sign. It did stop this time And It did jump. Okay. Awesome. That did fix it um Yeah, so what I had to do was basically reverse this part and reverse this part as well to deal with the different directions On the different y coordinates, but the jumping is just too much So you can see how it's jumping too high So I'm going to bring it really low down to two and again your computer may be different Okay, so now it's hardly moving at all. All right, so you can see I think the friction is causing a problem So I'm going to make less friction Uh, I'll try 95 Remember the higher the number the less friction at zero would be you can't move at all um, and I still think the D y we'll make that the one as well. See what happens And again, it's just something at this point where I just really got to kind of play with the numbers Okay, that's kind of a jump Okay, and I think the max speed is a little bit too high. So let's make that three Again, I'm just playing with the numbers You'll have to play with the numbers that work for your system again because the turtle module doesn't have a way to control that And I think we're going to make the jump a little bit higher because we need to jump from platform to platform But I think overall This port has been fairly successful Now I could draw these and make them Uh, you know solid if I wanted to I just don't don't feel like playing around with that part Okay, so it's kind of oops It's not quite enough. I'm gonna have to have to go ahead if I wanted to work like the other one I'll make this 2.5 Um, so we can get up there and hopefully jump from platform Now again, once if you start adding levels, you got to figure out the distances you want to do that's coming down And Okay, now that's looking like it again. It's still kind of I think doing a little bit too much with the Uh, I think the friction is still a little too high. I think at nine nine Yeah, that's a lot more like the original Okay, okay, and there you go. There is your Yeah, it's still probably a little too fast on the x y but I can live with that. Um, this is cool Okay, and there is your Turtle version of the platformer Um, so that was kind of you can see kind of the process of porting it from One module in this case pi game to the turtle And you basically as you saw I had to do you know a couple things I had to change, you know All the display code Um I was able to use this by using this color mode. That was kind of interesting. I hadn't done that before So just trying to Match what I had as closely as possible Again with the with the turtle module you need a a pen to draw with so I used a pen Then I rendered those blocks just by Uh, how can I put it by drawing them? You know all just lines on the corners um Yeah, and then I had to change the coordinates because the coordinate system is very different Um, I put this into a function. Let's get rid of that. We don't need this But again, I could put this I'm pretty sure I can put that into the player class But I'll leave it the way it is for now And uh, I had to do key binding instead window listen and wind up on keypress And Again, I had to reverse this because the y coordinates are reversed I had to reverse the signs here because the y coordinates are reversed Uh, I had to change this because the y coordinates are different I changed this because the x and y coordinates are different Um, and then yeah, there's a couple things I could take out I had to add this and I had to clear the screen. So this functioned similarly to how The pi game works. There's only one thing Uh, I'll just Search here, uh, phil Um, now there's a way to do this. Um, internal input So there's a begin phil. Okay, so begin Phil, there we go Okay, so I think what we do I'm going to try this and I think what this will do Uh, I'm going to say pen dot begin phil here And then once I've closed it up, I can do pen dot end phil I think I need to underscore And hopefully This gives us exactly except that wasn't green on the inside. Um And so I think I have to set the fill color as well. Um, I think I have to do pen color Dot Self dot color. So because that's the outline color and the inside color in this case Oops or not bad color arguments um What's it am I end up in color? Yeah, I just changed that self dot color Fill color, all right pen dot color Uh, and then we'll do pen dot fill color And we'll put that as self dot color Okay, these docs are very helpful. Let's see if that works Yes, oh my gosh You could not tell the difference between the two, uh The two versions. I mean it's just This was a little faster. I could probably set the the speed limit a little bit lower But it works almost exactly the same. Isn't that cool? Um, let me let me just open up the original real quick The pie game version So here's the pie game version You see it's a little slower just the way the numbers work out Uh, and now again, it's just because I chose certain numbers here I can make you just got to play with the numbers Um on your system, it might be a little faster or slower And then here is the oh, that's the pie game version And then let's take a look real quick at the turtle version And that one appears down there. Why is that? And I think other than like a little bit of speed difference Works exactly the same. So I'm going to put that in the Wind column and that was pretty cool. That was a lot of fun. Actually, uh, that's something I haven't really done before And uh, never really had a need to but uh, hopefully that'll you'll get the idea of the process Of porting from one module to the other Especially those of you who are more familiar with the turtle module and didn't want to do pie game Hopefully this will help you Yeah, uh with your turtle stuff. So anyway, thanks so much for watching again big shout out to members Click like subscribe or join below. Thanks so much. Keep on coding