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

来源:互联网 发布:比特彗星监听端口阻塞 编辑:程序博客网 时间:2024/05/17 07:44


#-------------------------------------------------------------------------------# Name:        module1# Purpose:## Author:      penglaixy## Created:     11/08/2013# Copyright:   (c) penglaixy 2013# Licence:     <your licence>#-------------------------------------------------------------------------------import turtlescore_a = 0score_b = 0def main():    wn = turtle.Screen()    wn.title("Tess becomes a traffice light!")    wn.bgcolor("lightgreen")    tess = turtle.Turtle()    wn.setup(1000,500)    tess.penup()    tess.back(400)    tess.pendown()    tess.pensize(2)    layout = "({0:>2},{1:>2})".format(0, 0)    def print_score(score_str):        tess.showturtle()        tess.penup()        tess.right(90)        tess.forward(10)        tess.write(score_str, False, 'left', ('Arial', 14, 'normal'))        tess.backward(10)        tess.left(90)        tess.pendown()        tess.hideturtle()    print_score(layout)    def bye_bye():        wn.bye()    def display_score_a():        global score_a        global score_b        tess.showturtle()        tess.penup()        tess.forward(60)        tess.left(30)        tess.pendown()        tess.forward(50)        tess.stamp()        tess.right(30)        if score_a == -1 or (score_a == 40 and score_b < 30):            layout = "Play A win"            wn.onkey(None, 'a')            wn.onkey(None, 'b')        elif score_a == 40 and score_b == 40:            layout = "Deuce"            wn.onkey(None, 'a')            wn.onkey(None, 'b')        else:            layout = "({0:>2},{1:>2})".format(score_a, score_b)        print_score(layout)    def display_score_b():        global score_a        global score_b        tess.showturtle()        tess.penup()        tess.forward(55)        tess.right(30)        tess.pendown()        tess.forward(50)        tess.stamp()        tess.left(30)        if score_b == -1 or (score_b == 40 and score_a < 30):            layout = "Play B win"            wn.onkey(None, 'a')            wn.onkey(None, 'b')        elif score_a == 40 and score_b == 40:            layout = "Deuce"            wn.onkey(None, 'a')            wn.onkey(None, 'b')        else:            layout = "({0:>2},{1:>2})".format(score_a, score_b)        print_score(layout)    def playA_add():        global score_a        if score_a == 0:            score_a = 15        elif score_a == 15:            score_a = 30        elif score_a == 30:            score_a = 40        elif score_a == 40:            score_a = -1        display_score_a()    def playB_add():        global score_b        if score_b == 0:            score_b = 15        elif score_b == 15:            score_b = 30        elif score_b == 30:            score_b = 40        elif score_b == 40:            score_b = -1        display_score_b()    #bind the event handler to the space key    #wn.onkey(advanced_state_machine, "space")    wn.onkey(bye_bye, 'q')    wn.onkey(playA_add, 'a')    wn.onkey(playB_add, 'b')    wn.listen()    turtle.mainloop()if __name__ == '__main__':    main()


原创粉丝点击