zl程序教程

您现在的位置是:首页 >  后端

当前栏目

小学生python编程--红包雨

Python编程 -- 红包 小学生
2023-09-11 14:20:50 时间

 

 

 

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