這篇文章主要介紹“Python+Pygame怎麼實現瘋狂吃水果游戲”,在日常操作中,相信很多人在Python+Pygame怎麼實現瘋狂吃水果游戲問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python+Pygame怎麼實現瘋狂吃水果游戲”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
隨機掉落:西瓜????加分、葡萄????減分、炸彈????一條生命值初始為二。鼠標右鍵移動。加減多少分具體就等你們自己玩兒了哈,都劇透了就不好玩了撒!每次的游戲代碼都給你們留點兒底,嘻嘻,自己摸索嘛~
小編使用的環境:Python3、Pycharm社區版、tkinter、Pygame模塊,部分自 帶模塊不展示。
模塊安裝:pip install -i https://pypi.douban.com/simple/+模塊名
准備了背景音樂更有勁兒啦!記得seven這首歌嘛,還挺好聽的。
准備好的素材圖片背景掉落的物品等。
代碼超級多的!僅展示部分
主程序
import tkinterimport randomimport timeimport Paramimport Imageimport Bonusimport Deductionimport Beanimport Bombimport pygame# 定義物質列表(包含加分西瓜和消分葡萄和炸彈)bonusth = []deductionth = []bigbombs = []# 定義bean變量,保存豆豆對象bean = ""# 定義當前用戶的初始分數score = 0life = 2# 定義游戲狀態game_state = Param.GAME_START # 創建窗體game_window = tkinter.Tk()# 窗口文字設置game_window.title('I LOVE FRUIT')# 窗口位置處理screenwidth = game_window.winfo_screenwidth()screenheight = game_window.winfo_screenheight()size = '%dx%d+%d+%d' % (Param.GAME_WIDTH, Param.GAME_HEIGHT, (screenwidth-Param.GAME_WIDTH)/2, 50)game_window.geometry(size)# 加載游戲用到的所有的圖片background_image,bean_image,Bonus_image,Bomb_image,Deduction_image= Image.load_image(tkinter)Start,Stop = Image.load_state_image(tkinter) # 獲取畫布window_canvas = tkinter.Canvas(game_window)# 畫布包裝方式window_canvas.pack(expand=tkinter.YES, fill=tkinter.BOTH) # 時間標志count = 0num = 30 def create_fruit():# 生成水果 global count global num global score if score % 10 ==1: if num >= 8: num -= 8 count += 1 if count % num == 0: c = random.randint(1,10) if c <= 5: # 加分水果生成 bonus = Bonus.Bonus(Bonus_image) bonusth.append(bonus) # 物質添加到列表中 window_canvas.create_image(bonus.x,bonus.y,anchor = tkinter.NW,image=bonus.image,tag=bonus.tag) elif c<=8: # 銷分水果生成 deduction = Deduction.Deduction(Deduction_image) deductionth.append(deduction) window_canvas.create_image(deduction.x,deduction.y,anchor = tkinter.NW,image=deduction.image,tag=deduction.tag) else: #炸彈生成 bigbomb = Bomb.BigBomb(Bomb_image) bigbombs.append(bigbomb) window_canvas.create_image(bigbomb.x,bigbomb.y,anchor = tkinter.NW,image=bigbomb.image,tag=bigbomb.tag) def step_fruit(): # 遍歷所有的物質,調用移動的方法 for bonus in bonusth: bonus.step(window_canvas) for deduction in deductionth: deduction.step(window_canvas) for bigbomb in bigbombs: bigbomb.step(window_canvas) def judge_state(event): global game_state if game_state == Param.GAME_START: game_state = Param.GAME_RUNNING # 畫分 window_canvas.create_text(20, 20, text="分數:%d" % (score), anchor=tkinter.NW, fill="white",\ font="time 12 bold",tag="SCORE") # 畫生命 window_canvas.create_text(20, 50, text="生命:%d" % (life), anchor=tkinter.NW, fill="white",\ font="time 12 bold",tag="LIFE") # 刪除啟動圖片 window_canvas.delete("Start") elif game_state == Param.GAME_STOP: window_canvas.delete("bean") window_canvas.delete("STOP") game_state = Param.GAME_START game_start() def bean_move(event): if game_state == Param.GAME_RUNNING: now_x = bean.x now_y = bean.y bean.x = event.x - bean.w/2 bean.y = event.y - bean.h/2 window_canvas.move("bean", bean.x-now_x, bean.y-now_y) def out_of_bounds(): # 獲取所有物質,判斷是否越界 for deduction in deductionth: if deduction.out_of_bounds(): window_canvas.delete(deduction.tag) deductionth.remove(deduction) for bonus in bonusth: global outnum if bonus.out_of_bounds(): outnum += 1 window_canvas.delete(bonus.tag) bonusth.remove(bonus) if outnum >= 5: game_state = Param.GAME_STOP # 畫游戲結束的狀態 game_over() for bigbomb in bigbombs: if bigbomb.out_of_bounds(): window_canvas.delete(bigbomb.tag) bigbombs.remove(bigbomb) def bomb_action(): global score global life global bean global game_state #加分 for bonus in bonusth: if bonus.bomb(bean): window_canvas.delete(bonus.tag) bonusth.remove(bonus) score += 3 #減分 for deduction in deductionth: if deduction.bomb(bean): window_canvas.delete(deduction.tag) deductionth.remove(deduction) if score - 5 < 0: score = 0 game_state = Param.GAME_STOP # 畫游戲結束的狀態 game_over() else: score -= 5 for bigbomb in bigbombs: if bigbomb.bomb(bean): window_canvas.delete(bigbomb.tag) bigbombs.remove(bigbomb) # 如果分數或生命小於0 游戲結束 if life - 1 <= 0: life = 0 game_state = Param.GAME_STOP # 畫游戲結束的狀態 game_over() else: life -= 1 def draw_action(): # 畫分 window_canvas.delete("SCORE") # 畫生命 window_canvas.delete("LIFE") window_canvas.create_text(20,20,text="分數:%d"%(score),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="SCORE") window_canvas.create_text(20,50,text="生命:%d"%(life),anchor=tkinter.NW,fill="white",font="time 12 bold",tag="LIFE") def game_over(): global game_state game_state = Param.GAME_STOP for deduction in deductionth: window_canvas.delete(deduction.tag) for bonus in bonusth: window_canvas.delete(bonus.tag) for bigbomb in bigbombs: window_canvas.delete(bigbomb.tag) deductionth.clear() bonusth.clear() bigbombs.clear() window_canvas.create_image(0,0,anchor=tkinter.NW,image=Stop,tag="STOP") if pygame.mixer.music.get_busy() == True: pygame.mixer.music.stop()#停止播放 def game_start(): global score global life global num global outnum num = 30 score = 0 life = 2 outnum = 0 # 畫游戲背景 window_canvas.create_image(0, 0, anchor=tkinter.NW, image=background_image, tag="background") # 創建豆豆對象 global bean bean = Bean.Bean(bean_image) window_canvas.create_image(bean.x, bean.y, anchor=tkinter.NW, image=bean.image, tag="bean") window_canvas.create_image(0, 0, anchor=tkinter.NW, image=Start, tag="Start") pygame.mixer.init() pygame.mixer.music.load('Seve(鋼琴版).mp3') #加載背景音樂 if pygame.mixer.music.get_busy() == False: pygame.mixer.music.play(300,0)#重復300次,從第一秒開始播放 def game(): if game_state == Param.GAME_START: game_start() # 鼠標監聽 window_canvas.bind("<Motion>",bean_move) window_canvas.bind("<Button-1>",judge_state) while True: if game_state == Param.GAME_RUNNING: # 物質入場 create_fruit() # 物質動起來 step_fruit() # 刪除越界的物質 out_of_bounds() # 檢測碰撞 bomb_action() if score >= 0: # 畫分和生命 draw_action() # 更新顯示 game_window.update() time.sleep(0.04) if __name__ == "__main__": game() game_window.mainloop()
1)游戲界面
2)隨機截圖
3)消耗結束
到此,關於“Python+Pygame怎麼實現瘋狂吃水果游戲”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速雲網站,小編會繼續努力為大家帶來更多實用的文章!