How to think like a Computer Scientist: 课后习题第九章 第5题

来源:互联网 发布:费斯托选型软件 编辑:程序博客网 时间:2024/06/06 08:25
#-------------------------------------------------------------------------------# Name:        module1# Purpose:## Author:      penglaixy## Created:     11/08/2013# Copyright:   (c) penglaixy 2013# Licence:     <your licence>#-------------------------------------------------------------------------------import turtlestate_num = 0def 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:            orange_tess.fillcolor("orange")            state_num = 1            wn.ontimer(advanced_state_machine, 1000)        elif state_num == 1:            green_tess.fillcolor("black")            state_num = 2            wn.ontimer(advanced_state_machine, 1000)        elif state_num == 2:            red_tess.fillcolor("red")            orange_tess.fillcolor("black")            state_num = 3            wn.ontimer(advanced_state_machine, 2000)        else:            green_tess.fillcolor("green")            red_tess.fillcolor("black")            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()

原创粉丝点击