As the Tokyo Olympics start, here's some Python code to draw the Olympic rings, using the turtle module.
You'll use the turtle module which you'll need to import, and then create a Turtle() which I'm calling ring in this code.
After setting the pensize to make the lines thicker, and setting the constant radius, you can start drawing the rings.
You'll alternate between drawing a circle with the pendown option, and then moving the ring turtle to the bottom of the next circle with the penup option.
Each time, you'll need to change the colour of the ring, of course!
When you move between one ring and the next, you need to shift the turtle by a bit more than the diameter of the circle as the rings have a gap between them.
After the first three rings, you'll need to move the turtle in the correct place so it's ready for the bottom two rings.
At the very end, you can use hideturtle so that the turtle (the arrow) is not visible.
Here's the full code:
import turtle
ring = turtle.Turtle()
ring.pensize(6)
radius = 50
# Set starting point
ring.penup()
ring.backward(radius*2)
ring.pendown()
# Top three rings
ring.color("blue")
ring.circle(radius)
ring.penup()
ring.forward(radius*2 + radius/5)
ring.pendown()
ring.color("black")
ring.circle(radius)
ring.penup()
ring.forward(radius*2 + radius/5)
ring.pendown()
ring.color("red")
ring.circle(radius)
ring.penup()
# Set up for bottom rings
ring.left(180)
ring.forward(radius + radius/10)
ring.right(90)
ring.forward(radius)
ring.left(90)
# Bottom two rings
ring.pendown()
ring.color("green")
ring.circle(radius)
ring.penup()
ring.forward(2*radius + radius/5)
ring.pendown()
ring.color("yellow")
ring.circle(radius)
ring.hideturtle()
turtle.done()
Enjoyed this and want to learn more Python for free? Check out our sister site The Python Coding Book, for in-depth guides on all things Python, or follow us on Twitter @codetoday_ and @s_gruppetta_ct
Comments