2017-10-19 727 views
-1

問:您的交通燈控制器程序已獲得專利,您即將變得非常富有。但是你的新客戶需要改變。他們在狀態機中需要四個狀態:綠色,然後是綠色和橙色,然後是橙色,然後是紅色。此外,他們希望在每個州花費不同的時間。機器應在綠色狀態下花費3秒,然後在綠色+橙色狀態下持續一秒,然後在橙色狀態下持續一秒,然後在紅色狀態下持續2秒。改變狀態機中的邏輯。Python turtle.ontimer(func,time)有多次?

import turtle   # Tess becomes a traffic light. 


wn = turtle.Screen() 
wn.title("Tess becomes a traffic light!") 
wn.bgcolor("lightgreen") 
tess = turtle.Turtle() 
alexis = turtle.Turtle() 


def draw_housing2(): 
    """ Draw a nice housing to hold the traffic lights """ 
    alexis.pensize(3) 
    alexis.color("black", "darkgrey") 
    alexis.penup() 
    alexis.backward(180) 
    alexis.pendown() 
    alexis.begin_fill() 
    alexis.forward(80) 
    alexis.left(90) 
    alexis.forward(275) 
    alexis.circle(40, 180) 
    alexis.forward(275) 
    alexis.left(90) 
    alexis.end_fill() 

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() 
draw_housing2() 

tess.penup() 
# Position tess onto the place where the green light should be 
tess.forward(40) 
tess.left(90) 
tess.forward(50) 
# Turn tess into a big green circle 
tess.shape("circle") 
tess.shapesize(3) 
tess.fillcolor("green") 
alexis.penup() 
alexis.forward(40) 
alexis.left(90) 
alexis.forward(50) 
alexis.shape("circle") 
alexis.shapesize(3) 
alexis.fillcolor("gray") 
bob = turtle.Turtle() 
bob.penup() 
bob.goto(alexis.position()[0], (alexis.position()[1] + 75)) 
bob.shape('circle') 
bob.shapesize(3) 
bob.fillcolor("gray") 
chris = turtle.Turtle() 
chris.penup() 
chris.goto(bob.position()[0], (bob.position()[1] + 75)) 
chris.shape('circle') 
chris.shapesize(3) 
chris.fillcolor('gray') 
jeff = turtle.Turtle() 
jeff.penup() 
jeff.goto(chris.position()[0], (chris.position()[1] + 75)) 
jeff.shape('circle') 
jeff.shapesize(3) 
jeff.fillcolor('gray') 

# A traffic light is a kind of state machine with three states, 
# Green, Orange, Red. We number these states 0, 1, 2 
# When the machine changes state, we change tess' position and 
# her fillcolor. 

# This variable holds the current state of the machine 
state_num = 0 
state_num_1 = 0 

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

def advance_state_machine2(): 
    global state_num_1 
    if state_num_1 == 0:  # Transition from state 0 to state 1 
     jeff.fillcolor('gray') 
     alexis.fillcolor('green') 
     state_num_1 = 1 
    elif state_num_1 == 1:  # Transition from state 1 to state 2 
     alexis.fillcolor('gray') 
     bob.fillcolor('green') 
     chris.fillcolor('orange') 
     state_num_1 = 2 
    elif state_num_1 == 2: 
     bob.fillcolor('gray') 
     chris.fillcolor('gray') 
     jeff.fillcolor('red') 
     state_num_1 = 0 
    wn.ontimer(advance_state_machine2, "5000") 

advance_state_machine() 
advance_state_machine2() 

wn.mainloop() 

我已經做了一切,除了改變定時器的部分。看來我只能在wn.ontimer(advanced_state_machine2,timer)語句中使用一個整數,並且當我嘗試使用列表時,它失敗了。

回答

0

只是看看在第二,四色,光,而不是使你的狀態機所有的代碼爲基礎的,你可以把它的數據結構,其中包括的時間量,以保持該狀態:

from turtle import Turtle, Screen 

def draw_housing(): 
    ''' Draw a nice housing to hold the traffic lights ''' 
    alexis.pensize(3) 
    alexis.color('black', 'darkgrey') 
    alexis.penup() 
    alexis.backward(180) 
    alexis.pendown() 
    alexis.begin_fill() 
    alexis.forward(80) 
    alexis.left(90) 
    alexis.forward(275) 
    alexis.circle(40, 180) 
    alexis.forward(275) 
    alexis.left(90) 
    alexis.end_fill() 

wn = Screen() 
wn.title('Alex becomes a traffic light!') 
wn.bgcolor('lightgreen') 

alexis = Turtle() 

draw_housing() 

alexis.shape('circle') 
alexis.penup() 
alexis.forward(40) 
alexis.left(90) 
alexis.forward(50) 
alexis.shapesize(3) 
alexis.fillcolor('gray') 

bob = Turtle('circle') 
bob.penup() 
bob.goto(alexis.xcor(), alexis.ycor() + 75) 
bob.shapesize(3) 
bob.fillcolor('gray') 

chris = Turtle('circle') 
chris.penup() 
chris.goto(bob.xcor(), bob.ycor() + 75) 
chris.shapesize(3) 
chris.fillcolor('gray') 

jeff = Turtle('circle') 
jeff.penup() 
jeff.goto(chris.xcor(), chris.ycor() + 75) 
jeff.shapesize(3) 
jeff.fillcolor('gray') 

state_num = 0 

state_machine = { \ 
    0: ('green', None, None, 'gray', 3), \ 
    1: ('gray', 'green', 'orange', None, 1), \ 
    2: (None, 'gray', None, None, 1), \ 
    3: (None, None, 'gray', 'red', 2), \ 
} 

def advance_state_machine(): 
    global state_num 

    *colors, time = state_machine[state_num] 

    for i, light in enumerate([alexis, bob, chris, jeff]): 
     if colors[i] is not None: 
      light.fillcolor(colors[i]) 

    state_num = (state_num + 1) % len(state_machine) 

    wn.ontimer(advance_state_machine, time * 1000) 

advance_state_machine() 

wn.mainloop() 

None的顏色表示沒有變化。一個稍微好一點的實現將是一組海龜/燈,而不是單獨命名的海龜,這將清理邏輯。

相關問題