Complete Python Turtle Graphics Overview! (From Beginner to Advanced)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys and welcome to my video on everything you need to know about the turtle graphics library of Python so to begin turtle graphics is basically this nice and easy to use tool that allows you to get the hang of the coding syntax while at the same time making these fun little animations in this video we'll start out by going over the basic most basic examples you can have in turtle so we'll do things like just drawing this line here then we'll move into more complex things like shapes and I'll also show you how to make these blue as you see then we'll move into pretty little flowers so something that looks like this and as we get even further we'll do some well show you how to you you can use these complex math equations to get graphs that look like this and you can see this one's still cranking out that animation and also this so this one I use like a square root function somewhere in there it's doing all sorts of funky stuff and then finally we'll end this video going over a more complex programming concept called recursion which allowed me to create this really cool star graph that you see here so as you can tell it's drawing little stars among big stars it's just there's so many stars everywhere and that's what we'll end this video on I'll put a video outline in the just in in the comments so if you're trying to find one of these specific examples go to the comments and you'll find it there all right you get started we'll want to open up a new sublime text window and then save a new python file and it doesn't matter what you name and just use that dot py file extension and whenever we start a turtle Python file we need to include the following at the top of it import turtle so this is just basically letting our Python file know that we're gonna be using the turtle library in it and we'll always want to end our turtle files with the the line dot done this just keeps our animation window open so that we can actually see what we've done and now that we've done that we're ready to create our first turtle super-fun Bob we're gonna say this is what we're gonna do follow me Bob equals turtle dot turtle I'm gonna save that of control this show B and yeah we got this this is super exciting just arrow on our screen all right and it's just the stationery okay so we haven't told it to do anything yet so I could go ahead and say something like this is the first command you'll want to know I can do bob dot forward here and I can type in a number like 100 and what this command is saying is it's saying we want to move the turtle Bob forward 100 pixels so this hundred represents the pixel amount and so if I run that we got that straight line cool they went a hundred pixels forward and also I just want to mention real quick I don't have to name this Bob I can name this you know Amazon Alexa sure why not I could name this after you know john cena why not but i'm gonna actually just name it just for simplicity's sake after myself Keith I'm the turtle Keith thought forward 100 and as you can see it still works okay so that's the most basic command forward in addition to forward we'll want to be able to turn our our turtle so we can do that with the function keith dot all that and I want to turn 45 degrees I want to be a little bit slanted so I'm going to type in 45 so this takes in an angle and degrees and then let's move myself forward again cool so we got that angle I showed in the beginning of the video and if you're wondering where I'm kind of look like how do I know these functions like how do I know that Ford works how do I know that left works I recommend you take a look at the official turtle documentation site so if you go to this link right here and don't worry about like kind of a zooming in and trying to figure out this link says I'll post this in a description but basically what this link has is everything every function you could want to know in turtle it's all documented here so like forward is documented here for or distant school but there's all sorts of cool things that I can't cover everything in this video so definitely check out this site alright and if we have forward we must have right as well so it is totally valid to write as well so you need to write 90 degrees and then go forward again after I run that you see that I went left 45 up forward 100 and then right 100 cool cool cool cool alright so now that we have that we're probably ready to move into the first shape we're gonna make some squares super super cool stuff alright so and if you want to keep this code here but still be able to build your squares with a blank slate just highlight all of this code and do control slash and it will comment at all this only works in sublime and some other editors so if it didn't work for you maybe try getting sublime alright so we're gonna make a square so I would say before I go ahead and just do it try to make a square in your own because you have all the building blocks you need with these commands so try to make your turtle do a square real quick pause it alright hopefully you figured it out if you didn't I'll go through it right now so we can do Keith dot forward 100 and that doesn't have to be a hundred pixels I could do 133.33 and it would still work can be any sort of value here if you make it too big of a value you'll go off the screen and if you make it too small you won't really see it like it's kind of hard to tell that I moved it all with one so we're gonna just stay at a hundred just simplicity's sake so move forward 100 and then I want to turn left 100 oh shoot I'm saying something in typing something else so I want to turn left 90 degrees so I get that right angle that's in a square so I do left 90 then I can do another forward because I want to move up now and I want to if it's a square I want to move up the same exact amount and so if I do that yeah we got that so I just need never repeat this process so left 90 degrees and then forward 100 and then left 90 more degrees because that's not going to be a full little square and then finally one last forward and will this give us a square moment of truth yay and also just worth noting right now one of the most common mistakes I see with people that are new to the Python turtle is that they'll spell one of these like names wrong they won't be consistent with their names they'll smell something else before wrong and if you're getting an error look to see what the error is in this window it also pops up right here and try to fix it so too many E's there if I run that now it works again if I typed in something wrong here like I just called this full word just so just be careful with your spelling it has to be expelled exactly right otherwise it doesn't know how to handle it and Python okay so we drew a square next let's give our square some color and to do that we want to start out by specifying the turtle color with the following command so I'm going to do Keith's dot color and then in quotations I'll type something like red as you can see that may red and so there's only a limited number of things you could type in here so the common colors will be accepted in quotations like orange will work we can also do blue that will work and we can do something like cyan that will even work that's probably like the as far as it goes is it Scion Qian I don't know but that also works as you can see but then you might be like Keith you know I want to do this certain shade of green how do I do that well you're in luck so if you go to there's plenty of sites that do this so I'm on this online color wheel site right here so now you can find these all over at Google but let's say we wanted that random shade of green so I could go here click on green and then like now take like this color so this will give us a hex value so this number down here is a hex value and I can actually copy that and then paste in hash tag which makes it a hex value this value and with we look at our turtle now it gives us that proper color we wanted cool so any color you kind of want you can do it with these hash tag values it also accepts RGB values that's a little bit more work but don't worry if you don't know what RGB values are but yeah you can do any color you want but I'm going to just stick to right now just doing the basic colors so like blue cool we have blue now we have a color outline colored how do we actually fill it in blue so that's the next question and if I looked at the documentation I would find two functions called begin fill and end fill so we need to surround whatever we want to be filled with these functions so I do Keith dot begin fill and then I end it with because after our last forward I do Keith thought and fill make sure you include the begin fill before and the infill after you you have both yeah we got blue and you still might be asking questions you know Keith a one blue with with cyan in the middle of it so we can do that just fine so go up to this Keith thought color function and then do comma so the first value will be the outline color and if we pass it in one more value that will be the fill color so if I pass it and say cyan here and then run it we finally got what we are looking for we got that blue with the sign in the middle and you can do this with any sort of animation you do in turtle so that's how you color things the next thing before we move into the more complex shapes is we'll want to do well let's say we wanted to make another square at a different location so I could go ahead I'm gonna stop filling for a second I could go ahead and after I'm done with this last forward I could move you know keep going in the downward direction that I was before I could just do forward 100 more so it goes through like that the one thing that's annoying about that is that if I wanted any spacing between the squares that I'm trying to draw so let's say I completely drew this square again I can't get any spacing with this this Florida it's all continuous so there's conveniently enough a pen up and pen down function that allows me to change my location without drawing the line so I could do Keith's dot pen up and then I could do Keith's thought forward 150 let's say and then Keith's dot pen down and as we can see now if I run that what the heck oops see I told you I made that ever the night I spelled it forward not force word so it jumped down there but it didn't draw the line so that's good and now if I copy this cool we got two squares just like we wanted and I can also fill these in begin fill and end to fill down here so I actually don't know if this will work we'll see yeah what the heck yeah once again I mean an error it's so easy to make errors make sure you read your error message oh it did work Wow sometimes if you did the the begin fill and then fill like around the whole thing you might have to actually break it up you can do that multiple fills and nothing so you might have to like move it here and then begin fill again right here and that will also work but that will just fill it first and then go to the second one and then fill that okay cool we're almost done with squares there's just one more thing I want to mention and that is that instead of doing left we can also manually just actually set the direction we want ahead so if we go to the documentation there's a function called set heading and you can use any of these values here you can use 0 to 360 it does the full circle and you can plug that in and actually mainly go up me and then go left because it sometimes gets annoying if you just want to head up but you're this weird angle like it would be annoying to have to calculate how many more degrees you have to turn to get straight up so set heading will do that for you alright let's go ahead and start that flower that I showed you at the start of the video so if you don't remember the flower that looked like yes and to do this I'm actually going to open a new file just because I want to fresh slate and I'll just say this is something like test2 dot py all right and so basically what we want to do for this is if we look at it one more time and I can even reanimate it it's already animate it real quick it's basically just moving forward then turning a certain angle then moving forward some more and the turning a certain angle so I mean all we really need to do is figure out what's a good angle we should turn and we just gotta keep running that code over and over again so to start off this file let's start out the same way import turtle and then turtle done we'll need at the end then we'll also need to just named our turtle I'm actually named this one Bob Bob equals turtle turtle and as we can see with this animation if it's still going that's done now open up another one we want to make it go forward and turn and go forward and turn and it's a pretty sharp turn so it's gonna be over 90 degrees something over 90 degrees so let's go ahead and implement that try to do this by yourself first and then you know pause the video right now and then I'll go over it in a sec okay this is the way that I would go about doing it so we know we want to go forward first so we'll say forward we're gonna say a 150 this time or maybe we'll say 200 why not we'll make it a little longer than before so 200 pixels and then I would know I want to turn more than 90 degrees but I don't know how much so I'm gonna try left you know 45 this think 90 plus 45 so 135 will try and we'll run that she we gotta go forward over time so Bob Ford will do that it again okay we're getting there so now we just need to keep copy and pasting this code oh my god let's see what happens now hey it looks like we got like a star we got to keep pop copy and pasting you might be saying right now Keith there's smarter ways to do this and copy and paste and you are very correct if you said that so we're gonna actually instead of copy and pasting all this time so we're gonna oh we got a little star this one worked out pretty nicely set a copy and paste thing what you can do is you can use either a for loop or a while loop if you've never heard of these you should check them out they're super convenient to use I can do something like for I in range 100 run these two commands so now what this is doing is it's going over a hundred loops and just running Bob for Bob left Bob for Bob left way easier than copy and pasting look at that nice nice start cool cool cool all right it's giving me an error right now just because I canceled the program before it was done okay so we got that we didn't get the star we're looking for when you're looking for a star that looked like this and I think the problem is we just don't have sharp enough angle and also this 135 comes back to the kind of place we want it pretty quickly so we should use something other than like a nice number like 135 and also let's not some color to this no color right now let's add red this is the red outline color and then if we want to fill it in yellow just like we have in the diagram we do red and then come a yellow so let's now run this this will be the different color and why did it fill in yellow try to figure out if you can find my mistake and that's because I didn't do begin fill and I didn't end it with end fill and it did a little weird thing and I think that's just because it didn't know exactly I cuz I over traced it didn't know exactly how to fill it in but we got the basics so what I said is that the angle was off we didn't have a sharp enough angle so let's try something like 160 and instead of running this ten times the chugging it a hundred times look at that go look at it go look at it go come on baby okay a hundred times just taking that long but it's still perfect I think 160 is still too nice of a number but we're getting cooler and cooler stars so said Oh 160 let's try something random let's try like 168 point five why not well you know look at this and also another useful thing is if we're running is a hundred times it's going really slow so we can also increase the speed I could do speed ten I think ten is the max and now we could run that and as you can see it goes a lot quicker than it did before well that looks pretty good it looks pretty similar to what we had in the example yeah and it fills in pretty nicely the and we could make it a little bit bigger if we wanted to eighty three hundred it's really fun just playing with us and seeing how you get these different designs as you go so just to speed things up the actual angle I used in the example was one seventy but this was the the code I had to write that example and yeah mess around with your for loop mess around with your fill but should look something like this from this flower could we can pretty easily go into those complex like crazy diagrams I should that use the math so I'll show you how to do that or a quick I just want to run this without errors real quick just so I have no error message popping up so now we want to try just playing around with some complex math functions so to do that we're gonna first have to import the math library just like that and then instead of doing forward what what I basically want to do is just play around with different math functions so one math function we could do is do like bath the square root of I why not so I is the placeholder for whatever is in range 10 so I'm just messing around you can do whatever you want just look up the math library in the math library in Python and just try to play around if these different functions and see what you can make and as you can see I'm taking the square root of I and it only goes up to 100 so it's not creating that big of a thing because hundreds of Max values so the square root of that is at max I'm moving forward ten pixels so maybe we try multiplying this all by 10 that's giving us something a little bit more interesting after that it's pretty cool it's like a little compass just like shooting out at us fun stuff you also could like be creative with how our angle goes you could do like once let me think we could do I mod one layer mod like 90 so what this does is now it allows us to turn left up to 90 degrees and then once we hit the 90 first iteration so I equals 91 this because it's 91 mod 90 it'll become one again so let's see how that works it's like a circle whoa that's pretty cool and then shout-out elsewhere and the fill is kind of weird right now just take out the fill and so actually this is pretty close to what I had for that square root function that I showed the start of the video I did math dot square root of I and instead of mod 98 and mod 180 I believe I bought it 180 so and I also made my iterations a lot bigger so I did something like 2000 I saw how that worked so it makes these weird circles cool cool cool and then you'll see that it shoots out of that once it becomes once it hits the one 480 first and makes a little angle really small makes the angle one right cool so we did that one and these are in super porn I'm just showing you what you can play with really right now oh my god I need to close some windows huh okay so now let's do the sinusoidal function that I did I'm just going to do this real quick because this video is already getting fairly long but I'm just trying another another function from the math library I'm going to do math the sine of I divided by ten times 25 and then I'm gonna have left 20 and I'm gonna make this Bob forward 10 and this section beat Bob left and literally don't don't worry if you don't know what math up sign is if you don't know what Bob does or if like why I'm plugging these values in I'm just making fun designs I'm just having fun with it see what that does so that makes like a circle and weaves off makes another circle we use back in makes a circle and you know that's just something using the sinusoidal chain so just play around with these functions see what you can do you could also use the random library that's another fun one to look up look these up on Google just just play around try to build your skills get the hang of this coding syntax I recommend also if you make something cool and you want to show me feel free to tweet me at a Keith underscore gala to pop up right on the screen I'd love to see what you guys are making alright finally let's end this video with that that star drawing so that the stars on stars on stars don't you see here so to do this let's begin by just saving another new file so a new file save as and doesn't matter what you call this I guess I guess it's good to be descriptive with your your name so I'm gonna just call this stars py alright and as we did before you know for turtle and we need to do turtle done at the end and what should we name this one it's outer space stars so I'm like I don't know I'm just trying to think of something that like fits some reason Pegasus is pop into my head and you know it doesn't matter what we use for these names so Pegasus is going to be our new turtle so turtle dot turtle yay let's go Pegasus all right cool we have that blank screen again first thing I noticed that's different about this diagram is it has that red background color so to figure out how to do that we can go into the documentation and I would look up just like something like I'm trying to change the background color and if I didn't remember first thing I would look up is just background and as you can see the first thing that comes up is BG color so screen dot BG color so how do I access the screen now so actually maybe we can do this directly on on Pegasus so let's do Pegasus dot BG color and we'll do red and it's not letting me do that and it's because it has to be on a screen so now if I go to and let's see what that error message says turtle has an object attribute PG color so I need to do this on a screen so now I'm gonna look up screen I'll shoot it pops up everywhere so now I'm gonna look up just like get screen and perfectly it pops up click on that see how to use that just do turtle dot get screen so now I can do Pegasus dot yet screen dot bgcolor dot red yeah oh my god that blinds my eyes so I want a nicer a little bit more of a subtle red so I'm just going to copy and paste in the hex value that I used in this drawing that's a lot easier on my eyes okay cool we got that and now I would say the the next thing we need to do is how do we how do we and also how do we make a star to begin with so I mean it's pretty similar to that flower we just drew you know you're going forward then turning a certain amount of angle then going forward so try to think about the angles you'll need and I'm thinking about like a Pentagon right now because that also has five points just like a star so I'm trying to think of like how a pentagon wouldn't work you know how how do I add these in my head it's gonna take a little bit of messing around and playing with to get it right so I'll go ahead and actually just relate how to draw a star try to draw a star and come back right now if you can't figure it out or figure it out to see what I do so drawing a star forward we'll just do two hundred and then I would play around with this angle for a while but after careful tweaking I found that 216 degrees so kind of the breakdown of why that works is you turn left a full 180 to be going the opposite way and then turn 36 more to get that nice sharp angle that you're looking for and then we're just gonna repeat this and we'll have to do it at least five times because there's five lines and a star look at that come on look at that nice nice looking star and I should have mentioned this with the square but if we want it to probably the good code practice would be to like create a function that takes in a turtle that makes a star so I could do something like this well we'll just call this will take say it takes in Pegasus and now instead of like calling this code a bunch if I wanted to repeat stars all over my place I could just call star Pegasus and that would run the code so sometimes it's good to draw it out into a function we're actually probably not going to do that right now but we could have done that with our square if we wanted to draw squares different places like just make it a function then call the function wherever you want to do it okay so we have the star but what we need to do is we need to smaller stars off of bigger stars so the angle doesn't change at all but the length of our lines does change so if I change all of these to 100 and this is gonna get obnoxious to do let's just make a for loop I changed all of these so a hundred will play around with this recession range five there's gonna be a smaller star hopefully yeah it is what if we made it 50 would that be smaller still even smaller still let's make it 10 I can still tell it's the star it's a little bit harder at this point but you get that if I pass in a smaller value here we get smaller stars so what we want to do with this recursive function is basically draw our star and as we're drawing the star passing stars with half the size so this is what it's gonna look like so we're going to do I'm gonna make a reverse recursive function called star so I'm just going to comment this stuff out real quick and the recursive function is gonna look like this it's gonna go call it will just call it star it's gonna take in a stir ttle and then it's also gonna take in the size variable oh my god what I just saw I can't type Z size variable so in this case like the size is the amount we want to go forward and what we want to do is we want to just copy in this code because we want to draw a star but the the change we want to make is that you can also undo comments with another control slash just FYI the change we want to make is that so this should really do anything now because we didn't call star if I called star with Pegasus oh and this should be turtle we should pass in the same thing that we you call it use as a variable in our function so we just turtle here in our function and your turtle here so now we're calling o it's gonna give me an error because I didn't give sighs so let's start with 100 oh shoot and I want to do size here I cannot type a Z okay so it gives me star like we want it let's do 300 that's that give us a bigger star yes it does perfect so what we want to do is let's just say at every line every time we finish a forward movement let's draw a smaller turtle so what we can do to do that is we call that star function with our turtle but now instead of passing in size we do let's do size in half so size divided by two watch what happens now oh look maybe I used too big of a value let's just let's try this with 200 still too big oh my god what's happening let's try it with 100 just to begin and it's giving me an error for some reason don't quite know why yet so it might not be working right now because whenever we have a recursive function we need a end case so we don't want it to keep going if the size is less than or equal to 10 so I'm going to return if that happens and we'll say else here I know this Aramis is just blocking you yeah that's where I can now looks like I think there was one other possibility for an error actually no I don't think that is I was thinking that maybe the size had to be like an integer value or something but it seems like he was trying to you know I guess what was happening was it was it kept entering this loop and calling but it would go forward but then it would call star with the smaller size and because it was just dividing and dividing and never having an end case to return out of to break out of that recursion it never would stop moving forward to just move forward slower and slower amounts like smaller and smaller amounts and that would just like pretty much forever but as you can see we have some stars here it's kind of looking like my my finished product over here but it's getting a little bit crowded and really all to fix the crowding all you have to do is just use probably a better value I also I started in mind I started with 360 and instead of item I 2 I divided by 3 so I'll save that right and if you want to see the full thing just let that go out here thing is though like we're now drawing this recursive stars and it's not much code at all actually like it's a pretty straightforward amount of code and if we wanted to add some like fills like the smallest stars in mine were filled with this this doesn't fill every star gets kind of tricky with the recursion but you can fill the smallest stars with this but yeah this is basically all the code you can also change the location one thing I never mentioned before is you can use I'm going to do Pegasus top it pen up and then Pegasus so that's lifting the pen go to is a function that can just put us to a specific location on that on the map so I could move this like negative 200 X 100 Y and that will give us and then put our pen back down that will just move it so that our first star actually fits on the screen pega pega goose is not defined look at that we've got our recursive function working okay that's all I'm gonna go over in this video hope you hopefully you've learned something and have fun in the process make sure to subscribe to not miss any of my future Python videos or any other stem type video that I'll publish and if you learned something in this video would mean a lot to me if you throw it a big thumbs up peace
Info
Channel: Keith Galli
Views: 414,287
Rating: 4.8769813 out of 5
Keywords: KGMIT, Keith Galli, MIT, Python Tutorial, Python, Python 3, Python Turtle, Python Turtle Graphics, Turtle Graphics, graphics in python, Drawing in python, Animations in python, beginner, easy, simple, drawing shapes in python, python tutorial for beginners with examples, best python tutorial, python tutorial for kids, kids, recursion in python, basic python, easy python for beginners, python programming, programming, beginner python exercises, turtle, exercise, graphics, animations, draw
Id: pxKu2pQ7ILo
Channel Id: undefined
Length: 39min 29sec (2369 seconds)
Published: Fri Feb 16 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.