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