• 小学生python练习3--跳伞防鸟小游戏


     

     

     

     

    from pgzrun import *
    from random import *
    sounds.bg.play(-1)

    WIDTH = 350
    HEIGHT = 700

    bg1 = Actor("bg1.png")
    over = Actor("bg3.png")
    kd = Actor("keduo1.png",[180,270])
    #任务1 作答区域
    bg2 = Actor("bg2.png")
    bg2.top = HEIGHT

    #创建小鸟
    bird = []
    def create_bird():
        b = Actor("bird1.png")
        b.x = -150
        b.y = randint(20, 680)
        bird.append(b)
    clock.schedule_interval(create_bird, 2)

    state = "begin"

    def draw():
        if state == "begin":
            bg1.draw()
            bg2.draw()
            for b in bird:
                b.draw()
            kd.draw()
        if state == "end":
            over.draw()
            screen.draw.text(str(time1), (185, 180), fontsize=55, color="gold")

    #背景移动    
    def move_bg():
        bg1.y = bg1.y - 10
        bg2.y = bg2.y - 10
        #任务2 作答区域
        if bg1.bottom < 0 :
            bg1.top = HEIGHT
        if bg2.bottom < 0 :
            bg2.top = HEIGHT

    #小鸟移动
    def move_bird():
        for b in bird:
            b.x = b.x + 2
            if b.left > WIDTH:
                bird.remove(b)
    #可多移动
    def move_kd():
        if keyboard.left == True:
            if kd.left > 0:
                kd.x = kd.x - 5
        if keyboard.right == True:
            if kd.right < WIDTH:
                kd.x = kd.x + 5
        if keyboard.up == True:
            if kd.top > 0:
                kd.y = kd.y - 5
        if keyboard.down == True:
            if kd.bottom < HEIGHT:
                kd.y = kd.y + 5

    def update():
        global state
        if state == "begin":
            move_bg()
            move_bird()
            move_kd()
            
            #小鸟和可多碰撞检测
            index = kd.collidelist(bird)
            if index >= 0:
                music.play_once("hit.mp3")
                sounds.bg.stop()
                bird.pop(index)
                state = "end"

    #{
    def show1():
        if state == "begin":
            kd.image = "keduo2"
            for b in bird:
                b.image = "bird2"
            clock.schedule_unique(show2, 0.3)
        
        
    def show2():
        if state == "begin":
            kd.image = "keduo1"
            for b in bird:
                b.image = "bird1"
            clock.schedule_unique(show1, 0.3)

    clock.schedule_unique(show1, 0.2)
    #}
    #{
    time1 = 0
    def count():
        global time1
        if state == "begin":
            time1 += 1
            clock.schedule_unique(count, 1)  

    count()
    #}


    go()

     

  • 相关阅读:
    git如何把test分支做好的功能 文件和文件夹复制到master
    Shell编程从看懂到看开②(字符串、数组、注释、流程控制、read读取控制台输入)
    axure9设置组件自适应浏览器大小
    M3E&ChatGLM向量化构建本地知识库
    jwt使用token传递前后端认证 实战项目演练
    网站页脚展示备案号并在新标签页中打开超链接
    个人交易者如何开始使用股票量化策略接口?
    Python---pyecharts地图案例
    三万字盘点Spring/SpringBoot的那些常用扩展点
    从零开始配置 vim(14)——目录管理插件
  • 原文地址:https://blog.csdn.net/fqfq123456/article/details/126110222