我已经编写了一些代码,我想将画布导出为图像,但是每次运行代码时,都隐藏了乌龟,绘制了形状,然后出于某种原因再次出现了乌龟并保存了图像。我尝试将t.ht()
添加到图像保存部分的所有其他行中,但无济于事。整个代码发布在下面
import turtle
from itertools import combinations
from math import pi, cos, sin
def points(n, r):
"""Generate a list of points making the vertices of a regular n-gon centred at the origin"""
return [(r * cos(2 * pi * i / n), r * sin(2 * pi * i / n)) for i in range(n)]
def rotate(points, a):
"""Rotate a given list of points by the angle a (in radians) about the origin"""
return [(x*cos(a) - y*sin(a), y*cos(a) + x*sin(a)) for x, y in points]
def scale(points, s):
"""Scale a given list of points by the scale factor s about the origin"""
return [(x*s, y*s) for x, y in points]
def complete_poly(points, n):
"""Draw a connected graph using the points specified"""
t.pu()
t.goto(points[-1])
t.pd()
for ix, iy in combinations(range(n), 2):
t.goto(points[ix])
t.goto(points[iy])
def incomplete_poly(points, n):
"""Draw a graph connecting only nodes seperating by a single node or none"""
t.pu()
t.goto(points[-1])
t.pd()
for inner in range(n):
t.pu()
t.goto(points[inner])
t.pd()
t.goto(points[(inner+2)%n])
def outer_poly(points, n):
"""Draw only the edges of an n-gon defined by points"""
t.pu()
t.goto(points[-1])
t.pd()
for side in range(n):
t.goto(points[side])
t = turtle.Turtle()
t.lt(90)
t.speed(0)
t.ht()
# n is the number of sides of the polygon it draws
# i is the number of iterations
# r is the starting radius of the polygon
n = 7
i = 10
r = 200
s = 2*cos(pi/n) - 1/cos(pi/n)
p = points(n, r)
outer_poly(p, n)
for _ in range(i):
incomplete_poly(p, n)
p = scale(rotate(p, pi/n), s)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="7-intergon.eps")
turtle.done()
答案 0 :(得分:0)
替换您的:
ts = turtle.getscreen()
具有:
ts = turtle.Screen()
显示在图形中的乌龟不是您的乌龟,而是您通过调用turtle.getscreen()
而脱颖而出的 default 乌龟>