我想绘制4个正方形,每个正方形变小10个像素并进入另一个正方形(居中)
这就是我现在所拥有的
square.py
def drawSquare(myTurtle,sideLength,x,y):
myTurtle.setx(x)
myTurtle.sety(y)
for i in range(4):
myTurtle.forward(sideLength)
myTurtle.right(90)
main.py
>>> from square import *
>>> import turtle
>>> t = turtle.Turtle()
>>> side = 200
>>> x = 60
>>> y = 60
>>> for i in range(4):
drawSquare(t,200,x,y)
side = side - 10
x = x- 5
y = y - 5
不幸的是,它的工作方式是4个不同的方块在彼此之上
有什么建议吗?
答案 0 :(得分:0)
我使用递归来解决这个问题,因为这可能是最简单的方法。
myTurtle = turtle.Turtle()
def drawSquares(myTurtle,sideLength, x, y, nSquares, distanceApart):
if nSquares > 0:
myTurtle.pu()
myTurtle.setx(x)
myTurtle.sety(y)
myTurtle.pd()
for i in range(4):
myTurtle.forward(sideLength)
myTurtle.right(90)
drawSquares(myTurtle, sideLength - distanceApart*2, x+10, y-10, nSquares-1, distanceApart)
drawSquares(myTurtle, 200, 60, 60, 4, 10)
答案 1 :(得分:0)
使用乌龟可以通过几种不同的方式解决这个问题。虽然明显的答案似乎是用于绘制正方形的嵌套循环,但下面是其他一些思考它的方法。
首先,使用turtle.circle(steps=4)
。圆程序可以绘制任意正多边形,包括正方形,但您必须对定位进行调整:
import turtle
def drawSquares(myTurtle, sideLength, nSquares, distanceApart):
myTurtle.penup()
x, y = myTurtle.position()
myTurtle.goto(x - sideLength/ 2, y - sideLength / 2) # adjust so current x, y is center
myTurtle.pendown()
myTurtle.setheading(-45) # by default square would be sitting on corner instead of on side
for _ in range(nSquares):
radius = sideLength * 2**0.5 / 2
myTurtle.circle(radius, steps=4)
sideLength -= 10
myTurtle.penup()
x, y = myTurtle.position()
myTurtle.goto(x + distanceApart / 2, y + distanceApart / 2)
myTurtle.pendown()
yertle = turtle.Turtle()
yertle.penup()
yertle.goto(60, 60)
yertle.pendown()
drawSquares(yertle, 200, 4, 10)
turtle.done()
对于我剩下的例子,我将假设相同的调用代码,并且仅提供修改后的drawSquares()
例程。我相信@ PVNRT的递归解决方案非常好,但我会稍微调整一下如下:
def drawSquares(myTurtle, sideLength, nSquares, distanceApart):
if nSquares < 1:
return
for _ in range(4):
myTurtle.forward(sideLength)
myTurtle.left(90)
myTurtle.penup()
x, y = myTurtle.position()
myTurtle.goto(x + distanceApart / 2, y + distanceApart / 2)
myTurtle.pendown()
drawSquares(myTurtle, sideLength - distanceApart, nSquares - 1, distanceApart)
我个人最喜欢的解决此类问题的方法是使用标记而不是绘图。这是一个非常快速的解决方案:
def drawSquares(yourTurtle, sideLength, nSquares, distanceApart):
myTurtle = yourTurtle.clone() # clone turtle so we don't have to restore changes
myTurtle.shape("square") # modify turtle shape for stamping
myTurtle.fillcolor(turtle.bgcolor()) # modify turtle fill color for stamping
for _ in range(nSquares):
myTurtle.turtlesize(sideLength / 20) # magic number 20 is default stamp size
myTurtle.stamp()
sideLength -= distanceApart
最后,使用嵌套循环的显而易见的解决方案,由于您已经看到其他方法,所以现在不应该那么明显:
def drawSquares(myTurtle, sideLength, nSquares, distanceApart):
for n in range(nSquares):
for _ in range(4):
myTurtle.forward(sideLength)
myTurtle.left(90)
myTurtle.penup()
x, y = myTurtle.position()
myTurtle.goto(x + distanceApart / 2, y + distanceApart / 2)
myTurtle.pendown()
sideLength -= distanceApart
OP规范的一个弱点是x&amp; amp;的含义。你的论点。我把它们排除在外,理解正方形是在当前的x&amp;年。 OP将它们视为图纸的右上角,这与龟中的任何东西都不一致。我选择将它们设置为左下角或中心(对于圆形和基于冲压的解决方案。)