Go back
Shapes and Fills
Subscribe to Tech with Tim
YouTube
Drawing and Filling Shapes
Now that we've learned how to move the turtle around the screen we can move onto drawing and filling shapes.
To draw a circle we can use:
- .circle(radius)
To fill shapes we must play a .begin_fill() where would like to start filling and a .end_fill() where we would like to stop.
import turtle tim = turtle.Turtle() tim.color("red", "blue") # Blue is the fill color tim.width(5) tim.begin_fill() tim.circle(50) tim.end_fill()
This results in the following image.
In the last tutorial I showed you how to draw a square using a series of turtle commands. An easier way uses a for loop and can be seen below.
import turtle tim = turtle.Turtle() tim.color("red", "blue") # Blue is the fill color tim.width(5) tim.begin_fill() for x in range(4): # This will draw a square filled in tim.forward(100) tim.right(90) tim.end_fill()
A new command that we can use to set the absolute position of the turtle object is .setpos(x, y).
tim.setpos(100, -50)
Example
The following program will randomly draw shapes on the screen.
import turtle import random colors = ["red", "blue","green", "purple", "yellow", "orange", "black"] tim = turtle.Turtle() tim.color("red", "blue") for x in range(5): randColor = random.randrange(0, len(colors)) rand1 = random.randrange(-300,300) rand2 = random.randrange(-300,300) tim.color(colors[randColor], colors[random.randrange(0, len(colors))]) tim.penup() tim.setpos((rand1, rand2)) tim.pendown() tim.begin_fill() tim.circle(random.randrange(0,80)) tim.end_fill() ```