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