简体   繁体   English

Python Turtle图形和循环

[英]Python Turtle Graphics and Loops

I am trying to create a loop which prints out 6 parallel lines horizontally.However when I using the code below I only am able to see one line that just moves continuously. 我试图创建一个循环,水平打印出6条平行线,但是当我使用下面的代码时,我只能看到一行连续移动。 Any tips would be appreciated. 任何提示将不胜感激。 Here is the code: 这是代码:

count = 0;
while(count < 6):
  actor.penup()
  actor.backward(100)
  actor.pendown()
  actor.forward(150)
  count = count + 1

You can use your familiarity with Cartesian co-ordinates. 您可以熟悉笛卡尔坐标。 Move the pen to one end of each line, pen down then move the pen to the opposite end of that line. 将笔移到每一行的一端,向下放笔,然后将笔移至该行的另一端。

>>> from turtle import *
>>> for _ in range(6):
...     up()
...     goto(0, 10*_)
...     down()
...     goto(50, 10*_)
...     
>>> up()
>>> home()

There must be other ways but I'm not at all familiar with turtle . 肯定还有其他办法,但是我对turtle也不熟悉。 乌龟图形

There must be other ways but I'm not at all familiar with turtle. 肯定还有其他方法,但我一点都不熟悉乌龟。 - @BillBell -@比尔·贝尔

Yes, there are, we can make a line shaped cookie cutter and stamp them out: 是的,有,我们可以制作线形的曲奇刀并将其压印出来:

import turtle

STAMP_SIZE = 20

actor = turtle.Turtle('square', visible=False)
actor.shapesize(0.5 / STAMP_SIZE, 200 / STAMP_SIZE, 0)
actor.penup()

for y in range(-3, 3):
    actor.sety(10 * y)
    actor.stamp()

turtle.exitonclick()

(If the lines don't appear on your system, change the 0.5 above to 1.0) (如果这些行未出现在您的系统上,请将上面的0.5更改为1.0)

This approach only uses turtle's .circle() method to draw the parallel lines: 这种方法仅使用turtle的.circle()方法绘制平行线:

import tkinter as tk
import turtle

WIDTH = 300

root = tk.Tk()  # extra work up front to make canvas smaller than window
root.title('Python Turtle Graphics')
root.geometry('{}x{}'.format(WIDTH, WIDTH * 2))

canvas = tk.Canvas(root, width=5 * WIDTH / 6, height=WIDTH * 2)
canvas.pack()
screen = turtle.TurtleScreen(canvas)

actor = turtle.RawTurtle(screen, shape='turtle')
actor.penup()
actor.right(45)
actor.goto(-WIDTH / 2, -5 * WIDTH / 6)
actor.pendown()

for y in range(3):
    actor.circle(WIDTH / 2**0.5, steps=4)
    actor.sety(actor.ycor() + WIDTH / 3)

root.mainloop()

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

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