2013-10-14 65 views
2

大家好我從我的類,它要求我創建使用serperate功能的眼睛,嘴巴和頭部笑臉的問題。之後,他們希望我們畫10次,稍微相互重疊,每次重複10次左右傾斜。我知道如何做for循環,我現在的問題是傾斜。下面是我迄今爲止的內容。你能指出我傾斜的方向嗎?甲魚,傾斜對象

import turtle 
s=turtle.Screen() 
p=turtle.Turtle() 

def happymouth(p,x,y): 
    p.setheading(-60) 
    jump(p,x-60.62,y+65) 
    p.circle(70,120) 

def eyes(p,x,y): 
    jump(p,x+35,y+120) 
    p.dot(25) 
    jump(p,x-35,y+120) 
    p.dot(25) 

def jump(p,x,y): 
    p.up() 
    p.goto(x,y) 
    p.down() 


def emoticon(p,x,y): 
    p=turtle.Turtle() 
    s=turtle.Screen() 
    p.pensize(3) 
    p.setheading(0) 
    jump(p,x,y) 
    p.circle(100) 
    eyes(p,x,y) 
    happymouth(p,x,y) 
    jump(p,x,y) 
+2

是否http://docs.python.org/3.1/library/turtle.html#turtle.tilt不是你想要做什麼? – misha

+0

@misha,'turtle.tilt()'只適用於沖印,不適用於繪圖。 – cdlane

回答

2

您可以這樣做,但它需要您重新考慮您的繪圖邏輯。爲了在10度旋轉下保持表情,在繪製表情時你對龜的定位必須是相對的,而不是絕對的。沒有turtle.goto(),沒有jump(turtle, x, y)。然後,爲了使您的十個表情符合頁面,您需要進行尺寸調整,而不是絕對的。下面是這是否返工:

from turtle import Turtle, Screen 

def jump(turtle, x, y): 
    turtle.up() 
    turtle.goto(x, y) 
    turtle.down() 

def head(turtle, size): 
    # to draw circle with current position as center, have to adjust the y position 
    turtle.up() 
    turtle.right(90) 
    turtle.forward(size) 
    turtle.left(90) 
    turtle.color("black", "yellow") 
    turtle.down() 

    turtle.begin_fill() 
    turtle.circle(size) 
    turtle.end_fill() 

    # return to the center of the circle 
    turtle.up() 
    turtle.color("black") 
    turtle.left(90) 
    turtle.forward(size) 
    turtle.right(90) 
    turtle.down() 

def eyes(turtle, size): 
    turtle.up() 
    turtle.forward(0.35 * size) 
    turtle.left(90) 
    turtle.forward(0.2 * size) 
    turtle.right(90) 
    turtle.down() 

    turtle.dot(0.25 * size) 

    turtle.up() 
    turtle.backward(0.7 * size) 
    turtle.down() 

    turtle.dot(0.25 * size) 

    turtle.up() 
    turtle.forward(0.35 * size) 
    turtle.right(90) 
    turtle.forward(0.2 * size) 
    turtle.left(90) 
    turtle.down() 

def happymouth(turtle, size): 
    turtle.up() 
    turtle.left(180) 
    turtle.forward(0.6 * size) 
    turtle.left(90) 
    turtle.forward(0.35 * size) 
    turtle.left(90) 
    turtle.down() 

    turtle.right(60) 
    turtle.circle(0.7 * size, 120) 

    turtle.up() 
    turtle.circle(0.7 * size, 240) 
    turtle.left(60) 
    turtle.forward(0.6 * size) 
    turtle.left(90) 
    turtle.forward(0.35 * size) 
    turtle.right(90) 
    turtle.down() 

def emoticon(turtle, size): 
    turtle.pensize(0.03 * size) 
    head(turtle, size) 
    eyes(turtle, size) 
    happymouth(turtle, size) 

screen = Screen() 
yertle = Turtle() 

width, height = screen.window_width(), screen.window_height() 

yertle.setheading(-50) 

for xy in range(-5, 5): 
    jump(yertle, xy * width/10, xy * height/10) 

    emoticon(yertle, 60) 

    yertle.setheading(yertle.heading() + 10) 

screen.exitonclick() 

上面的代碼沒有經過優化,只要拉 - 它總是會返回到中央,以確保每一個部件是繪製相對於它。但它基本上作品:

enter image description here

有就可以解決這個問題,它允許我們使用絕對turtle.goto()但有它自己的困難完全不同的方式。我們可以將烏龜本身設置爲表情符號並將其貼在頁面上。這也讓我們忽略的相對大小,因爲龜光標都有自己的尺寸能力:

from turtle import Turtle, Screen, Shape 

def jump(turtle, x, y): 
    turtle.up() 
    turtle.goto(x, y) 
    turtle.down() 

def head(turtle, shape, x, y): 
    jump(turtle, x, y - 100) 
    turtle.begin_poly() 
    turtle.circle(100) 
    turtle.end_poly() 
    shape.addcomponent(turtle.get_poly(), "yellow", "black") 

def happymouth(turtle, shape, x, y): 
    turtle.setheading(-60) 
    jump(turtle, x - 60, y - 35) 
    turtle.begin_poly() 
    turtle.circle(70, 120) 
    turtle.end_poly() 
    shape.addcomponent(turtle.get_poly(), "black") 
    turtle.setheading(90) 

def eyes(turtle, shape, x, y): 
    jump(turtle, x + 35, y + 20) 
    turtle.begin_poly() 
    turtle.circle(13) 
    turtle.end_poly() 
    shape.addcomponent(turtle.get_poly(), "black") 

    jump(turtle, x - 35, y + 20) 
    turtle.begin_poly() 
    turtle.circle(13) 
    turtle.end_poly() 
    shape.addcomponent(turtle.get_poly(), "black") 

def emoticon(turtle, x, y): 
    shape = Shape("compound") 

    head(turtle, shape, x, y) 
    eyes(turtle, shape, x, y) 
    happymouth(turtle, shape, x, y) 

    screen.register_shape("emoticon", shape) 

screen = Screen() 
yertle = Turtle(visible="False") 
emoticon(yertle, 0, 0) 
yertle.shape("emoticon") 

yertle.clear() 

yertle.shapesize(0.6, 0.6) 

width, height = screen.window_width(), screen.window_height() 

yertle.setheading(50) 

for xy in range(-5, 5): 
    jump(yertle, xy * width/10, xy * height/10) 

    yertle.stamp() 
    yertle.setheading(yertle.heading() + 10) 

screen.exitonclick() 

不幸的是,使用turtle.*_poly()做郵票只能由封閉的多邊形,這意味着表情微笑改變有點:

enter image description here

玩得開心!