2014-10-27 27 views
1

我有這樣的任務對於學校:使用Python烏龜,使圈wtihout圈子功能

堆雪人沒有circle功能

雪人應該是在藍色的背景,並且應當制定充滿白色。

雪人的輪廓應該是黑色的。

雪人的身體應該由3個實心圓圈組成。

每個圓的輪廓應該是3個像素寬。

底部的圓應該有一個100像素的半徑。

中圈的半徑應爲70像素。

頂部的圓應該有一個40像素的半徑。

每個圓應該居中在它下面的一個圓上(除了底圓,它可以位於任何位置)。

圓圈之間應該沒有差距。

給雪人一個嘴巴,眼睛和鼻子(一頂帽子是可選的)。

確保包括兩個手臂和每手至少兩個手指。

到目前爲止,我創造了這個,但我似乎無法得到這個圈子,然後再繼續前進。 另外,不知道如何在圈子中着色爲眼睛做點。請幫助我,第一次編碼。

import turtle        # allows us to use turtle library 
wn = turtle.Screen()      # allows us to create a graphics window 
wn.bgcolor("blue")       # sets gtaphics windows background color to blue 
import math         # allows us to use math functions 
quinn = turtle.Turtle()      # sets up turtle quinn 
quinn.setpos(0,0) 
quinn.pensize(3) 
quinn.up() 

# drawing first circle middle 
quinn.forward(70) 
quinn.down() 
quinn.left(90) 

# calculation of cicumference of a circle 
a = (math.pi*140.00/360) 

#itineration for first circle 
for i in range (1,361,1): 
    quinn.left(a) 
    quinn.forward (1) 

# drawing second circle bottom 
quinn.up() 
quinn.home() 
quinn.right(90) 
quinn.forward(70) 
quinn.left(90) 
quinn.down() 

b = (math.pi*200.00/360) 

for i in range (1,361,1): 
    quinn.right(b) 
    quinn.forward(1) 

# drawing third circle head top 

quinn.up() 
quinn.goto(0,70) 
quinn.right(90) 
quinn.down() 

c =(math.pi*80/360) 

for i in range (1,361,1): 
    quinn.left(c) 
    quinn.forward(1) 

wn.exitonclick() 
+1

除非你的教授特別要求,它被認爲是不好的編碼風格,評論每行。特別是當線很明顯的時候。 – IanAuld 2014-10-27 04:11:16

+0

另外,自己命名你的龜似乎...困惑?小說中的一些着名的烏龜是米開朗基羅和甘托克。絕對是一隻海豚龜。 – tripleee 2014-10-27 05:37:37

回答

1

以下是一個示例函數來繪製填充在藍色圓:圓心

def draw_circle(radius):  
    turtle.up() 
    turtle.goto(0,radius) # go to (0, radius) 
    turtle.begin_fill() # start fill 
    turtle.down() # pen down 
    turtle.color('blue') 
    times_y_crossed = 0 
    x_sign = 1.0 
    while times_y_crossed <= 1: 
     turtle.forward(2*math.pi*radius/360.0) # move by 1/360 
     turtle.right(1.0) 
     x_sign_new = math.copysign(1, turtle.xcor())   
     if(x_sign_new != x_sign): 
      times_y_crossed += 1 
     x_sign = x_sign_new 
    turtle.up() # pen up 
    turtle.end_fill() # end fill. 
    return 

然後,可以修改上述功能附加參數爲位置(x,y)的:

def draw_circle(radius, x, y):  
    turtle.up() 
    turtle.goto(x,y+radius) # go to (x, y + radius) 
    turtle.begin_fill() # start fill 
    turtle.down() # pen down 
    turtle.color('blue') 
    times_y_crossed = 0 
    x_sign = 1.0 
    while times_y_crossed <= 1: 
     turtle.forward(2*math.pi*radius/360.0) # move by 1/360 
     turtle.right(1.0) 
     x_sign_new = math.copysign(1, turtle.xcor())   
     if(x_sign_new != x_sign): 
      times_y_crossed += 1 
     x_sign = x_sign_new 
    turtle.up() # pen up 
    turtle.end_fill() # end fill. 
    return 

您可以輕鬆地添加點作爲,例如,:

turtle.goto(-20,10) 
turtle.color('red') 
turtle.dot(20) 
turtle.goto(40,10) 
turtle.dot(20) 

放在一起:

import turtle 
import math 

