简体   繁体   中英

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.

Then just move the pen forward in the direction needed!

A function might look something like this:

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

then just call it with the rest of your code:

--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)

  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)

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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