2017-12-10 9 views
0

パイソン2でタートルカラーフェードを作るにはどうしたらいいですか?タートルカラーフェードを作るにはどうすればいいですか?

私は、カメで中央にゆっくりとフェードする広場を持っていたいと思います。

from turtle import Turtle 
t = Turtle() 
t.begin_fill() 
t.color('white') 
for counter in range(4): 
    t.forward(100) 
    t.right(90) 
    t.color(+1) 
t.end_fill() 
+0

「ontimer」を使用して、それを新しい色で再描画する定期的機能を実行することができます。 – furas

答えて

1

あなたは定期的に新しい色で四角形を再描画する関数を実行するためにontimer()を使用することができます。

私は ontimer()の使用に関する@furas(+1)と一致していますがrectanglar形状で作業している場合、私はあなたの四角形を表すためにカメを使用して、シンプルな実装で行くと亀さんを変えたい
import turtle as t 

def rect(c): 
    t.color(c) 
    t.begin_fill() 
    for _ in range(4): 
     t.forward(100) 
     t.right(90) 
    t.end_fill()  

def fade(): 
    global c 

    # draw   
    rect(c) 

    # change color 
    r, g, b = c 
    r += 0.1 
    g += 0.1 
    b += 0.1 
    c = (r, g, b) 

    # repeat function later 
    if r <= 1.0 and g <= 1.0 and b <= 1.0: 
     # run after 100ms 
     t.ontimer(fade, 100) 

# --- main --- 

# run fast 
t.speed(0) 

# starting color 
c = (0,0,0) # mode 1.0 color from (0.0,0.0,0.0) to (1.0,1.0,1.0) 

# start fadeing 
fade() 

# start "the engine" so `ontimer` will work 
t.mainloop() 
1

from turtle import Turtle, Screen, mainloop 

CURSOR_SIZE = 20 
DELAY = 75 
DELTA = 0.05 

def fade(turtle, gray=0.0): 

    turtle.color(gray, gray, gray) # easily upgradable to a hue 

    if gray < 1.0: 
     screen.ontimer(lambda: fade(turtle, min(gray + DELTA, 1.0)), DELAY) 
    else: 
     turtle.hideturtle() # disappear altogether 

screen = Screen() 

rectangle = Turtle('square') 
rectangle.shapesize(90/CURSOR_SIZE, 100/CURSOR_SIZE) # "draw" rectangle 

fade(rectangle) 

mainloop() 
+0

ontimerは有効なコードではありません – John

+0

@ジョン、標準の完全なPythonインストールで作業しているのですか、Trinket.ioのようなオンラインPythonで作業していますか? ? – cdlane

関連する問題