Python實戰(zhàn)之大魚吃小魚游戲的實現(xiàn)
一.游戲畫面
二.游戲素材
三.程序介紹
大魚吃小魚.py
注意程序的mouth對象,它并不是"隱藏"的,雖然它看不見。
小魚碰到mouth會被“吃掉”。如果把mouth用hide命令設為隱藏,那么是無法獲取到mouth的綁定盒,從而碰撞檢測失效。
四.游戲代碼
1.精靈對象。這個函數(shù)計算矩形下右角的一個坐標并返回它
from sprites import * def calculate_pos(obj): """obj:精靈對象。這個函數(shù)計算矩形下右角的一個坐標并返回它。 """ x,y = obj.position() # 角色的坐標 mx,my = mouse_position() # 鼠標指針的坐標 k = 1 if mx > x else -1 # 在右則為1,否則為-1 left,top,right,bottom = obj.bbox()# 獲取綁定盒 w = right-left # 大魚的寬度 h = top - bottom # 大魚的高度 x0 = x + k * w//2.5 # 嘴巴大概的x坐標 y0 = y - h//12 # 嘴巴大概的y坐標 return x0,y0
2.設置游戲?qū)傩?/h3>
width,height = 480,360
screen = Screen() # 新建寬高
screen.setup(width,height) # 設置寬高
screen.bgpic('res/underwater.png') # 設背景圖
screen.title("圖靈大海之大魚吃小魚")
width,height = 480,360 screen = Screen() # 新建寬高 screen.setup(width,height) # 設置寬高 screen.bgpic('res/underwater.png') # 設背景圖 screen.title("圖靈大海之大魚吃小魚")
3.游戲?qū)ο?/h3>
fish_group = Group(tag='fish') # 新建組,標簽為fish
fishes = ['res/fish1.png','res/fish2.png','res/fish3.png','res/crab-b.png']
# 由于下面的魚的標簽都是fish,所以會自動加入到fish_group中
for x in range(10):
x = random.randint(-200,200)
y = random.randint(-140,140)
f = Sprite(shape=random.choice(fishes),tag='fish',pos=(x,y))
f.scale(0.5)
[fish.setheading(random.randint(1,360)) for fish in fish_group]
m1 = Mouse(1) # 鼠標左鍵
fish = Sprite('res/fish1-a.png') # 實例化大魚
fish.rotatemode(1) # 左右翻轉(zhuǎn)
fishscale= 0.6
fish.scale(fishscale)
mouth = Sprite(shape='circle') # 實例化嘴巴,用于碰撞檢測
mouthscale = 0.4
mouth.scale(mouthscale) # 縮放嘴巴大小
mouth.setalpha(0) # 把它設為透明,改為非0它會顯示出來
clock = Clock() # 新建時鐘對象
fish_group = Group(tag='fish') # 新建組,標簽為fish fishes = ['res/fish1.png','res/fish2.png','res/fish3.png','res/crab-b.png'] # 由于下面的魚的標簽都是fish,所以會自動加入到fish_group中 for x in range(10): x = random.randint(-200,200) y = random.randint(-140,140) f = Sprite(shape=random.choice(fishes),tag='fish',pos=(x,y)) f.scale(0.5) [fish.setheading(random.randint(1,360)) for fish in fish_group] m1 = Mouse(1) # 鼠標左鍵 fish = Sprite('res/fish1-a.png') # 實例化大魚 fish.rotatemode(1) # 左右翻轉(zhuǎn) fishscale= 0.6 fish.scale(fishscale) mouth = Sprite(shape='circle') # 實例化嘴巴,用于碰撞檢測 mouthscale = 0.4 mouth.scale(mouthscale) # 縮放嘴巴大小 mouth.setalpha(0) # 把它設為透明,改為非0它會顯示出來 clock = Clock() # 新建時鐘對象
4.游戲動態(tài)效果
while True: ? ? for f in fish_group: ? ? ? ? if f.isvisible():f.fd(1) ? ? # 在可見的情況下才移動 ? ? ? ? # 小魚碰到嘴巴及單擊鼠標則被吃掉,大魚長大 ? ? ? ? if f.collide(mouth,0.5) and m1.down() : ? ? ? ? ? ? fishscale += 0.01 ? ? ? ? ? ? fish.scale(fishscale) ? ? # 大魚長大 ? ? ? ? ? ? mouthscale += 0.01 ? ? ? ? ? ? mouth.scale(mouthscale) ? # 嘴巴跟著加大 ? ? ? ? ? ? x = random.randint(-200,200) ? ? ? ? ? ? y = random.randint(-140,140) ? ? ? ? ? ? # 注意這里調(diào)用了reborn后,魚會立即隱藏,3后后出現(xiàn) ? ? ? ? ? ? # 在3秒內(nèi)碰撞檢測無效,所以魚不能動 ? ? ? ? ? ? f.reborn(x,y,delay=3) ? ? ? ? ? ? f.shape(random.choice(fishes)) ? ? ? ? ? ? ? ? ? ? f.bounce_on_edge() ? ? ? ?? ? ? fish.heading(mouse_pos()) ? ? ? ?# 大魚跟隨鼠標指針 ? ? x0,y0 = calculate_pos(fish) ? ? ?# 計算嘴巴的大概坐標 ? ? mouth.goto(x0,y0) ? ? ? ? ? ? ? ?# 嘴巴大這個坐標? ? ? md = ?fish.distance(mouse_pos()) # 計算魚到鼠標指針距離 ? ? if md > 50:fish.fd(min(md,4)) ? ?# 如果距離大于50則游 ? ? # 張嘴與合嘴 ? ? if m1.down(): ? ? ? ? fish.shape('res/fish1-a.png') ? ? else: ? ? ? ? fish.shape('res/fish1-b.png') ? ? screen.update() ? ? clock.tick(60) ? fish.shape('res/fish1-a.png') ? ? else: ? ? ? ? fish.shape('res/fish1-b.png') ? ? screen.update() ? ? clock.tick(60)
以上就是Python實戰(zhàn)之大魚吃小魚游戲的實現(xiàn)的詳細內(nèi)容,更多關于Python大魚吃小魚的資料請關注腳本之家其它相關文章!
相關文章
python標準庫壓縮包模塊zipfile和tarfile詳解(常用標準庫)
在我們常用的系統(tǒng)windows和Linux系統(tǒng)中有很多支持的壓縮包格式,包括但不限于以下種類:rar、zip、tar,這篇文章主要介紹了python標準庫壓縮包模塊zipfile和tarfile詳解(常用標準庫),需要的朋友可以參考下2022-06-06Python基于QRCode實現(xiàn)生成二維碼的方法【下載,安裝,調(diào)用等】
這篇文章主要介紹了Python基于QRCode實現(xiàn)生成二維碼的方法,結(jié)合實例形式較為詳細的分析了Python下載,安裝與調(diào)用QRCode實現(xiàn)生成二維碼功能的具體步驟與相關操作技巧,需要的朋友可以參考下2017-07-07Python?GUI利用tkinter皮膚ttkbootstrap實現(xiàn)好看的窗口
這篇文章主要介紹了Python?GUI利用tkinter皮膚ttkbootstrap實現(xiàn)好看的窗口,文章基于python的相關資料展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06Python3.5內(nèi)置模塊之random模塊用法實例分析
這篇文章主要介紹了Python3.5內(nèi)置模塊之random模塊用法,結(jié)合實例形式分析了Python3.5 random模塊生成隨機數(shù)與隨機字符串相關操作技巧,需要的朋友可以參考下2019-04-04