Python 初学者蟒蛇3:关于海龟模块的问题
这是练习 1) 为什么我试图改变乌龟轮的速度。速度(10)不起作用?如果我不指示速度,默认速度是多少 2) 一旦我的乌龟完成了,我该如何把它放在圆圈中间Python 初学者蟒蛇3:关于海龟模块的问题,python,python-3.x,Python,Python 3.x,这是练习 1) 为什么我试图改变乌龟轮的速度。速度(10)不起作用?如果我不指示速度,默认速度是多少 2) 一旦我的乌龟完成了,我该如何把它放在圆圈中间 非常感谢你 正如abarnert所说,与你的锻炼相关的链接断开了,但是你可以做一些事情来加速你的锻炼计划。FWIW,海龟模块有一个.circle方法,但我想你练习的重点是使用多边形来近似圆 车轮速度(10)确实有效(尽管车轮速度(0)甚至更快)。这看起来没有多大区别,因为360边多边形的边非常小,所以海龟必须做几次移动和旋转才能穿过一个像素 使
非常感谢你 正如abarnert所说,与你的锻炼相关的链接断开了,但是你可以做一些事情来加速你的锻炼计划。FWIW,海龟模块有一个
.circle
方法,但我想你练习的重点是使用多边形来近似圆
车轮速度(10)
确实有效(尽管车轮速度(0)
甚至更快)。这看起来没有多大区别,因为360边多边形的边非常小,所以海龟必须做几次移动和旋转才能穿过一个像素
使用360边多边形绘制一个圆,无论半径是多少,都不是一个好的策略。下面的代码确定圆周的边数,使每边约为1个像素。这对于任何半径的圆都很好,并且可以避免对小圆做太多的工作
除了使用turtle.speed
加快速度外,还可以减少画布的帧延迟。另外,如果你在画画的时候把乌龟藏起来,它会走得更快
在Python2.6.6上测试了以下代码:;在Python3.x上,它也应该可以工作,而不需要修改,但是在Python3.x上,您可以从“导入”部分的“打印”函数中删除
#!/usr/bin/env python
''' Approximate a circle using a polygon
From http://stackoverflow.com/q/30475519/4014959
Code revised by PM 2Ring 2015.05.27
'''
from __future__ import division, print_function
import turtle
def drawPolygon(t, sideLength, numSides):
turnAngle = 360 / numSides
for i in range(numSides):
t.forward(sideLength)
t.left(turnAngle)
def drawFilledCircle(anyTurtle, radius, vcolor):
anyTurtle.fillcolor(vcolor)
anyTurtle.begin_fill()
circumference = 2 * 3.14159 * radius
#Calculate number of sides so that each side is approximately 1 pixel
numSides = int(0.5 + circumference)
sideLength = circumference / numSides
print('side length = {0}, number of sides = {1}'.format(sideLength, numSides))
drawPolygon(anyTurtle, sideLength, numSides)
anyTurtle.end_fill()
wn = turtle.Screen()
#Set minimum canvas update delay
wn.delay(1)
wheel = turtle.Turtle()
#Set fastest turtle speed
wheel.speed(0)
#wheel.hideturtle()
radius = 20
#Move to the bottom of the circle, so
#that the filled circle will be centered.
wheel.up()
wheel.goto(0, -radius)
wheel.down()
drawFilledCircle(wheel, radius, 'purple')
#Move turtle to the center of the circle
wheel.up()
wheel.goto(0, 0)
#wheel.showturtle()
wn.exitonclick()
“不起作用”是什么意思?你想在哪里做?你期望发生什么?到底发生了什么?你剩下的问题都包含在。(我敢打赌,第二个问题也会在你做练习的任何地方详细讨论,但因为你链接到了它,所以很难确定。)
#!/usr/bin/env python
''' Approximate a circle using a polygon
From http://stackoverflow.com/q/30475519/4014959
Code revised by PM 2Ring 2015.05.27
'''
from __future__ import division, print_function
import turtle
def drawPolygon(t, sideLength, numSides):
turnAngle = 360 / numSides
for i in range(numSides):
t.forward(sideLength)
t.left(turnAngle)
def drawFilledCircle(anyTurtle, radius, vcolor):
anyTurtle.fillcolor(vcolor)
anyTurtle.begin_fill()
circumference = 2 * 3.14159 * radius
#Calculate number of sides so that each side is approximately 1 pixel
numSides = int(0.5 + circumference)
sideLength = circumference / numSides
print('side length = {0}, number of sides = {1}'.format(sideLength, numSides))
drawPolygon(anyTurtle, sideLength, numSides)
anyTurtle.end_fill()
wn = turtle.Screen()
#Set minimum canvas update delay
wn.delay(1)
wheel = turtle.Turtle()
#Set fastest turtle speed
wheel.speed(0)
#wheel.hideturtle()
radius = 20
#Move to the bottom of the circle, so
#that the filled circle will be centered.
wheel.up()
wheel.goto(0, -radius)
wheel.down()
drawFilledCircle(wheel, radius, 'purple')
#Move turtle to the center of the circle
wheel.up()
wheel.goto(0, 0)
#wheel.showturtle()
wn.exitonclick()