Python Turtle Graphics Tutorial #2 - Shapes and Fills

# Python Turtle Graphics Tutorial: Filling Shapes and Drawing Random Objects

Welcome back to another YouTube video! This is the second installment in my Python turtle programming series. In this tutorial, I’ll show you how to fill shapes and draw them randomly on the screen. If you’re not interested in shapes and prefer to learn about mouse or key click events instead, feel free to skip ahead to the next video. However, I still recommend watching this one to get a solid foundation in using the turtle module.

---

## Setting Up Your Program

At the start of our program, we’ve included some basic setup code to save time:

```python

import turtle

import random

tim = turtle.Turtle()

colors = ["red", "blue", "green", "yellow", "black"]

turtle.colormode(255)

tim.color("red", "blue")

tim.width(5)

```

Here’s what this code does:

- `import turtle` and `import random` are necessary for using the turtle graphics library and generating random numbers.

- `tim = turtle.Turtle()` creates a new turtle object named `tim`.

- We define a list of colors for later use, but they aren’t needed right now.

- `turtle.colormode(255)` sets up the color mode to allow RGB coloring (more on this later).

- `tim.color("red", "blue")` sets the pen color to red and the fill color to blue.

- `tim.width(5)` sets the width of the turtle’s pen.

---

## Filling Shapes

To fill a shape in turtle graphics, you need to use the `begin_fill()` and `end_fill()` methods. Here’s an example with a circle:

```python

tim.begin_fill()

tim.circle(50)

tim.end_fill()

```

- `tim.begin_fill()` starts the filling process.

- `tim.circle(50)` draws a circle with a radius of 50 pixels.

- `tim.end_fill()` fills in the shape that was just drawn.

When you run this code, you’ll see a blue-filled circle on the screen. The outline of the circle will be red because we set the pen color to red earlier.

---

## Drawing Squares

Drawing squares is similar to drawing circles, but it requires more steps. Here’s how you can draw a square manually:

```python

for _ in range(4):

tim.forward(100)

tim.right(90)

```

This code moves the turtle forward 100 pixels and turns it right by 90 degrees, repeating this process four times to create a square. If you want to fill the square, add `tim.begin_fill()` before drawing and `tim.end_fill()` after finishing:

```python

tim.begin_fill()

for _ in range(4):

tim.forward(100)

tim.right(90)

tim.end_fill()

```

You can also change the color of the turtle’s pen for each shape by using `tim.color(new_color)` before drawing. For example, to draw a yellow square:

```python

tim.color("yellow", "black")

for _ in range(4):

tim.forward(100)

tim.right(90)

tim.end_fill()

```

---

## Drawing Random Circles

To make things more interesting, let’s learn how to draw random circles on the screen. First, you’ll need to understand how positions work in turtle graphics.

The turtle module uses a Cartesian coordinate system:

- The center of the screen is `(0, 0)`.

- Positive X values go to the right, and positive Y values go upward.

- Negative X values go to the left, and negative Y values go downward.

To draw circles at random positions, we’ll use the `setposition()` method. Here’s how it works:

```python

tim.penup()

x = random.randint(-300, 300)

y = random.randint(-300, 300)

tim.setposition(x, y)

tim.pendown()

size = random.randint(0, 80)

tim.begin_fill()

tim.circle(size)

tim.end_fill()

```

- `tim.penup()` lifts the pen so that the turtle doesn’t draw while moving.

- `random.randint(-300, 300)` generates a random X and Y coordinate within the range `-300` to `300`.

- `tim.setposition(x, y)` moves the turtle to the new position.

- `tim.pendown()` puts the pen back down so it can start drawing again.

- `size = random.randint(0, 80)` generates a random circle size between 0 and 80 pixels.

You can also change the color of each circle randomly by adding:

```python

fill_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

tim.color(fill_color)

```

This will create circles with different colors every time you draw one.

---

## Conclusion

In this tutorial, we’ve learned how to:

1. Set up the turtle graphics environment.

2. Fill shapes using `begin_fill()` and `end_fill()`.

3. Draw squares manually or using loops.

4. Draw random circles at random positions on the screen with different colors.

If you have any questions or need clarification on anything, feel free to leave a comment below. Don’t forget to like this video and subscribe for more Python tutorials in the future! I’ll see you in the next one!

