Python 如何画三个相互重叠的正方形?
我想使用类似于以下的turtle函数在Python中绘制三个重叠的正方形: [1] : 到目前为止,这是我的代码Python 如何画三个相互重叠的正方形?,python,turtle-graphics,Python,Turtle Graphics,我想使用类似于以下的turtle函数在Python中绘制三个重叠的正方形: [1] : 到目前为止,这是我的代码 from turtle import * # Set up canvas setup() title('three (3) squares of different colours') speed('fastest') pensize(4) # Draw the outline of square penup
from turtle import *
# Set up canvas
setup()
title('three (3) squares of different colours')
speed('fastest')
pensize(4)
# Draw the outline of square
penup ()
goto (0,0)
pendown ()
color ('black')
for number in range (4):
forward(150)
left(90)
penup()
# Fill square with colour
goto (0,0)
color('black', 'yellow')
pendown()
begin_fill()
for number in range (4):
forward(150)
left(90)
end_fill()
hideturtle ()
done ()
你没有画三个正方形,而是画了两次相同的正方形——一次填充,一次不填充。您应该能够使用一系列简单的
forward()
、right(90)
和left(90)
命令绘制此图。以及颜色更改和填充请求
我认为有趣的方法是考虑红色和绿色正方形大小相同,因此我们应该能够用不同的填充颜色重用相同的代码。第二个挑战是,在最初将笔以最佳方式放置在窗口中之后,在不再次提起笔的情况下绘制图形。这类似于举起铅笔在纸上画这个数字
from turtle import *
LENGTH = 415
OUTLINE = 10
title('Three (3) squares of different colours')
pensize(OUTLINE)
penup()
goto(-LENGTH/4, LENGTH/4) # center drawing on the window
pendown()
fillcolor('lime')
for _ in range(2):
begin_fill()
forward(LENGTH/2)
right(90)
for _ in range(3):
forward(LENGTH)
right(90)
forward(LENGTH/2)
end_fill()
fillcolor('red')
left(90)
left(90)
fillcolor('yellow')
begin_fill()
for _ in range(4):
forward(LENGTH/2)
left(90)
end_fill()
hideturtle()
exitonclick()
只是一些想法。还有其他有趣的方法来接近这幅画。例如,我们可以使用冲压代替绘图:
非常感谢。我会给它一个答案:不要感谢和删除问题和答案,考虑测试答案,如果你觉得它有用,就接受它。
from turtle import *
LENGTH = 415
OUTLINE = 10
CURSOR_SIZE = 20
title('Three (3) squares of different colours')
penup()
shape('square')
shapesize(LENGTH / CURSOR_SIZE, outline=OUTLINE)
goto(LENGTH/4, LENGTH/4)
fillcolor('red')
stamp()
goto(-LENGTH/4, -LENGTH/4)
fillcolor('lime')
stamp()
shapesize(LENGTH / 2 / CURSOR_SIZE)
home()
fillcolor('yellow')
stamp()
hideturtle()
exitonclick()