def draw_circle(radius, x, y):  
    turtle.up() 
    turtle.goto(x,y+radius) # go to (0, radius) 
    turtle.begin_fill() # start fill 
    turtle.down() # pen down 
    turtle.color('blue') 
    times_y_crossed = 0 
    x_sign = 1.0 
    while times_y_crossed <= 1: 
     turtle.forward(2*math.pi*radius/360.0) # move by 1/360 
     turtle.right(1.0) 
     x_sign_new = math.copysign(1, turtle.xcor())   
     if(x_sign_new != x_sign): 
      times_y_crossed += 1 
     x_sign = x_sign_new 
    turtle.up() # pen up 
    turtle.end_fill() # end fill. 
    return 


draw_circle(100, 10, 10) 
turtle.goto(-20,10) 
turtle.color('red') 
turtle.dot(20) 
turtle.goto(40,10) 
turtle.dot(20) 
turtle.pen(shown=False) 
turtle.done() 

你應該嘗試自己做作業..的剩餘部分;)

+0

@Quinn:有幫助嗎? – gmas80 2014-10-27 04:26:04

0

對不起,沒有給予解釋。 第一部分是Ramanujan的pi的aproximation,但不是一個很好的一個,因爲它只能在循環300,000次迭代之後達到pi的aproximation,並且它只能精確到5個小數位。這將是這部分:

r += (1/k) * (-1)**i 
    pi = (4 * (1 - r)) 

然後我用一個圓的周長:

t.forward(2*5*pi) 

最後,我只是讓烏龜走順時針由20

import turtle 
t = turtle.Turtle() 
t.right(90) 
t.penup() 
t.goto(100, 0) 
t.pendown() 

i = 0 
r = 0 
k = 3 

while i <= 360: 
    r += (1/k) * (-1)**i 
    pi = (4 * (1 - r)) 
    t.write(pi) 
    t.forward(2*5*pi) 
    t.right(20) 

    i += 1 
    k += 2 

turtle.done() 
+1

雖然這段代碼片段是受歡迎的,並且可能會提供一些幫助,但它會[如果它包含解釋](/ meta.stackexchange.com/q/114762)*如何解決該問題[將會有很大改進。沒有這些,你的答案就沒有什麼教育價值了 - 記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 – 2017-05-04 17:26:58

0

大多數解決方案到「沒有烏龜圈功能」涉及將自己的等價物寫入烏龜的圈函數。但是現在已經有兩種其他的方式可以繪製出輪廓豐滿的烏龜圈。

一個是你可以使用同心

turtle.color('black') 
turtle.dot(100) 
turtle.color('white') 
turtle.dot(100 - 6) 

只要記住,dot()需要一個直徑,而circle()需要一個半徑:

enter image description here

不過,我更喜歡使用衝壓來解決這些問題:

''' Build a Snowman without turtle circle function ''' 

from turtle import Turtle, Screen 

# The snowman’s body should be made of 3 filled circles. 

# The bottom circle should have a radius of 100 pixels. 
# The middle circle should have a radius of 70 pixels. 
# The top circle should have a radius of 40 pixels. 

RADII = (100, 70, 40) 

STAMP_SIZE = 20 

# The snowman should be on a blue background 
screen = Screen() 
screen.bgcolor('blue') 

quinn = Turtle('circle') 
quinn.setheading(90) 
quinn.up() 

# The outline of the snowman should be in black, and should be drawn filled with white. 
quinn.color('black', 'white') 

for not_first, radius in enumerate(RADII): 

    if not_first: 
     quinn.forward(radius) 

    # The outline of each circle should be 3 pixels wide. 
    quinn.shapesize((radius * 2)/STAMP_SIZE, outline=3) 

    quinn.stamp() 

    # Each circle should be centered above the one below it 
    # There should be no gap between the circles. 
    quinn.forward(radius) 

# Give the snowman eyes 

quinn.shapesize(15/STAMP_SIZE) 
quinn.color('black') 
quinn.backward(3 * RADII[-1]/4) 

for x in (-RADII[-1]/3, RADII[-1]/3): 
    quinn.setx(x) 
    quinn.stamp() 

# Give the snowman a mouth, and a nose (a hat is optional). 

pass 

# Make sure to include two stick-arms and at least two fingers on each hand. 

pass 

quinn.hideturtle() 

screen.exitonclick() 

這個想法是你扭曲烏龜光標本身到你所需要的,在屏幕上做一個快照,然後將它扭曲到你需要繪製的下一個東西。

enter image description here