To make use of the turtle methods and functionalities, we need to import turtle. “turtle” comes packed with the standard Python package and need not be installed externally. The roadmap for executing a turtle program follows 3 steps: a- Import the turtle module. b- Create a turtle to control. c- Draw around using the turtle methods.
Instructions:
here is my code:
from turtle import *
# Set up the screen and turtle
screen = Screen()
t = Turtle()
t.speed(1) # You can adjust the speed as needed
# Define colors
color_fill = "yellow"
color_border = "blue"
border_size = 5
gap_size = 10
rectangle_width = 50 # Adjust the width as needed
rectangle_height = 100 # Adjust the height as needed
circle_radius = 50 # Adjust the radius for the circular space
# Function to draw a colored rectangle with a border
def draw_rectangle_with_border(fill_color, border_color, border_size, width, height):
# Draw the border
t.pencolor(border_color)
t.pensize(border_size)
t.penup()
t.goto(-width / 2, -height / 2)
t.pendown()
for _ in range(2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
# Draw the fill
t.fillcolor(fill_color)
t.begin_fill()
for _ in range(2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.end_fill()
# Set the starting position
t.penup()
t.goto(0, -rectangle_height / 2)
# Draw the central circular space
t.pencolor("white") # Set the color to match the background
t.fillcolor("white")
t.penup()
t.goto(0, -circle_radius)
t.pendown()
t.begin_fill()
t.circle(circle_radius)
t.end_fill()
# Calculate the total number of rectangles to form a complete circle
num_rectangles = 8
angle = 360 / num_rectangles
# Draw the circular pattern of rectangles around the central circular space
for _ in range(num_rectangles):
draw_rectangle_with_border(color_fill, color_border, border_size, rectangle_width, rectangle_height)
t.penup()
t.goto(0, -rectangle_height / 2)
t.left(angle)
t.forward(gap_size)
# Close the window on click
screen.exitonclick()
and here is the output:
and i am want to have this output :
Your rectangles look pretty good, although you don't need to draw the fill and outline separately. All that's missing is moving forward and backward from the start point to the corner of each square:
from turtle import Screen, Turtle
def draw_rectangle_with_border(t, width, height):
t.pendown()
t.begin_fill()
for _ in range(2):
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.end_fill()
t.penup()
def draw_rectangles_in_circle(t):
color_fill = "yellow"
color_border = "blue"
border_size = 9
rectangle_width = 60
rectangle_height = 90
circle_radius = 110
num_rectangles = 8
angle = 360 / num_rectangles
t.pencolor(color_border)
t.pensize(border_size)
t.fillcolor(color_fill)
t.penup()
for _ in range(num_rectangles):
t.forward(circle_radius)
draw_rectangle_with_border(t, rectangle_width, rectangle_height)
t.backward(circle_radius)
t.left(angle)
if __name__ == "__main__":
turtle = Turtle()
draw_rectangles_in_circle(turtle)
Screen().exitonclick()
Now, the actual image has a little bit of overlap from square to square, so you could do a mini-turn to make that happen and adjust to taste:
# ...
t.penup()
t.right(5)
for _ in range(num_rectangles):
t.forward(circle_radius)
t.left(5)
draw_rectangle_with_border(t, rectangle_width, rectangle_height)
t.right(5)
t.backward(circle_radius)
t.left(angle)
# ...
Note that the from turtle import *
advice is questionable. This adds over 100 methods into the global namespace, making it easy to encounter aliasing bugs and confusion between instance and functional interfaces. You're only using Screen
and Turtle
from turtle, so it's easy to import what you need explicitly, as shown above.