python3搶收成語創意語文小遊戲_少兒程式設計_作者:李興球
"""這是一個雙人小遊戲,在螢幕上會時不時的出現一些成語,玩家操作小方塊去碰這些成語即可得分。"""
from turtletools import * #從turtletools模組匯入所有命令,init_screen,init_sound,countdown from writer import * #從writer模組匯入Writer類 from idiom import * #匯入成語表 idiomList和Idiom類 from square import * #匯入方塊類 Square from time import sleep def clearlines(): """清除方塊所畫的線條圖形""" [square.clear() for square in squares] def end_of_countdown(): """倒計時結束遊戲""" global gametime if gametime>0: gametime = gametime - 1 screen.ontimer(end_of_countdown,1000) else: """停止所有方塊的移動""" [square.stop() for square in squares] idiom.stop() #成語停止 screen.bgpic(endimage) #顯示結束畫面 info = [ square.name + ":" + str(square.score) for square in squares] info = ",".join(info) #連線列表每一項 myfont = ("楷體",24,"normal") end_writer = Writer(0,-50,myfont,info,13000,screen) screen.onclick(lambda x,y:screen.bye()) #單擊螢幕關閉視窗 if __name__=="__main__": """初始化螢幕""" game_name = "搶收成語" screen_width,screen_height = 480,360 #定義全域性變數螢幕寬度和高度 ps = game_name,"black",screen_width,screen_height,"background2.gif" screen = init_screen(*ps) #呼叫初始化螢幕函式 """把每個成語gif圖片註冊到形狀列表,idiomList從模組idiom中來""" [screen.addshape(idiom) for idiom in idiomList] #註冊所有成語到形狀列表
"""初始化聲音""" have_pygame,bumpsound = init_sound("Popcorn1.wav","叮.wav")
"""寫遊戲的題目""" myfont = ("黑體",32,"normal") title_writer = Writer(0,100,myfont,game_name,3,screen) #Writer類
"""寫版權所有""" copy_right = "版權所有,Copy right by lxq" ps = 0,50-screen_height/2 ,("黑體",12,"normal"),copy_right,3,screen copyright_writer = Writer(*ps) """倒計時顯示,準備開始遊戲""" countdown(4,myfont,"cyan") #4,3,2,1倒計時
"""生成紅色小方塊""" ps = screen,"紅點",-50,0,180,"red","Up","Down","Left","Right" redsquare = Square(*ps) #方塊類,生成後會自己移動
"""生成藍色小方塊""" ps = screen,"藍點",50,0,0,"blue","w","s","a","d" bluesquare = Square(*ps) #方塊類,生成後會自己移動
"""把小方塊裝到列表裡,方便管理""" squares = [redsquare,bluesquare]
"""生成一個成語物件,成語隨機出現""" ps = 137,42,[redsquare,bluesquare],screen,have_pygame,bumpsound,game_name idiom = Idiom(*ps) #生成後,它等待被撞,然後換造型
"""註冊空格鍵事件,如果按空格鍵,那麼清除所有筆跡""" screen.onkeypress(clearlines,"space") #按空格鍵清除所畫的圖形
"""倒計時結束,顯示畫面""" endimage = "結束畫面.png" gametime = 60 #設定遊戲時間 end_of_countdown() #非同步等待遊戲結束
"""設定螢幕焦點,進入主迴圈""" screen.listen() screen.mainloop() """以上程式只是主程式,需要各個模組程式碼及素材請聯絡風火輪少兒程式設計李興球先生"""