Python+Pygame實戰(zhàn)之瘋狂吃水果游戲的實現(xiàn)
導(dǎo)語
嘿嘿!木木子今日閃現(xiàn)——已經(jīng)給大家寫了很多內(nèi)容啦~
涉及的人工智能、初學(xué)者、爬蟲、數(shù)據(jù)分析(這方面的一般不過審核)游戲........

PS:
吃豆人我寫過了哈
Python+Pygame實戰(zhàn)之吃豆豆游戲的實現(xiàn)
切水果我寫過了哈
Python Pygame實戰(zhàn)之水果忍者游戲的實現(xiàn)
今天二者集合,做出一款新游戲,哈哈哈,名字叫做《瘋狂??吃水果》小游戲,其實聽著挺??的,但是做出來的效果其實沒有想象中那么高大尚吶!(給你們打個預(yù)防針)
本文是基于Pygame寫的一款游戲哈!
一、準(zhǔn)備中
1)游戲玩法
隨機(jī)掉落:西瓜??加分、葡萄??減分、炸彈??一條生命值初始為二。鼠標(biāo)右鍵移動。加減多少分具體就等你們自己玩兒了哈,都劇透了就不好玩了撒!每次的游戲代碼都給你們留點兒底,嘻嘻,自己摸索嘛~
2)環(huán)境安裝
小編使用的環(huán)境:Python3、Pycharm社區(qū)版、tkinter、Pygame模塊,部分自 帶模塊不展示。
模塊安裝:pip install -i https://pypi.douban.com/simple/+模塊名
3)素材準(zhǔn)備
準(zhǔn)備了背景音樂更有勁兒啦!記得seven這首歌嘛,還挺好聽的。

?準(zhǔn)備好的素材圖片背景掉落的物品等。

二、代碼展示
代碼超級多的!僅展示部分
主程序
import tkinter
import random
import time
import Param
import Image
import Bonus
import Deduction
import Bean
import Bomb
import pygame
# 定義物質(zhì)列表(包含加分西瓜和消分葡萄和炸彈)
bonusth = []
deductionth = []
bigbombs = []
# 定義bean變量,保存豆豆對象
bean = ""
# 定義當(dāng)前用戶的初始分?jǐn)?shù)
score = 0
life = 2
# 定義游戲狀態(tài)
game_state = Param.GAME_START
# 創(chuàng)建窗體
game_window = tkinter.Tk()
# 窗口文字設(shè)置
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)
# 時間標(biāo)志
count = 0
num = 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) # 物質(zhì)添加到列表中
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():
# 遍歷所有的物質(zhì),調(diào)用移動的方法
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="分?jǐn)?shù):%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():
# 獲取所有物質(zhì),判斷是否越界
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
# 畫游戲結(jié)束的狀態(tài)
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
# 畫游戲結(jié)束的狀態(tài)
game_over()
else:
score -= 5
for bigbomb in bigbombs:
if bigbomb.bomb(bean):
window_canvas.delete(bigbomb.tag)
bigbombs.remove(bigbomb)
# 如果分?jǐn)?shù)或生命小于0 游戲結(jié)束
if life - 1 <= 0:
life = 0
game_state = Param.GAME_STOP
# 畫游戲結(jié)束的狀態(tài)
game_over()
else:
life -= 1
def draw_action():
# 畫分
window_canvas.delete("SCORE")
# 畫生命
window_canvas.delete("LIFE")
window_canvas.create_text(20,20,text="分?jǐn)?shù):%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")
# 創(chuàng)建豆豆對象
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)#重復(fù)300次,從第一秒開始播放
def game():
if game_state == Param.GAME_START:
game_start()
# 鼠標(biāo)監(jiān)聽
window_canvas.bind("<Motion>",bean_move)
window_canvas.bind("<Button-1>",judge_state)
while True:
if game_state == Param.GAME_RUNNING:
# 物質(zhì)入場
create_fruit()
# 物質(zhì)動起來
step_fruit()
# 刪除越界的物質(zhì)
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)隨機(jī)截圖

3)消耗結(jié)束

以上就是Python+Pygame實戰(zhàn)之瘋狂吃水果游戲的實現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python Pygame瘋狂吃水果的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中的hashlib和base64加密模塊使用實例
這篇文章主要介紹了python中的hashlib和base64加密模塊使用實例,hashlib模塊支持的加密算法有md5 sha1 sha224 sha256 sha384 sha512,需要的朋友可以參考下2014-09-09
Keras中的多分類損失函數(shù)用法categorical_crossentropy
這篇文章主要介紹了Keras中的多分類損失函數(shù)用法categorical_crossentropy,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器的過程
這篇文章主要介紹了ubuntu在線服務(wù)器python?Package安裝到離線服務(wù)器,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
利用Pandas讀取文件路徑或文件名稱包含中文的csv文件方法
今天小編就為大家分享一篇利用Pandas讀取文件路徑或文件名稱包含中文的csv文件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python機(jī)器學(xué)習(xí)庫scikit-learn使用詳解
scikit-learn是Python中最流行的機(jī)器學(xué)習(xí)庫之一,它提供了各種各樣的機(jī)器學(xué)習(xí)算法和工具,包括分類、回歸、聚類、降維等2023-03-03

