How to think like a Computer Scientist: 課後習題第九章 第4題

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     11/08/2013
# Copyright:   (c) penglaixy 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import turtle

state_num = 0

def main():
    wn = turtle.Screen()
    wn.title("Tess becomes a traffice light!")
    wn.bgcolor("lightgreen")
    tess = turtle.Turtle()
    green_tess = turtle.Turtle()
    red_tess = turtle.Turtle()
    orange_tess = turtle.Turtle()
    wn.setup(600,500)

    green_tess.hideturtle()
    orange_tess.hideturtle()
    red_tess.hideturtle()

    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()
        tess.hideturtle()
    
    def draw_traffic_light():
        green_tess.penup()
        green_tess.forward(40)
        green_tess.left(90)
        green_tess.forward(50)

        green_tess.shape("circle")
        green_tess.shapesize(3)
        green_tess.fillcolor("green")
        green_tess.pendown()

        red_tess.penup()
        red_tess.forward(40)
        red_tess.left(90)
        red_tess.forward(120)

        red_tess.shape("circle")
        red_tess.shapesize(3)
        red_tess.fillcolor("black")
        red_tess.pendown()

        orange_tess.penup()
        orange_tess.forward(40)
        orange_tess.left(90)
        orange_tess.forward(190)

        orange_tess.shape("circle")
        orange_tess.shapesize(3)
        orange_tess.fillcolor("black")
        orange_tess.pendown()


    draw_housing()
    draw_traffic_light()

    green_tess.showturtle()
    red_tess.showturtle()
    orange_tess.showturtle()

    #A traffic light is a kind of state machine whith three states,
    #green, orange, red. we number these states 0,1,2
    #when the machine changes state, we change tess's position and
    #her fillcolor.

    #This variable holds the current state of the machine

    def bye_bye():
        wn.bye()

    def advanced_state_machine():

        global state_num

        if state_num == 0:
            green_tess.fillcolor("black")
            orange_tess.fillcolor("orange")
            state_num = 1
        elif state_num == 1:
            orange_tess.fillcolor("black")
            red_tess.fillcolor("red")
            state_num = 2
        else:
            red_tess.fillcolor("black")
            green_tess.fillcolor("green")
            state_num = 0
        wn.ontimer(advanced_state_machine, 3000)

    #bind the event handler to the space key
    #wn.onkey(advanced_state_machine, "space")
    advanced_state_machine()
    wn.onkey(bye_bye, 'q')
    wn.listen()
    turtle.mainloop()

if __name__ == '__main__':
    main()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章