简体   繁体   English

乌龟图形 - 间距绘图形状

[英]turtle graphics - spacing drawing shapes

In the code below, how can I give some space after drawing each shape and then draw the next shape.在下面的代码中,如何在绘制每个形状后留出一些空间,然后再绘制下一个形状。

from turtle import *

color('black','green')
shape('turtle')
pensize(5)
speed(1)

def makeShape (numSides):
    for i in range(numSides):
        forward(100)
        left(360.0/numSides)
        i += 1
        

for i in range(3,13):
    makeShape(i)

For example, you can change the code like this:例如,您可以像这样更改代码:

space = [10, 30, 50]
for i in range(3,6):
    makeShape(i)
    up()
    setpos(space[i-3], space[i-3])
    down()

The numbers are test and you can change the numbers for the distances according to your needs.这些数字是测试的,您可以根据需要更改距离的数字。

Use the penup() and pendown() functions, which stop and start the 'Drawing mode' respectively.使用penup()和 pendown( pendown()函数,它们分别停止和启动“绘图模式”。

Then just move the pen forward in the direction needed!然后将笔朝需要的方向向前移动!

A function might look something like this: function 可能看起来像这样:

def somespace(spaceamount):
    penup()
    forward(spaceamount)
    pendown()

then just call it with the rest of your code:然后只需使用代码的 rest 调用它:

--snip--
for i in range(3,13):
    makeShape(i)
    # orientate shape here if needed
    somespace(50) # give space of 50

The code the i am sharing will produce a dashed square that will make you better understand:我分享的代码会产生一个虚线方块,让您更好地理解:

important points要点

  1. penup() is method from Turtle class that you can access with an object that in my case i access it with tim abject, will take the pen up from the screen (Not Drawing) penup() 是来自 Turtle class 的方法,您可以使用 object 访问它,在我的情况下,我使用tim abject 访问它,将从屏幕上拿起笔(不绘图)

  2. pendown() is method from Turtle class that you can access with an object that in my case i access it with tim abject, will take the pen down on the screen (Drawing) pendown() 是来自Turtle class 的方法,您可以使用 object 访问它,在我的情况下,我使用tim abject 访问它,将把笔放在屏幕上(绘图)

from turtle import Turtle, Screen

tim = Turtle()
tim.shape("turtle")
tim.color('red')

turn = 0
while turn < 4:
    for _ in range(30):
        tim.forward(5)
        tim.penup()
        tim.forward(5)
        tim.pendown()
    turn += 1    
    tim.right(90)


screen = Screen()
screen.exitonclick()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM