如何在正方形内绘制正方形?

时间:2019-12-17 18:04:36

标签: python turtle-graphics

pink square is the task我正在尝试使用乌龟图形在正方形内绘制正方形。我不能完全像原来的那样画画。 这是我的代码

import turtle
win = turtle.Screen()
tess = turtle.Turtle()

for two in range(12):

for _ in range(4):
    tess.forward(20)
    tess.left(90)
tess.penup()
tess.goto(-7,-4)
tess.pendown()
for _ in range(4):
    tess.forward(35)
    tess.left(90)
tess.penup()
tess.goto(-14,-8)
tess.pendown()
for _ in range(4):
    tess.forward(50)
    tess.left(90)
tess.penup()
tess.goto(-21,-16)
tess.pendown()
for _ in range(4):
    tess.forward(70)
    tess.left(90)

2 个答案:

答案 0 :(得分:0)

我认为,我的代码解决了您的问题。

from turtle import *

def draw_square(a,color,x,y):
    penup()
    goto(x,y)
    setheading(90)
    backward(a//2)
    setheading(0)
    backward(a//2)
    pendown()
    pencolor(color)
    for _ in range(4):
        forward(a)
        left(90)

draw_square(20,"pink",0,0)
draw_square(50,"pink",0,0)
draw_square(70,"pink",0,0)
draw_square(90,"pink",0,0)
draw_square(110,"pink",0,0)

答案 1 :(得分:0)

一个很好的例子,可以通过冲压而不是绘画更好地生活:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

screen = Screen()
screen.bgcolor('lightgreen')

turtle = Turtle()
turtle.shape('square')
turtle.color('pink', 'lightgreen')

for size in range(100, 0, -20):
    turtle.shapesize(size / CURSOR_SIZE, outline=3)
    turtle.stamp()

screen.exitonclick()

enter image description here