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!