2017-08-22 42 views
0

你好我是python的新手,我试图用龟做两个不同的形状。Python:试图在同一个文件中创建2个不同的形状

import turtle 
from turtle import 
color('blue', 'red') 
begin_fill() 
while True: 
    turtle.fd(150) 
    turtle.left(144) 
    if abs(pos()) < 1: 
     break 
end_fill() 
done() 

我有这首作品,其corectly,但我试图让周围转了一圈,但我似乎无法甚至使一个,这一点:

color('blue', 'red') 
begin_fill() 
while True: 
    turtle.circle(555) 
    if abs(pos()) < 1: 
     break 
end_fill() 
done() 
+1

做什么你的意思是“似乎甚至没有一个”?你的代码工作正常,它不会画出包围星星的圆。那是你要的吗? –

+0

如果您只是将第二个代码块添加到第一个代码块中,那么它将不会执行任何操作,因为第一个块末尾的done()。正如上面提到的,你的代码绘制一个圆圈就好,只是不在明星周围。 – SiHa

回答

0

你可以做这样的事情。

import turtle 
from turtle import * 
color('blue', 'red') 
begin_fill() 
while True: 
    turtle.fd(150) 
    turtle.left(144) 
    if abs(pos()) < 1: 
     break 

end_fill() 
turtle.penup() 
turtle.sety(25) 
turtle.setx(-1.2) 
turtle.right(90) 
turtle.pendown() 
turtle.circle(78) 
done() 

我觉得我的计算出错了几个小数。但我想这应该给你一个想法。

enter image description here

而且在你的代码,我看到你已经使用了一个while循环画圆。你的想法是正确的,你需要打破圆形图,但circle功能负责这一点,并自动停止,一旦到达开始位置。注意我已经使用了循环函数而不使用任何循环。

+0

感谢您的回复。我不知道你使用的一些命令,我​​现在得到他们所做的。 – lokimanor

0

意识到turtle.circle()是一个普通的多边形画图程序,我们可以更简单:

import turtle 

turtle.color('blue', 'red') 

turtle.begin_fill() 
turtle.circle(79, extent=720, steps=5) 
turtle.end_fill() 

turtle.circle(79) 

turtle.done() 

enter image description here

或者,如果你要坚持你目前的算法,我们可以这样做:

import turtle 

turtle.color('blue', 'red') 

turtle.begin_fill() 
while True: 
    turtle.fd(150) 
    turtle.left(144) 
    if abs(turtle.pos()) < 1: 
     break 
turtle.end_fill() 

turtle.setheading(-69) 

while True: 
    turtle.fd(8.25) 
    turtle.left(6) 
    if abs(turtle.pos()) < 1: 
     break 

turtle.done() 

enter image description here

相关问题