程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

小學生python編程--紅包雨

編輯: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()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved