我的老师希望我使用turtle
用for
语句创建一个平面(图形平面)。我无法理解,所以我想知道是否有人可以为我提供代码。
#my start of code
import turtle
wn = turtle.Screen()
t = turtle.Turtle()
答案 0 :(得分:2)
也许这个例子会有所帮助。仅仅通过阅读代码来学习编程是很难的,你需要进行实验:将它拉开&把它重新组合起来,直到你明白所有不同的东西在做什么。所以玩代码,尝试修改各种事情,看看会发生什么。
#! /usr/bin/env python
''' Draw a squared grid filling the visible portion of the Turtle window
From http://stackoverflow.com/q/29943686/4014959
Written by PM 2Ring 2015.04.30
'''
import turtle
def draw_lines(current, num, length, delta):
for i in range(num):
turtle.up()
turtle.goto(current)
turtle.down()
turtle.forward(length)
current += delta
def draw_grid(step):
cols = turtle.window_width() // step
rows = turtle.window_height() // step
mx, my = cols * step, rows * step
origin = turtle.Vec2D(-mx // 2, -my // 2)
#Horizontals
delta = turtle.Vec2D(0, step)
draw_lines(origin, rows + 1, mx, delta)
#Verticals
turtle.left(90)
delta = turtle.Vec2D(step, 0)
draw_lines(origin, cols + 1, my, delta)
def main():
turtle.setup(width=0.8, height=0.8)
turtle.hideturtle()
turtle.speed(0)
draw_grid(25)
#Wait for the window to be closed
turtle.done()
if __name__ == '__main__':
main()