• 小学生python编程--红包雨


     

     

     

    from pgzrun import *
    from random import *
    #{
    music.play("bg.mp3")
    WIDTH = 370
    HEIGHT = 596

    bg0 = Actor("bg0.png")
    bg1 = Actor("bg1.png")
    score_show = Actor("score.png", [60, 36])
    time_show = Actor("time.png", [300, 36])
    over = Actor("over.png")
    again = Actor("again.png",[175,375])
    #}
    #创建红包
    red = []
    def create_red():
        r = Actor("hongbao1.png")
        r.x = randint(20,330)
        r.y = randint(0, 50)
        red.append(r)
    clock.schedule_interval(create_red, 0.6)

    #按下按键让state变为 "run",开启游戏
    #{
    state = "ready"
    def on_key_down(key):
        global state
        if keyboard.space == True:
            state = "run"
    #}

    #倒计时
    #{
    total = 30
    def timing():
        global total, state
        total = total - 1
        if total == 0:
            state = "over"
            clock.unschedule(timing)
    clock.schedule_interval(timing, 1)
    #}

    #根据state的值来绘制不同游戏状态的角色
    #{
    def draw():
        if state == "ready":
            bg0.draw()    
        
        if state == "run":
            bg1.draw()
            
            for r in red:
                r.draw()
                
            score_show.draw()
            time_show.draw()
            
            screen.draw.text(
                "score: "+str(score), 
                fontsize=30, 
                center=[64, 38]
                )
            screen.draw.text(
                "time: "+str(total), 
                fontsize=30, 
                center=[292, 38]
                )
        
        if state == "over":
            over.draw()
            again.draw()
            screen.draw.text(
                str(score), 
                fontsize=80, 
                center=[178, 110], 
                )
            music.stop()
    #}

    #移动红包   
    #{
    def move_red():
        for r in red:
            r.y = r.y + 3
            if r.bottom < 0:
                red.remove(r)

    def update():
        if state == "run":
            move_red()
    #}

    #鼠标点击红包        
    score = 0
    def on_mouse_down(pos):
        global score, total, red, state
        
        for r in red:
            
            if r.collidepoint(pos):
                red.remove(r)
                score = score + 1
                sounds.get.play()
                
        #鼠标点击重玩按钮则重新开始 
        if state == "over":
            if again.collidepoint(pos):
                score = 0
                total = 30
                red = []
                state = "ready"
                music.play("bg.mp3")
                clock.schedule_interval(timing, 1)

    go()

  • 相关阅读:
    15天深度复习JavaWeb的详细笔记(一)——JDBC
    冯诺依曼体系结构
    Roson的Qt之旅 #112 QML布局之GridLayout(表格布局)
    华为昇腾系列——入门学习
    CV计算机视觉每日开源代码Paper with code速览-2023.11.3
    `英语` 2022/8/21
    KafKa3.x基础
    17、生成器
    9.3 链表从指定节点插入新节点
    Go float精度 .00值判断
  • 原文地址:https://blog.csdn.net/fqfq123456/article/details/126124960