Python實戰(zhàn)之大魚吃小魚游戲的實現(xiàn)
更新時間:2022年04月01日 10:07:14 作者:五包辣條!
這篇文章主要介紹了如何利用Python制作一個經(jīng)典游戲之大魚吃小魚,文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以參考一下
一.游戲畫面

二.游戲素材







三.程序介紹
大魚吃小魚.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.設置游戲屬性
width,height = 480,360
screen = Screen() # 新建寬高
screen.setup(width,height) # 設置寬高
screen.bgpic('res/underwater.png') # 設背景圖
screen.title("圖靈大海之大魚吃小魚")
3.游戲對象
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) # 左右翻轉
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)
? ? ? ? ? ? # 注意這里調用了reborn后,魚會立即隱藏,3后后出現(xiàn)
? ? ? ? ? ? # 在3秒內碰撞檢測無效,所以魚不能動
? ? ? ? ? ? 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)的詳細內容,更多關于Python大魚吃小魚的資料請關注腳本之家其它相關文章!
相關文章
Python3網(wǎng)絡爬蟲之使用User Agent和代理IP隱藏身份
這篇文章主要介紹了Python3網(wǎng)絡爬蟲之使用User Agent和代理IP隱藏身份,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
matplotlib 使用 plt.savefig() 輸出圖片去除旁邊的空白區(qū)域
這篇文章主要介紹了matplotlib 使用 plt.savefig() 輸出圖片去除旁邊的空白區(qū)域,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
python遍歷 truple list dictionary的幾種方法總結
下面小編就為大家?guī)硪黄猵ython遍歷 truple list dictionary的幾種方法總結。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
python+opencv實現(xiàn)動態(tài)物體追蹤
這篇文章主要為大家詳細介紹了python+opencv實現(xiàn)動態(tài)物體的追蹤,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

