用Python编一个小游戏

**用Python编一个小游戏
`

import random
import time
print('''欢迎来到「曾曾的世界」!
曾曾对你的到来表示十二万分的兴奋w''')
time.sleep(1.5)
print('--------------------------')
print('''今天空气真好呢!
或许...''')
time.sleep(1.5)
print('你想和曾曾一起做游戏嘛(星星眼)')
time.sleep(1.5)
print('--------------------------')
print('【请选择】')
print('(曾曾温馨提示:要输入大写的字母鸭)')
a1 = input('A.想      B.不想\n')
if a1 == 'B' :
    print('真遗憾,下次有缘再见咯~')
    time.sleep(2)
elif a1 == 'A':
    while True:
        print('(σ´∀`)σ 曾曾超开心,并努力抑制喜悦于言表')
        time.sleep(1.5)
        print('那么,你想和曾曾玩儿什么呢')
        a2 = input('A.猜数字     B.打小怪兽     C.大人的游戏     D.计算器     E.画画     F.打砖块\n')
        if a2 == 'A' :
            import random

            n = random.randint(1, 100)
            k = int(input("请输入数字:"))
            if k > n:
                while True:
                    print("大了大了")
                    k = int(input("请输入数字:"))
            elif k < n:
                while print("小了小了"):
                    k = int(input("请输入数字:"))
            elif k == n:
                print("恭喜你猜对了")
        elif a2 == 'B' :
            import random
            import time

            while True:
                input("请按回车键开始游戏")
                print("遇到一只小怪兽~~~")
                print("选择是否打小怪兽?")
                w = input("A:打小怪兽  B:不打小怪兽\n")

                print("你的选择是:", w)

                if w == "A":
                    p_b = random.randint(50, 100)
                    p_a = random.randint(50, 150)
                    time.sleep(1)
                    print('\n----------------- 你的信息 -----------------')
                    print("您的血量为:", p_b)
                    print("您的攻击力为:", p_a)
                    input('请按回车键为您匹配小怪兽。')
                    print('\n----------------- 小怪兽信息 -----------------')
                    m_b = random.randint(50, 150)
                    m_a = random.randint(40, 100)
                    time.sleep(1)
                    print("小怪兽的血量为:", m_b)
                    print("小怪兽的攻击力为:", m_a)
                    time.sleep(1)

                    print('\n--------------- 【开始第1局攻击】 ---------------')
                    input('请按回车键进行攻击。')
                    print("正在攻击,请稍后~~~~~~~~~~")
                    time.sleep(5)
                    p_b -= m_a
                    m_b -= p_a

                    print('\n-------------- 【第1局攻击结束】 ---------------')
                    print("您的剩余血量为:", p_b)
                    print("小怪兽的剩余血量为:", m_b)
                    print("是否继续进行攻击???")
                    b = input('A:继续进行攻击        B.放弃攻击\n')
                    print('你的选择是:', b)
                    if b == "A":
                        print("------------ 【第2局攻击开始】  ----------------")
                        print("正在攻击,请稍后~~~~~~~~~~")
                        time.sleep(5)
                        R = random.randint(20, 150)
                        T = random.randint(20, 150)
                        p_b -= m_a
                        m_b -= p_a + T
                        print('\n-------------- 【第2局攻击结束】 --------------')
                        print("紧不紧张,请按回车键查看结果")
                        input()
                        print("您的剩余血量为:", p_b)
                        print("小怪兽的剩余血量为:", m_b)
                        if p_b > m_b:
                            print("哈哈,你赢了,真厉害!")
                            print("A:退出游戏       B:重新再来")
                            s = input('请选择:\n')
                            if s == "A":
                                break
                            else:
                                print("准备中~~~~~~~~请稍后")
                                time.sleep(10)
                                "\n"
                        elif p_b == m_b:
                            if p_a > m_a:
                                print("哈哈,你赢了,真厉害!")
                                print("A:退出游戏       B:重新再来")
                                s = input('请选择:\n')
                                if s == "A":
                                    break
                                else:
                                    print("准备中~~~~~~~稍后")
                                    time.sleep(10)
                                    "\n"
                            else:
                                print("很遗憾,你输了,继续加油!")
                                print("A:退出游戏       B:重新再来")
                                s = input('请选择;\n')
                                if s == "A":
                                    break
                                else:
                                    print("准备中-----请稍后")
                                    time.sleep(10)
                                    "\n"
                        else:
                            print("很遗憾,你输了,继续加油!")
                            print("A:退出游戏       B:重新再来")
                            s = input('请选择:\n')
                            if s == "A":
                                break
                            else:
                                print("准备中~~~~~~~~请稍后")
                                time.sleep(5)
                                "\n  "
                    else:
                        print("放弃攻击。选择保命")
                        break
                else:
                    print("保护怪兽,借道逃跑。")
                    break
        elif a2 == 'C' :
            print('''诶诶诶///\n大人的游戏是什么辣(捂脸)\n曾曾只和有缘的人玩这个游戏哦''')
            time.sleep(1.5)
            name = input('请输入你的名字:')
            if name == '343434':
                print('如果是343434的话///可以噢')
                print('咕嘿嘿嘿嘿嘿嘿')
            else:
                print('诶,真遗憾。你和曾曾不是有缘人呢')
            time.sleep(1.5)
        elif a2=="D":
            n = int(input("请输入第一个数:"))
            m = int(input("请输入第二个数:"))
            z: int = (input("请输入操作符:"))
            if z == ("+"):
                print(n, z, m, "=", n + m)
            elif z == ("-"):
                print(n, z, m, "=", n - m)
            elif z == ("//"):
                print(n, z, m, "=", n // m)
            elif z == ("*"):
                print(n, z, m, "=", n * m)
            elif z == ("/"):
                print(n, z, m, "=", n / m)
            elif z == ("**"):
                print(n, z, m, "=", n ** m)
            else:
                print(n, z, m, "=", n % m)
        elif a2=="E":
            print("你要让曾曾画什么?")
            q=input("A:皮卡丘    B:樱花树     C:美国队长的盾牌      D:笑脸      E:奥运五环    F:LOVE   G:哆啦A梦\n")
            if q=="A":
                z=input("A:皮卡丘1    B:皮卡丘2\n")
                if z=="A":

                    import turtle as t


                    def infoPrt():
                        print('coordinate: ' + str(t.pos()))
                        print('angle: ' + str(t.heading()))


                    t.pensize(3)
                    t.hideturtle()
                    t.colormode(255)
                    t.color("black")
                    t.setup(700, 650)
                    t.speed(10)
                    t.st()
                    # t.dot()
                    t.pu()
                    # t.goto(-150,100)
                    t.goto(-210, 86)
                    t.pd()
                    infoPrt()

                    # 头
                    print('头')
                    t.seth(85)
                    t.circle(-100, 50)
                    # t.seth(78)
                    # t.circle(-100,25)

                    infoPrt()

                    t.seth(25)
                    t.circle(-170, 50)
                    infoPrt()

                    # 右耳
                    print('右耳')
                    t.seth(40)
                    # t.circle(-250,52)
                    t.circle(-250, 30)
                    infoPrt()

                    # 右耳尖
                    t.begin_fill()
                    # 左
                    t.circle(-250, 22)
                    # t.fillcolor("pink")
                    # 右
                    t.seth(227)
                    t.circle(-270, 15)

                    prePos = t.pos()
                    infoPrt()
                    # 下
                    t.seth(105)
                    t.circle(100, 32)
                    t.end_fill()

                    t.pu()
                    t.setpos(prePos)
                    t.pd()
                    t.seth(212)
                    t.circle(-270, 28)

                    prePos = t.pos()
                    t.pu()
                    t.goto(t.xcor() + 5, t.ycor() - 2)
                    t.pd()

                    # 躯干
                    print('躯干')
                    t.seth(280)
                    t.circle(500, 30)
                    infoPrt()

                    # 臀部
                    print('臀部')
                    t.seth(120)
                    # t.circle(150, -55)
                    t.circle(150, -11)
                    p_tail = t.pos()
                    t.circle(150, -44)
                    p_butt = t.pos()
                    infoPrt()

                    # 尾巴
                    t.pu()
                    t.setpos(p_tail)
                    t.pd()

                    t.begin_fill()
                    t.seth(50)
                    t.fd(25)
                    t.seth(-50)
                    t.fd(30)
                    p_tail1 = t.pos
                    t.seth(-140)
                    t.fd(36)
                    t.end_fill()
                    t.seth(39)
                    # 右尾和h1
                    t.fd(72)
                    # 右尾和v1
                    t.seth(125)
                    t.fd(48)
                    # 右尾和h2
                    t.seth(40)
                    t.fd(53)
                    # 右尾和v2
                    t.seth(88)
                    t.fd(45)
                    # 右尾和h3
                    t.seth(35)
                    t.fd(105)
                    # 右尾和v3
                    t.seth(105)
                    t.circle(850, 8)
                    # t.fd(105)
                    t.seth(215)
                    # t.fd(125)
                    t.circle(850, 11)
                    t.seth(280)
                    t.fd(110)
                    t.seth(220)
                    t.fd(50)
                    t.seth(309)
                    t.fd(56)

                    # 底盘
                    print('底盘')
                    t.pu()
                    t.setpos(p_butt)
                    t.pd()
                    t.seth(20)
                    t.circle(120, -45)
                    infoPrt()

                    t.seth(330)
                    t.circle(-150, -30)
                    infoPrt()

                    prePos = t.pos()
                    t.pu()
                    t.goto(t.xcor() + 20, t.ycor())
                    t.pd()

                    t.seth(230)
                    t.circle(-70, 120)
                    p_bot = t.pos()

                    # 两脚-right
                    t.pu()
                    t.setpos(p_butt)
                    t.setpos(t.xcor() + 5, t.ycor() + 5)
                    t.pd()
                    t.seth(-86)
                    t.fd(30)
                    t.seth(-93)
                    t.fd(33)
                    t.seth(-225)
                    t.circle(-150, 22)
                    # 两脚-left
                    t.pu()
                    t.setpos(p_bot)
                    t.setpos(t.xcor() + 85, t.ycor() - 43)
                    t.pd()
                    t.seth(-105)
                    t.fd(50)
                    t.seth(-225)
                    t.circle(-150, 22)

                    # 左躯干
                    print('躯干')
                    t.pu()
                    t.setpos(p_bot)
                    t.pd()
                    t.seth(90)
                    t.circle(450, 13)
                    p_lfhd = t.pos()
                    t.circle(450, 5)
                    t.pu()
                    t.circle(450, 5)
                    t.pd()
                    t.circle(450, 6)
                    infoPrt()

                    # 左脸
                    print('左脸')
                    t.seth(330)
                    t.circle(50, -90)
                    infoPrt()

                    # 左酒窝
                    t.seth(30)
                    t.circle(-15, 120)
                    t.seth(-70)
                    t.circle(-30, 90)

                    # 左手
                    t.pu()
                    t.setpos(p_lfhd)
                    t.pd()
                    t.seth(160)
                    t.circle(150, 30)
                    infoPrt()

                    t.seth(180)
                    t.circle(-30, 150)
                    t.fd(67)

                    t.pu()
                    t.setpos(t.xcor() - 40, t.ycor() - 60)
                    t.pd()
                    t.seth(200)
                    t.circle(-5, 180)

                    # 右手
                    t.pu()
                    t.setpos(p_lfhd)
                    t.setpos(t.xcor() + 180, t.ycor() + 5)
                    t.pd()
                    t.seth(200)
                    t.circle(-50, 100)
                    t.pu()
                    t.circle(-50, 15)
                    t.pd()
                    t.circle(-50, 65)
                    t.pu()
                    t.setpos(t.xcor() + 10, t.ycor() - 45)
                    t.pd()
                    # t.seth(270)
                    # t.circle(-30, -180)
                    t.seth(80)
                    t.fd(10)
                    t.seth(165)
                    t.circle(10, 60)
                    t.seth(90)
                    t.fd(5)
                    t.seth(165)
                    t.circle(10, 60)
                    t.seth(95)
                    t.fd(5)
                    t.seth(185)
                    t.circle(10, 60)
                    t.seth(105)
                    t.fd(10)
                    t.seth(230)
                    t.fd(20)
                    t.seth(145)
                    t.fd(10)
                    t.seth(285)
                    t.fd(20)

                    # 右酒窝
                    t.pu()
                    t.setpos(t.xcor() - 40, t.ycor() + 110)
                    t.pd()
                    t.circle(27, 360)

                    # 嘴
                    t.pu()
                    t.setpos(t.xcor() - 30, t.ycor() + 28)
                    t.pd()
                    t.seth(280)
                    t.circle(-130, 30)
                    t.seth(270)
                    t.circle(-6, 160)
                    t.seth(130)
                    t.circle(-130, 30)
                    t.pu()
                    t.setpos(t.xcor() - 5, t.ycor() + 5)
                    t.pd()
                    t.seth(160)
                    t.circle(-20, -70)
                    t.seth(160)
                    t.circle(-30, -60)
                    t.pu()
                    t.setpos(t.xcor(), t.ycor() - 28)
                    t.pd()
                    t.seth(200)
                    t.circle(50, 58)

                    # 左眼
                    t.pu()
                    t.setpos(t.xcor() - 40, t.ycor() + 90)
                    t.pd()
                    t.circle(5)
                    t.pu()
                    t.setpos(t.xcor() + 5, t.ycor() + 10)
                    t.pd()
                    t.begin_fill()
                    t.seth(190)
                    t.circle(15, 130)
                    t.seth(310)
                    t.circle(10, 15)
                    t.seth(0)
                    t.circle(17, 133)
                    t.seth(90)
                    t.circle(10, 15)
                    t.end_fill()
                    t.pu()
                    t.setpos(t.xcor() + 2, t.ycor() - 15)
                    t.pd()
                    t.color("white")
                    t.begin_fill()
                    t.circle(5)
                    t.end_fill()

                    # 右眼
                    t.pu()
                    t.setpos(t.xcor() + 85, t.ycor() + 15)
                    t.pd()
                    t.color("black")
                    t.circle(5)
                    t.pu()
                    t.setpos(t.xcor() + 5, t.ycor() + 10)
                    t.pd()
                    t.begin_fill()
                    t.seth(190)
                    t.circle(20, 130)
                    t.seth(310)
                    t.circle(10, 15)
                    t.seth(0)
                    t.circle(22, 133)
                    t.seth(90)
                    t.circle(13, 15)
                    t.end_fill()
                    t.pu()
                    t.setpos(t.xcor() - 7, t.ycor() - 15)
                    t.pd()
                    t.color("white")
                    t.begin_fill()
                    t.circle(7)
                    t.end_fill()

                    # 左耳
                    t.color("black")
                    t.pu()
                    t.goto(-210, 86)
                    t.setpos(t.xcor() + 15, t.ycor() + 38)
                    t.pd()
                    t.seth(90)
                    t.circle(-250, 30)

                    t.begin_fill()
                    # 左
                    t.circle(-250, 18)

                    # 右
                    t.seth(270)
                    t.circle(-270, 12)

                    prePos = t.pos()
                    # 下
                    t.seth(180)
                    t.circle(100, 30)
                    t.end_fill()

                    t.pu()
                    t.setpos(prePos)
                    t.pd()
                    t.seth(270)
                    t.circle(-270, 18)

                    t.done()
                elif z=="B":
                    import turtle as t


                    def nose():
                        t.penup()
                        t.seth(90)
                        t.fd(100)
                        t.pendown()
                        t.begin_fill()
                        t.fillcolor('black')
                        t.seth(45)
                        t.fd(25)
                        t.seth(135)
                        t.circle(25, 90)
                        t.seth(315)
                        t.fd(25)
                        t.end_fill()


                    def eyes(seth, fd, c):
                        t.penup()
                        t.seth(seth)
                        t.fd(fd)
                        t.pendown()
                        t.begin_fill()
                        t.fillcolor('black')
                        t.circle(50)
                        t.end_fill()

                        t.penup()
                        t.circle(50, c)
                        t.pendown()
                        t.begin_fill()
                        t.fillcolor('white')
                        t.circle(20)
                        t.end_fill()


                    def face(seth, fd):
                        t.penup()
                        t.seth(seth)
                        t.fd(fd)
                        t.pendown()
                        t.begin_fill()
                        t.fillcolor('red')
                        t.circle(70)
                        t.end_fill()


                    def lip():
                        t.penup()
                        t.seth(135)
                        t.fd(250)
                        t.pendown()
                        t.seth(-300)
                        t.circle(30, -65)
                        t.begin_fill()
                        t.fillcolor('Firebrick')
                        t.seth(165)
                        t.fd(140)
                        t.seth(195)
                        t.fd(140)
                        t.seth(-360)
                        t.circle(30, -65)
                        t.penup()
                        t.seth(-60)
                        t.circle(30, 65)
                        t.pendown()
                        t.seth(-70)
                        t.fd(240)
                        t.circle(55, 140)
                        t.seth(70)
                        t.fd(240)
                        t.end_fill()
                        t.seth(-110)
                        t.fd(80)
                        t.begin_fill()
                        t.fillcolor('Firebrick1')
                        t.seth(120)
                        t.circle(120, 123)
                        t.seth(-70)
                        t.fd(165)
                        t.circle(55, 140)
                        t.seth(72)
                        t.fd(165)
                        t.end_fill()


                    def setting():
                        t.pensize(4)
                        t.hideturtle()
                        t.setup(1000, 600)
                        t.speed(10)
                        t.screensize(bg='yellow')


                    def main():
                        setting()
                        nose()
                        eyes(160, 250, 60)
                        eyes(-9.5, 530, 230)
                        face(195, 600)
                        face(-11, 720)
                        lip()
                        t.done()


                    if __name__ == '__main__':
                        main()
            elif q=="B":
                import turtle as T
                import random
                import time


                # 画樱花的躯干(60,t)
                def Tree(branch, t):
                    time.sleep(0.0005)
                    if branch > 3:
                        if 8 <= branch <= 12:
                            if random.randint(0, 2) == 0:
                                t.color('snow')  # 白
                            else:
                                t.color('lightcoral')  # 淡珊瑚色
                            t.pensize(branch / 3)
                        elif branch < 8:
                            if random.randint(0, 1) == 0:
                                t.color('snow')
                            else:
                                t.color('lightcoral')  # 淡珊瑚色
                            t.pensize(branch / 2)
                        else:
                            t.color('sienna')  # 赭(zhě)色
                            t.pensize(branch / 10)  # 6
                        t.forward(branch)
                        a = 1.5 * random.random()
                        t.right(20 * a)
                        b = 1.5 * random.random()
                        Tree(branch - 10 * b, t)
                        t.left(40 * a)
                        Tree(branch - 10 * b, t)
                        t.right(20 * a)
                        t.up()
                        t.backward(branch)
                        t.down()


                # 掉落的花瓣
                def Petal(m, t):
                    for i in range(m):
                        a = 200 - 400 * random.random()
                        b = 10 - 20 * random.random()
                        t.up()
                        t.forward(b)
                        t.left(90)
                        t.forward(a)
                        t.down()
                        t.color('lightcoral')  # 淡珊瑚色
                        t.circle(1)
                        t.up()
                        t.backward(a)
                        t.right(90)
                        t.backward(b)


                # 绘图区域
                t = T.Turtle()
                # 画布大小
                w = T.Screen()
                t.hideturtle()  # 隐藏画笔
                t.getscreen().tracer(5, 0)
                w.screensize(bg='wheat')  # wheat小麦
                t.left(90)
                t.up()
                t.backward(150)
                t.down()
                t.color('sienna')

                # 画樱花的躯干
                Tree(60, t)
                # 掉落的花瓣
                Petal(200, t)
                w.exitonclick()
                Petal(400, t)
                w.exitonclick()
            elif q=="C":
                import turtle as t

                t.pencolor("red")
                t.hideturtle()
                t.pensize(25)
                t.pu()
                t.goto(0, -150)
                t.pendown()
                t.circle(150)
                t.pu()
                t.goto(0, -100)
                t.pendown()
                t.fillcolor('blue')
                t.begin_fill()
                t.circle(100)
                t.end_fill()
                t.pu()
                t.goto(-85, 25)
                t.pendown()
                t.color("white")
                t.pensize(1)
                t.begin_fill()
                for i in range(5):
                    t.fd(165)
                    t.right(144)
                t.end_fill()
            elif q=="D":
                import turtle as t

                t.shape("turtle")
                t.pensize(8)
                t.fillcolor("yellow")
                t.pencolor("yellow")
                t.begin_fill()
                t.penup()
                t.goto(0, -100)
                t.pendown()
                t.circle(200)
                t.end_fill()
                t.penup()
                t.goto(-90, 100)
                t.pencolor("white")
                t.pendown()
                t.circle(30)
                t.penup()
                t.goto(90, 100)
                t.pendown()
                t.circle(30)
                t.penup()
                t.goto(-70, -10)
                t.pendown()

                t.left(90)
                t.pencolor("red")

                t.circle(-60, -200)
                t.end_fill()
                t.penup()
                t.goto(-100, 130)
                t.pendown()
                t.pencolor("brown")
                t.fillcolor("brown")
                t.pensize(1)
                t.begin_fill()
                t.circle(10)
                t.end_fill()
                t.penup()
                t.goto(90, 130)
                t.pendown()
                t.fillcolor("brown")
                t.begin_fill()
                t.circle(10)
                t.end_fill()
            elif q=="E":
                import turtle

                turtle.shape("turtle")
                turtle.pensize(10)
                turtle.penup()
                turtle.goto(-250, 20)
                turtle.pendown()
                turtle.pencolor("blue")
                turtle.circle(100)
                turtle.penup()
                turtle.goto(-40, 20)
                turtle.pendown()
                turtle.pencolor("black")
                turtle.circle(100)
                turtle.penup()
                turtle.goto(170, 20)
                turtle.pendown()
                turtle.pencolor("red")
                turtle.circle(100)
                turtle.penup()
                turtle.goto(-150, -100)
                turtle.pendown()
                turtle.pencolor("yellow")
                turtle.circle(100)
                turtle.penup()
                turtle.goto(60, -100)
                turtle.pendown()
                turtle.pencolor("green")
                turtle.circle(100)
                turtle.penup()
            elif q=="F":
                import turtle as t

                t.pensize(1)
                t.pencolor("red")
                t.pu()
                t.goto(-140, 100)
                t.right(90)
                t.pd()
                t.fd(150)
                t.left(90)
                t.fd(80)
                t.pu()
                t.goto(-50, 50)
                t.left(-180)
                t.pd()
                t.circle(50)
                t.pu()
                t.goto(-10, 100)
                t.left(110)
                t.pd()
                t.fd(150)
                t.right(-140)
                t.fd(150)
                t.pu()
                t.goto(110, 20)
                t.left(-70)
                t.pd()
                t.fd(50)
                t.fd(-50)
                t.left(90)
                t.fd(50)
                t.left(-90)
                t.fd(50)
                t.fd(-50)
                t.left(-90)
                t.fd(100)
                t.right(-90)
                t.fd(50)
                import turtle


                # 画心形圆弧
                def hart_arc():
                    for i in range(200):
                        turtle.right(1)
                        turtle.forward(2)


                def move_pen_position(x, y):
                    turtle.hideturtle()  # 隐藏画笔(先)
                    turtle.up()  # 提笔
                    turtle.goto(x, y)  # 移动画笔到指定起始坐标(窗口中心为0,0)
                    turtle.down()  # 下笔
                    turtle.showturtle()  # 显示画笔


                # 初始化
                turtle.setup(width=800, height=500)  # 窗口(画布)大小
                turtle.color('red', 'pink')  # 画笔颜色
                turtle.pensize(3)  # 画笔粗细
                turtle.speed(1)  # 描绘速度
                # 初始化画笔起始坐标
                move_pen_position(x=0, y=-180)  # 移动画笔位置
                turtle.left(140)  # 向左旋转140度
                # 画心形直线( 左下方 )
                turtle.forward(224)  # 向前移动画笔,长度为224
                # 画爱心圆弧
                hart_arc()  # 左侧圆弧
                turtle.left(120)  # 调整画笔角度
                hart_arc()  # 右侧圆弧
                # 画心形直线( 右下方 )
                turtle.forward(224)
                # 点击窗口关闭程序
                window = turtle.Screen()
                window.exitonclick()
            elif q=="G":
                import turtle as t

                t.speed(10)
                t.pensize(8)
                t.hideturtle()
                t.screensize(500, 500, bg='white')
                # 猫脸
                t.fillcolor('#00A1E8')
                t.begin_fill()
                t.circle(120)
                t.end_fill()
                t.pensize(3)
                t.fillcolor('white')
                t.begin_fill()
                t.circle(100)
                t.end_fill()
                t.pu()
                t.home()
                t.goto(0, 134)
                t.pd()
                t.pensize(4)
                t.fillcolor("#EA0014")
                t.begin_fill()
                t.circle(18)
                t.end_fill()
                t.pu()
                t.goto(7, 155)
                t.pensize(2)
                t.color('white', 'white')
                t.pd()
                t.begin_fill()
                t.circle(4)
                t.end_fill()
                t.pu()
                t.goto(-30, 160)
                t.pensize(4)
                t.pd()
                t.color('black', 'white')
                t.begin_fill()
                a = 0.4
                for i in range(120):
                    if 0 <= i < 30 or 60 <= i < 90:
                        a = a + 0.08
                        t.lt(3)  # 向左转3度
                        t.fd(a)  # 向前走a的步长
                    else:
                        a = a - 0.08
                        t.lt(3)
                        t.fd(a)
                t.end_fill()
                t.pu()
                t.goto(30, 160)
                t.pensize(4)
                t.pd()
                t.color('black', 'white')
                t.begin_fill()
                for i in range(120):
                    if 0 <= i < 30 or 60 <= i < 90:
                        a = a + 0.08
                        t.lt(3)  # 向左转3度
                        t.fd(a)  # 向前走a的步长
                    else:
                        a = a - 0.08
                        t.lt(3)
                        t.fd(a)
                t.end_fill()
                t.pu()
                t.goto(-38, 190)
                t.pensize(8)
                t.pd()
                t.right(-30)
                t.forward(15)
                t.right(70)
                t.forward(15)
                t.pu()
                t.goto(15, 185)
                t.pensize(4)
                t.pd()
                t.color('black', 'black')
                t.begin_fill()
                t.circle(13)
                t.end_fill()
                t.pu()
                t.goto(13, 190)
                t.pensize(2)
                t.pd()
                t.color('white', 'white')
                t.begin_fill()
                t.circle(5)
                t.end_fill()
                t.pu()
                t.home()
                t.goto(0, 134)
                t.pensize(4)
                t.pencolor('black')
                t.pd()
                t.right(90)
                t.forward(40)
                t.pu()
                t.home()
                t.goto(0, 124)
                t.pensize(3)
                t.pencolor('black')
                t.pd()
                t.left(10)
                t.forward(80)
                t.pu()
                t.home()
                t.goto(0, 114)
                t.pensize(3)
                t.pencolor('black')
                t.pd()
                t.left(6)
                t.forward(80)
                t.pu()
                t.home()
                t.goto(0, 104)
                t.pensize(3)
                t.pencolor('black')
                t.pd()
                t.left(0)
                t.forward(80)
                # 左边的胡子
                t.pu()
                t.home()
                t.goto(0, 124)
                t.pensize(3)
                t.pencolor('black')
                t.pd()
                t.left(170)
                t.forward(80)
                t.pu()
                t.home()
                t.goto(0, 114)
                t.pensize(3)
                t.pencolor('black')
                t.pd()
                t.left(174)
                t.forward(80)
                t.pu()
                t.home()
                t.goto(0, 104)
                t.pensize(3)
                t.pencolor('black')
                t.pd()
                t.left(180)
                t.forward(80)
                t.pu()
                t.goto(-70, 70)
                t.pd()
                t.color('black', 'red')
                t.pensize(6)
                t.seth(-60)
                t.begin_fill()
                t.circle(80, 40)
                t.circle(80, 80)
                t.end_fill()
                t.pu()
                t.home()
                t.goto(-80, 70)
                t.pd()
                t.forward(160)
                t.pu()
                t.home()
                t.goto(-50, 50)
                t.pd()
                t.pensize(1)
                t.fillcolor("#eb6e1a")
                t.seth(40)
                t.begin_fill()
                t.circle(-40, 40)
                t.circle(-40, 40)
                t.seth(40)
                t.circle(-40, 40)
                t.circle(-40, 40)
                t.seth(220)
                t.circle(-80, 40)
                t.circle(-80, 40)
                t.end_fill()
                # 领带
                t.pu()
                t.goto(-70, 12)
                t.pensize(14)
                t.pencolor('red')
                t.pd()
                t.seth(-20)
                t.circle(200, 30)
                t.circle(200, 10)
                # 铃铛
                t.pu()
                t.goto(0, -46)
                t.pd()
                t.pensize(3)
                t.color("black", '#f8d102')
                t.begin_fill()
                t.circle(25)
                t.end_fill()
                t.pu()
                t.goto(-5, -40)
                t.pd()
                t.pensize(2)
                t.color("black", '#79675d')
                t.begin_fill()
                t.circle(5)
                t.end_fill()
                t.pensize(3)
                t.right(115)
                t.forward(7)
                # 点击窗口关闭
                window = t.Screen()
                window.exitonclick()
        elif a2=="F":

            import pygame
            from pygame.locals import *
            import sys, random, time, math


            class GameWindow(object):
                '''创建游戏窗口类'''

                def __init__(self, *args, **kw):
                    self.window_length = 600
                    self.window_wide = 500
                    # 绘制游戏窗口,设置窗口尺寸
                    self.game_window = pygame.display.set_mode((self.window_length, self.window_wide))
                    # 设置游戏窗口标题
                    pygame.display.set_caption("CatchBallGame")
                    # 定义游戏窗口背景颜色参数
                    self.window_color = (135, 206, 250)

                def backgroud(self):
                    # 绘制游戏窗口背景颜色
                    self.game_window.fill(self.window_color)


            class Ball(object):
                '''创建球类'''

                def __init__(self, *args, **kw):
                    # 设置球的半径、颜色、移动速度参数
                    self.ball_color = (255, 215, 0)
                    self.move_x = 1
                    self.move_y = 1
                    self.radius = 10

                def ballready(self):
                    # 设置球的初始位置、
                    self.ball_x = self.mouse_x
                    self.ball_y = self.window_wide - self.rect_wide - self.radius
                    # 绘制球,设置反弹触发条件
                    pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)

                def ballmove(self):
                    # 绘制球,设置反弹触发条件
                    pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)
                    self.ball_x += self.move_x
                    self.ball_y -= self.move_y
                    # 调用碰撞检测函数
                    self.ball_window()
                    self.ball_rect()
                    # 每接5次球球速增加一倍
                    if self.distance < self.radius:
                        self.frequency += 1
                        if self.frequency == 5:
                            self.frequency = 0
                            self.move_x += self.move_x
                            self.move_y += self.move_y
                            self.point += self.point
                    # 设置游戏失败条件
                    if self.ball_y > 520:
                        self.gameover = self.over_font.render("Game Over", False, (0, 0, 0))
                        self.game_window.blit(self.gameover, (100, 130))
                        self.over_sign = 1


            class Rect(object):
                '''创建球拍类'''

                def __init__(self, *args, **kw):
                    # 设置球拍颜色参数
                    self.rect_color = (255, 0, 0)
                    self.rect_length = 100
                    self.rect_wide = 10

                def rectmove(self):
                    # 获取鼠标位置参数
                    self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
                    # 绘制球拍,限定横向边界
                    if self.mouse_x >= self.window_length - self.rect_length // 2:
                        self.mouse_x = self.window_length - self.rect_length // 2
                    if self.mouse_x <= self.rect_length // 2:
                        self.mouse_x = self.rect_length // 2
                    pygame.draw.rect(self.game_window, self.rect_color, (
                        (self.mouse_x - self.rect_length // 2), (self.window_wide - self.rect_wide), self.rect_length,
                        self.rect_wide))


            class Brick(object):
                def __init__(self, *args, **kw):
                    # 设置砖块颜色参数
                    self.brick_color = (139, 126, 102)
                    self.brick_list = [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1],
                                       [1, 1, 1, 1, 1, 1]]
                    self.brick_length = 80
                    self.brick_wide = 20

                def brickarrange(self):
                    for i in range(5):
                        for j in range(6):
                            self.brick_x = j * (self.brick_length + 24)
                            self.brick_y = i * (self.brick_wide + 20) + 40
                            if self.brick_list[i][j] == 1:
                                # 绘制砖块
                                pygame.draw.rect(self.game_window, self.brick_color,
                                                 (self.brick_x, self.brick_y, self.brick_length, self.brick_wide))
                                # 调用碰撞检测函数
                                self.ball_brick()
                                if self.distanceb < self.radius:
                                    self.brick_list[i][j] = 0
                                    self.score += self.point
                    # 设置游戏胜利条件
                    if self.brick_list == [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                                           [0, 0, 0, 0, 0, 0],
                                           [0, 0, 0, 0, 0, 0]]:
                        self.win = self.win_font.render("You Win", False, (0, 0, 0))
                        self.game_window.blit(self.win, (100, 130))
                        self.win_sign = 1


            class Score(object):
                '''创建分数类'''

                def __init__(self, *args, **kw):
                    # 设置初始分数
                    self.score = 0
                    # 设置分数字体
                    self.score_font = pygame.font.SysFont('arial', 20)
                    # 设置初始加分点数
                    self.point = 1
                    # 设置初始接球次数
                    self.frequency = 0

                def countscore(self):
                    # 绘制玩家分数
                    my_score = self.score_font.render(str(self.score), False, (255, 255, 255))
                    self.game_window.blit(my_score, (555, 15))


            class GameOver(object):
                '''创建游戏结束类'''

                def __init__(self, *args, **kw):
                    # 设置Game Over字体
                    self.over_font = pygame.font.SysFont('arial', 80)
                    # 定义GameOver标识
                    self.over_sign = 0


            class Win(object):
                '''创建游戏胜利类'''

                def __init__(self, *args, **kw):
                    # 设置You Win字体
                    self.win_font = pygame.font.SysFont('arial', 80)
                    # 定义Win标识
                    self.win_sign = 0


            class Collision(object):
                '''碰撞检测类'''

                # 球与窗口边框的碰撞检测
                def ball_window(self):
                    if self.ball_x <= self.radius or self.ball_x >= (self.window_length - self.radius):
                        self.move_x = -self.move_x
                    if self.ball_y <= self.radius:
                        self.move_y = -self.move_y

                # 球与球拍的碰撞检测
                def ball_rect(self):
                    # 定义碰撞标识
                    self.collision_sign_x = 0
                    self.collision_sign_y = 0

                    if self.ball_x < (self.mouse_x - self.rect_length // 2):
                        self.closestpoint_x = self.mouse_x - self.rect_length // 2
                        self.collision_sign_x = 1
                    elif self.ball_x > (self.mouse_x + self.rect_length // 2):
                        self.closestpoint_x = self.mouse_x + self.rect_length // 2
                        self.collision_sign_x = 2
                    else:
                        self.closestpoint_x = self.ball_x
                        self.collision_sign_x = 3

                    if self.ball_y < (self.window_wide - self.rect_wide):
                        self.closestpoint_y = (self.window_wide - self.rect_wide)
                        self.collision_sign_y = 1
                    elif self.ball_y > self.window_wide:
                        self.closestpoint_y = self.window_wide
                        self.collision_sign_y = 2
                    else:
                        self.closestpoint_y = self.ball_y
                        self.collision_sign_y = 3
                    # 定义球拍到圆心最近点与圆心的距离
                    self.distance = math.sqrt(
                        math.pow(self.closestpoint_x - self.ball_x, 2) + math.pow(self.closestpoint_y - self.ball_y, 2))
                    # 球在球拍上左、上中、上右3种情况的碰撞检测
                    if self.distance < self.radius and self.collision_sign_y == 1 and (
                            self.collision_sign_x == 1 or self.collision_sign_x == 2):
                        if self.collision_sign_x == 1 and self.move_x > 0:
                            self.move_x = - self.move_x
                            self.move_y = - self.move_y
                        if self.collision_sign_x == 1 and self.move_x < 0:
                            self.move_y = - self.move_y
                        if self.collision_sign_x == 2 and self.move_x < 0:
                            self.move_x = - self.move_x
                            self.move_y = - self.move_y
                        if self.collision_sign_x == 2 and self.move_x > 0:
                            self.move_y = - self.move_y
                    if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:
                        self.move_y = - self.move_y
                    # 球在球拍左、右两侧中间的碰撞检测
                    if self.distance < self.radius and self.collision_sign_y == 3:
                        self.move_x = - self.move_x

                # 球与砖块的碰撞检测
                def ball_brick(self):
                    # 定义碰撞标识
                    self.collision_sign_bx = 0
                    self.collision_sign_by = 0

                    if self.ball_x < self.brick_x:
                        self.closestpoint_bx = self.brick_x
                        self.collision_sign_bx = 1
                    elif self.ball_x > self.brick_x + self.brick_length:
                        self.closestpoint_bx = self.brick_x + self.brick_length
                        self.collision_sign_bx = 2
                    else:
                        self.closestpoint_bx = self.ball_x
                        self.collision_sign_bx = 3

                    if self.ball_y < self.brick_y:
                        self.closestpoint_by = self.brick_y
                        self.collision_sign_by = 1
                    elif self.ball_y > self.brick_y + self.brick_wide:
                        self.closestpoint_by = self.brick_y + self.brick_wide
                        self.collision_sign_by = 2
                    else:
                        self.closestpoint_by = self.ball_y
                        self.collision_sign_by = 3
                    # 定义砖块到圆心最近点与圆心的距离
                    self.distanceb = math.sqrt(
                        math.pow(self.closestpoint_bx - self.ball_x, 2) + math.pow(self.closestpoint_by - self.ball_y,
                                                                                   2))
                    # 球在砖块上左、上中、上右3种情况的碰撞检测
                    if self.distanceb < self.radius and self.collision_sign_by == 1 and (
                            self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
                        if self.collision_sign_bx == 1 and self.move_x > 0:
                            self.move_x = - self.move_x
                            self.move_y = - self.move_y
                        if self.collision_sign_bx == 1 and self.move_x < 0:
                            self.move_y = - self.move_y
                        if self.collision_sign_bx == 2 and self.move_x < 0:
                            self.move_x = - self.move_x
                            self.move_y = - self.move_y
                        if self.collision_sign_bx == 2 and self.move_x > 0:
                            self.move_y = - self.move_y
                    if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:
                        self.move_y = - self.move_y
                    # 球在砖块下左、下中、下右3种情况的碰撞检测
                    if self.distanceb < self.radius and self.collision_sign_by == 2 and (
                            self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
                        if self.collision_sign_bx == 1 and self.move_x > 0:
                            self.move_x = - self.move_x
                            self.move_y = - self.move_y
                        if self.collision_sign_bx == 1 and self.move_x < 0:
                            self.move_y = - self.move_y
                        if self.collision_sign_bx == 2 and self.move_x < 0:
                            self.move_x = - self.move_x
                            self.move_y = - self.move_y
                        if self.collision_sign_bx == 2 and self.move_x > 0:
                            self.move_y = - self.move_y
                    if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:
                        self.move_y = - self.move_y
                    # 球在砖块左、右两侧中间的碰撞检测
                    if self.distanceb < self.radius and self.collision_sign_by == 3:
                        self.move_x = - self.move_x


            class Main(GameWindow, Rect, Ball, Brick, Collision, Score, Win, GameOver):
                '''创建主程序类'''

                def __init__(self, *args, **kw):
                    super(Main, self).__init__(*args, **kw)
                    super(GameWindow, self).__init__(*args, **kw)
                    super(Rect, self).__init__(*args, **kw)
                    super(Ball, self).__init__(*args, **kw)
                    super(Brick, self).__init__(*args, **kw)
                    super(Collision, self).__init__(*args, **kw)
                    super(Score, self).__init__(*args, **kw)
                    super(Win, self).__init__(*args, **kw)
                    # 定义游戏开始标识
                    start_sign = 0

                    while True:
                        self.backgroud()
                        self.rectmove()
                        self.countscore()

                        if self.over_sign == 1 or self.win_sign == 1:
                            break
                        # 获取游戏窗口状态
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                sys.exit()
                            if event.type:
                                pressed_array = pygame.mouse.get_pressed()
                                if pressed_array[0]:
                                    start_sign = 1
                        if start_sign == 0:
                            self.ballready()
                        else:
                            self.ballmove()

                        self.brickarrange()

                        # 更新游戏窗口
                        pygame.display.update()
                        # 控制游戏窗口刷新频率
                        time.sleep(0.010)


            if __name__ == '__main__':
                pygame.init()
                pygame.font.init()
                catchball = Main()

        print('--------------------------\n还要继续和曾曾一起玩儿嘛?\n【请选择】\nA.要!     B.曾曾拜拜,下次再和你一起玩辣')
        a = input('你的选择是:\n')
        if a == 'B':
            print('拜拜咯|ू・ω・` )曾曾会想你的唔!')
            time.sleep(2)
            break
`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值