2016-12-03 39 views
0

我有一個程序,它使用ontimer函數將Turtle圖形停車燈中的顏色從綠色變爲黃色再變回紅色並再次變回。這裏是一個完整的程序:如何使用hide/showturtle在Python中創建一個stoplight機器?

import turtle   

turtle.setup(400,500) 
wn = turtle.Screen() 
wn.title("Tess becomes a traffic light!") 
wn.bgcolor("lightgreen") 
tess = turtle.Turtle() 


def draw_housing(): 
""" Draw a nice housing to hold the traffic lights """ 
    tess.pensize(3) 
    tess.color("black", "darkgrey") 
    tess.begin_fill() 
    tess.forward(80) 
    tess.left(90) 
    tess.forward(200) 
    tess.circle(40, 180) 
    tess.forward(200) 
    tess.left(90) 
    tess.end_fill() 


draw_housing() 

tess.penup() 

tess.forward(40) 
tess.left(90) 
tess.forward(50) 

tess.shape("circle") 
tess.shapesize(3) 
tess.fillcolor("green") 


state_num = 0 


def advance_state_machine(): 
    global state_num 
    wn.ontimer(advance_state_machine, 2000) 
    if state_num == 0:  
     tess.forward(70) 
     tess.fillcolor("orange") 
    state_num = 1 
elif state_num == 1:  
    tess.forward(70) 
    tess.fillcolor("red") 
    state_num = 2 
else:      
    tess.back(140) 
    tess.fillcolor("green") 
    state_num = 0 


advance_state_machine() 
wn.mainloop() 

但是現在,我想基本上是做同樣的事情,但是通過使用hideturtleshowturtle。我已經完全擺脫了advance_state_machine函數。取而代之的是這樣的,draw_housing()後馬上開始:

tess.penup() 
tess.hideturtle() 

green = turtle.Turtle() 
green.pu() 
green.forward(40) 
green.left(90) 
green.forward(50) 
green.shape("circle") 
green.shapesize(3) 
green.fillcolor("green") 

yellow = turtle.Turtle() 
yellow.pu() 
yellow.forward(40) 
yellow.left(90) 
yellow.forward(50) 
yellow.shape("circle") 
yellow.shapesize(3) 
yellow.forward(70) 
yellow.fillcolor("orange") 

red = turtle.Turtle() 
red.pu() 
red.forward(40) 
red.left(90) 
red.forward(190) 
red.shape("circle") 
red.shapesize(3) 
red.fillcolor("red") 

Esentially,我試圖創造一種循環隱藏和顯示每個單獨的龜模仿我的第一個程序。我可以用手寫代碼來隱藏每個烏龜並將其展示在無限遠的計時器上,但我寧願它自己繼續!另外,有沒有簡單的方法可以讓一隻燈「開」,另外兩隻是「關」,表示爲變暗/變灰的顏色,而不是那隻烏龜完全消失?預先感謝您的幫助/建議。

回答

0

我認爲你的狀態機是你程序中最好的部分,大致類似於真正的紅綠燈是如何工作的。我們將會把它放回去,但讓它改變了紅,黃,綠海龜適當:

from turtle import Turtle, Screen 

DARKRED = "#442222" 
DARKYELLOW = "#444433" 
DARKGREEN = "#224422" 

def advance_state_machine(state_num=[0]): # intentional dangerous default value 
    period = 3000 

    if state_num[0] == 0: 
     green.color("black", DARKGREEN) 
     yellow.color("black", "orange") 
     period = 1500 # yellow lights are shorter than red or green 

    elif state_num[0] == 1: 
     yellow.color("black", DARKYELLOW) 
     red.color("black", "red") 

    elif state_num[0] == 2: 
     red.color("black", DARKRED) 
     green.color("black", "green") 

    state_num[0] = (state_num[0] + 1) % 3 

    screen.ontimer(advance_state_machine, period) 

def draw_housing(turtle): 
    """ Draw a nice housing to hold the traffic lights """ 
    turtle.pensize(3) 
    turtle.color("black", "darkgrey") 
    turtle.begin_fill() 
    turtle.forward(80) 
    turtle.left(90) 
    turtle.forward(200) 
    turtle.circle(40, 180) 
    tess.forward(200) 
    turtle.left(90) 
    turtle.end_fill() 

screen = Screen() 
screen.setup(400, 500) 
screen.title("Tess becomes a traffic light!") 
screen.bgcolor("lightgreen") 

tess = Turtle() 
draw_housing(tess) 
tess.hideturtle() 

green = Turtle(shape="circle", visible=False) 
green.shapesize(3) 
green.pu() 
green.forward(40) 
green.left(90) 
green.forward(50) 
green.fillcolor("green") 
green.showturtle() 

yellow = Turtle(shape="circle", visible=False) 
yellow.shapesize(3) 
yellow.pu() 
yellow.forward(40) 
yellow.left(90) 
yellow.forward(120) 
yellow.fillcolor(DARKYELLOW) 
yellow.showturtle() 

red = Turtle(shape="circle", visible=False) 
red.shapesize(3) 
red.pu() 
red.forward(40) 
red.left(90) 
red.forward(190) 
red.fillcolor(DARKRED) 
red.showturtle() 

screen.ontimer(advance_state_machine, 3000) 

screen.mainloop() 

請注意,我們沒有改變任何給定的狀態改變了所有的燈,只需打開一個關閉,另一個。

相關問題