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


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

score_a = 0
score_b = 0

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值