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

#-------------------------------------------------------------------------------
# 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:
            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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值