"WEBVTTKind: captionsLanguage: enhey guys and welcome back to another YouTube video this is gonna be the second video in my Python turtle programming series or a tutorial whatever I called it on there and in this video I'm going to be showing you how to fill shapes and how you can draw shapes kind of randomly on the screen now there's a few new things that I have that I'm gonna be going over in just a minute but I just want to say if you guys aren't really interested in shapes and you kind of want to learn some cooler stuff maybe just head on to the next video in that video I'm going to be showing you what happens when you have mouse click events and key click events and how you can kind of drag the turtle around and it can draw based on where your mouse is going and that one's cool as well I still recommend you watch this video just you have some more basics of the turtle module but if you're not interested in that go ahead and hop over to the next one alright so let's go ahead and get started so again at the beginning of our program I've just written a little starter file here just to save some time we import turtle I do import random just because I'm gonna do something cool at the end of the video but I'm just leaving up there now and I have Tim equals turtle turtle creating again a new turtle object and then I have this list of colors again we're not going to use right now and then set the color for the turtle so we go red and then this is a little bit different from the last video so red is going to be our outline color and blue is gonna be our fill color so by simply putting a comma here we can separate those setting the pen width of the turtle width which is going to be width of 5 and then this is how we fill in a shape so to fill in the shape we start by writing our total objects Tim dot begin fill after that we can draw whatever we want and after we're done we hit end fill and it's gonna fill in the shape it just drew so when we make a circle of radius 50 and this one is new so inside circle it creates a circle of radius 50 then it's gonna end the fill and we can see what happens when I run the program now don't think you can go ahead and do turtle to Tim's square Tim triangle doesn't work like that there's only one for circle okay so let's go ahead and run the program and see what happens there we go so we get a circle and it's filled in blue pretty straightforward now I'm going to show you how we would do a square per say it's the same kind of idea but I'm showing you guys anyways so first I'm just gonna move my pen up off the screen so we're not drawing and I'm moving my turtle I'm gonna go Tim go forward 150 pixels get out of the way and Tim dot and down now I do also want to change the color so I'm gonna get Tim color and in this case let's do yellow and black like that and see how that looks and now I'm gonna go ahead and draw a square so the you can draw a square by using like Tim dot forward Tim dot right Tim go for Tim dot right and having eight lines of code or you can do the easy way by creating a for loop so I'm gonna do that I'm gonna show you guys how it works so 4x in range for Tim dot forward 100 pixels and then Tim dot right nine now this is gonna execute four times we're gonna go forward ones turn right at forward ones turn right and it's gonna end up creating a square now that's a much easier way to do it then just keep writing out forward right forward right it's just kind of a pain now I also want to fill it this in so what I'm gonna do is before I start drawing the objects I'm gonna do Tim dot begin underscore bill and then Tim dot and underscore fill just like that and now we can go ahead and run the program and see what we get there we go so again really straightforward we have the circle and then bill the screen we move to the right and then we draw the rectangle filled in now that's pretty much all the commands I'm gonna be showing you guys but stick around because what I'm gonna do now is show you how you would go about drawing random circles or random objects on the screen and it actually does use one command so I so I lied that I haven't shown you guys yet and that command is gonna be set underscore position like this okay so the way that positions work in turtle is unlike pi game if you've seen that let me just open up this window here quickly it's probably gonna crash because I didn't give it a position yep don't worry about that so right here where the circles drawn is the middle of our screen now the way it works here is if we can draw an axis here and an axis here then we can have a Cartesian kind of a plane right so here from right on if we have an imaginary line down the middle we have 0 1 2 3 4 for the X and then upwards so above this line we have 1 2 3 4 positive for the X so in this right quadrant here it's positive down here we have positive in the X and negative in the Y because we're underneath that horizontal line and I'm making with my mouse right now imaginary now if we move over into this furthest left quadrant here that means we're underneath the horizontal line or to the left of the what do you call it vertical line so we have negative for the x value so negative 1 negative 2 negative 3 and negative for the Y values get negative 1 negative 2 negative 3 going downwards again last quadrant top left we have negative for the X because again from this horizontal line which the left of it and then we're positive for the Y because we're above this horizontal line it's just an Cartesian plane it's really basic and if you guys don't understand just look up Cartesian plane on Google and I'm sure you can find something that can explain it to you or you help you visualize better so when I've set positions here if I want to move to the left of the middle of the screen then I have to do something like negative 200 and then if I want to be moving downwards I would go negative 150 like so and that would put me in the bottom left quadrant of the screen so just explain that before I started using so what I'm gonna do is I'm going to create the for loop I'm just going to go for X and range and this is going to be how many objects I want to draw so in this case I just want to do five and we're gonna make it really basic and I'm just gonna make a bunch of circles so what I won't need to do is I need to create two random integers so a way that we do that by importing random is we can do Rand 1 it's gonna be random dots Rand range and then I believe you can do this I'm gonna do negative 300 - 300 because again we need negative values so we can go to the left and down and now for rent - I'm gonna do the exact same thing we're just gonna be called rant - and there we go now what I'm when I set the position for my turtle I want to make sure that I have my pen up so that it doesn't draw a line while I'm moving the turtle that makes sense so I'm gonna do tim duck pen up set the position and we're just going to simply set an x-coordinate and a y-coordinate so we're gonna use R and 1 as the X and ran to as the Y now I'm not sure if this has to come in as a tuple we'll test this out but excuse me if we get an error then I'm gonna put the pen down like that I'm gonna begin a fill so to begin the fill again we do Tim dog begin underscore oops yeah underscore fill like so and then I am going to go here I'm gonna draw a circle so the way we did is certainly got his Tim dog circle and in here I'm going to do random dog Rand ring and we're gonna put zero to 80 so we can get different size circles they're not all the same and end the fill right here oops not begin and underscore fill now I also would like to change the color of my turtle every time that I create a new object so the way I can do that is we need to create another random integer so in this case I'm just gonna call this R and call there like so and that's gonna be between say a Rand brand range like that and then in here we need to do zero and then the length of our couple is so zero and Len of colors like so now I know it's a lot of typing going on here we need to change the color of our turtle so timko there and then in here is going to be equal to colors brand color like so now I probably made a mistake here so forgive me but let's give it a try so we first start by drawing a circle then our square and has no attribute set ha okay let me just have a quick look at what the position is for setting here go to maybe go to is the one that we have to use set position set post oh no underscore that's what sorry about that guns so set pause is what we needed and hopefully this will work for us crossing our fingers there we go Circle Circle Circle and we're drawing random circles around the screen awesome so unfortunately we didn't get a too many variety of different colors but you can see how that works if we wanted to have a different fill color you can just do this do colors and then random branch range again zero blend colors like so and then that would give us a random fill color for our circles hopefully yeah there we go and we can see we get more of a variety now as they draw on the screen all right so that's been it for this video on python turtle graphics if you have any questions or anything you'd like to address just leave a comment down below I'd be more than happy to help you guys out and if you liked the video please make sure you leave a like and subscribe and I will see you again in the next onehey guys and welcome back to another YouTube video this is gonna be the second video in my Python turtle programming series or a tutorial whatever I called it on there and in this video I'm going to be showing you how to fill shapes and how you can draw shapes kind of randomly on the screen now there's a few new things that I have that I'm gonna be going over in just a minute but I just want to say if you guys aren't really interested in shapes and you kind of want to learn some cooler stuff maybe just head on to the next video in that video I'm going to be showing you what happens when you have mouse click events and key click events and how you can kind of drag the turtle around and it can draw based on where your mouse is going and that one's cool as well I still recommend you watch this video just you have some more basics of the turtle module but if you're not interested in that go ahead and hop over to the next one alright so let's go ahead and get started so again at the beginning of our program I've just written a little starter file here just to save some time we import turtle I do import random just because I'm gonna do something cool at the end of the video but I'm just leaving up there now and I have Tim equals turtle turtle creating again a new turtle object and then I have this list of colors again we're not going to use right now and then set the color for the turtle so we go red and then this is a little bit different from the last video so red is going to be our outline color and blue is gonna be our fill color so by simply putting a comma here we can separate those setting the pen width of the turtle width which is going to be width of 5 and then this is how we fill in a shape so to fill in the shape we start by writing our total objects Tim dot begin fill after that we can draw whatever we want and after we're done we hit end fill and it's gonna fill in the shape it just drew so when we make a circle of radius 50 and this one is new so inside circle it creates a circle of radius 50 then it's gonna end the fill and we can see what happens when I run the program now don't think you can go ahead and do turtle to Tim's square Tim triangle doesn't work like that there's only one for circle okay so let's go ahead and run the program and see what happens there we go so we get a circle and it's filled in blue pretty straightforward now I'm going to show you how we would do a square per say it's the same kind of idea but I'm showing you guys anyways so first I'm just gonna move my pen up off the screen so we're not drawing and I'm moving my turtle I'm gonna go Tim go forward 150 pixels get out of the way and Tim dot and down now I do also want to change the color so I'm gonna get Tim color and in this case let's do yellow and black like that and see how that looks and now I'm gonna go ahead and draw a square so the you can draw a square by using like Tim dot forward Tim dot right Tim go for Tim dot right and having eight lines of code or you can do the easy way by creating a for loop so I'm gonna do that I'm gonna show you guys how it works so 4x in range for Tim dot forward 100 pixels and then Tim dot right nine now this is gonna execute four times we're gonna go forward ones turn right at forward ones turn right and it's gonna end up creating a square now that's a much easier way to do it then just keep writing out forward right forward right it's just kind of a pain now I also want to fill it this in so what I'm gonna do is before I start drawing the objects I'm gonna do Tim dot begin underscore bill and then Tim dot and underscore fill just like that and now we can go ahead and run the program and see what we get there we go so again really straightforward we have the circle and then bill the screen we move to the right and then we draw the rectangle filled in now that's pretty much all the commands I'm gonna be showing you guys but stick around because what I'm gonna do now is show you how you would go about drawing random circles or random objects on the screen and it actually does use one command so I so I lied that I haven't shown you guys yet and that command is gonna be set underscore position like this okay so the way that positions work in turtle is unlike pi game if you've seen that let me just open up this window here quickly it's probably gonna crash because I didn't give it a position yep don't worry about that so right here where the circles drawn is the middle of our screen now the way it works here is if we can draw an axis here and an axis here then we can have a Cartesian kind of a plane right so here from right on if we have an imaginary line down the middle we have 0 1 2 3 4 for the X and then upwards so above this line we have 1 2 3 4 positive for the X so in this right quadrant here it's positive down here we have positive in the X and negative in the Y because we're underneath that horizontal line and I'm making with my mouse right now imaginary now if we move over into this furthest left quadrant here that means we're underneath the horizontal line or to the left of the what do you call it vertical line so we have negative for the x value so negative 1 negative 2 negative 3 and negative for the Y values get negative 1 negative 2 negative 3 going downwards again last quadrant top left we have negative for the X because again from this horizontal line which the left of it and then we're positive for the Y because we're above this horizontal line it's just an Cartesian plane it's really basic and if you guys don't understand just look up Cartesian plane on Google and I'm sure you can find something that can explain it to you or you help you visualize better so when I've set positions here if I want to move to the left of the middle of the screen then I have to do something like negative 200 and then if I want to be moving downwards I would go negative 150 like so and that would put me in the bottom left quadrant of the screen so just explain that before I started using so what I'm gonna do is I'm going to create the for loop I'm just going to go for X and range and this is going to be how many objects I want to draw so in this case I just want to do five and we're gonna make it really basic and I'm just gonna make a bunch of circles so what I won't need to do is I need to create two random integers so a way that we do that by importing random is we can do Rand 1 it's gonna be random dots Rand range and then I believe you can do this I'm gonna do negative 300 - 300 because again we need negative values so we can go to the left and down and now for rent - I'm gonna do the exact same thing we're just gonna be called rant - and there we go now what I'm when I set the position for my turtle I want to make sure that I have my pen up so that it doesn't draw a line while I'm moving the turtle that makes sense so I'm gonna do tim duck pen up set the position and we're just going to simply set an x-coordinate and a y-coordinate so we're gonna use R and 1 as the X and ran to as the Y now I'm not sure if this has to come in as a tuple we'll test this out but excuse me if we get an error then I'm gonna put the pen down like that I'm gonna begin a fill so to begin the fill again we do Tim dog begin underscore oops yeah underscore fill like so and then I am going to go here I'm gonna draw a circle so the way we did is certainly got his Tim dog circle and in here I'm going to do random dog Rand ring and we're gonna put zero to 80 so we can get different size circles they're not all the same and end the fill right here oops not begin and underscore fill now I also would like to change the color of my turtle every time that I create a new object so the way I can do that is we need to create another random integer so in this case I'm just gonna call this R and call there like so and that's gonna be between say a Rand brand range like that and then in here we need to do zero and then the length of our couple is so zero and Len of colors like so now I know it's a lot of typing going on here we need to change the color of our turtle so timko there and then in here is going to be equal to colors brand color like so now I probably made a mistake here so forgive me but let's give it a try so we first start by drawing a circle then our square and has no attribute set ha okay let me just have a quick look at what the position is for setting here go to maybe go to is the one that we have to use set position set post oh no underscore that's what sorry about that guns so set pause is what we needed and hopefully this will work for us crossing our fingers there we go Circle Circle Circle and we're drawing random circles around the screen awesome so unfortunately we didn't get a too many variety of different colors but you can see how that works if we wanted to have a different fill color you can just do this do colors and then random branch range again zero blend colors like so and then that would give us a random fill color for our circles hopefully yeah there we go and we can see we get more of a variety now as they draw on the screen all right so that's been it for this video on python turtle graphics if you have any questions or anything you'd like to address just leave a comment down below I'd be more than happy to help you guys out and if you liked the video please make sure you leave a like and subscribe and I will see you again in the next one\n"