時間比較倉猝,這個素材比較撿漏是以将就一下,關鍵看操作。
其次我今天大婚大家多多祝福我啊,,另外祝大家愚人節快樂,哈哈哈~
這還是這幾天外甥吵吵着想玩大魚吃小魚,作為一個合格的舅舅我必須出手了,雖然他沒玩兩分鐘就覺得太醜玩不下去了,但是我覺得還是分享一下,畢竟作為我們内行還是看門道的嘛~

目錄
-
- 一.遊戲畫面
- 二.遊戲素材
- 三.程式介紹
- 四.遊戲代碼
-
- 1.精靈對象。這個函數計算矩形下右角的一個坐标并傳回它。
- 2.設定遊戲屬性
- 3.遊戲對象
- 4.遊戲動态效果
- 五:經驗總結
一.遊戲畫面
二.遊戲素材
三.程式介紹
"""
大魚吃小魚.py
注意程式的mouth對象,它并不是"隐藏"的,雖然它看不見。
小魚碰到mouth會被“吃掉”。如果把mouth用hide指令設為隐藏,那麼是無法擷取到mouth的綁定盒,進而碰撞檢測失效。
"""
四.遊戲代碼
1.精靈對象。這個函數計算矩形下右角的一個坐标并傳回它。
from sprites import *
def calculate_pos(obj):
"""obj:精靈對象。這個函數計算矩形下右角的一個坐标并傳回它。
"""
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.遊戲動态效果
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後後出現
# 在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)