How to draw a pattern of 4 colored circles in Turtle?

Viewed 50

Here's my attempt:

from turtle import *

speed(100)
pensize(4)

color("black", "yellow")
begin_fill()
for i in range(4):
    fd(200)
    lt(90)
    end_fill()

    up()
    fd(100)
    lt(90)
    fd(100)
    down()

colors = ["red", "blue" , "green" , "violet" ]

for i in range(4):
    color("black",colors[i])
    begin_fill()
    circle(50)
    rt(90)
    end_fill()
    
ht()

Here's the expected result:

4 colored circles in a square layout

What am I doing wrong here?

2 Answers

An alternate approach, using stamping instead of drawing:

from turtle import Screen, Turtle

SIZE = 50
CURSOR_SIZE = 20
COLORS = ['red', 'blue', 'lime', 'yellow']

screen = Screen()

turtle = Turtle()
turtle.speed('fastest')
turtle.shape('circle')
turtle.shapesize(SIZE*2 / CURSOR_SIZE, outline=4)
turtle.penup()

for color in COLORS:
    turtle.fillcolor(color)
    turtle.stamp()
    turtle.backward(SIZE)
    turtle.left(90)

turtle.hideturtle()
screen.exitonclick()

You're pretty close. You can see that there's a 45-degree rotation relative to your drawing, and the colors are a bit off.

My strategy is this: draw a square around the origin with each corner counterclockwise a colored circle.

import turtle

size = 50
colors = (1, 0, 0), (0, 0, 1), (0, 1, 0), (1, 1, 0)
t = turtle.Turtle()
t.speed("fastest")
t.pensize(4)
t.penup()
t.goto(size / 2, size / 2)

for color in colors:
    t.color("black", color)
    t.pendown()
    t.begin_fill()
    t.circle(size)
    t.end_fill()
    t.penup()
    t.left(90)
    t.forward(size)

t.hideturtle()
turtle.exitonclick()

I prefer a turtle instance rather than the namespace-polluting from turtle import * which can lead to subtle bugs.

I recommend using the long version of the commands for clarity.

Also, speed(100) isn't any faster than the maximum of 0 or "fastest", according to the docs.

Related