遊戲編程:
(初學python,耗費兩天時間才編出代碼,尚有很多可以優化之處)
按以下要求定義一個烏龜類和魚類並嘗試編寫遊戲:
假設遊戲場景爲範圍(x, y)爲0<=x<=10,0<=y<=10
遊戲生成1只烏龜和10條魚 # 它們的移動方向均隨機
烏龜的最大移動能力是2(Ta可以隨機選擇1還是2移動),魚兒的最大移動能力是1
當移動到場景邊緣,自動向反方向移動
烏龜初始化體力爲100(上限)
烏龜每移動一次,體力消耗1
當烏龜和魚座標重疊,烏龜喫掉魚,烏龜體力增加20
魚暫不計算體力
當烏龜體力值爲0(掛掉)或者魚兒的數量爲0遊戲結束
======================================
編程思路:
生成一條烏龜10條小魚
while
烏龜小魚分別考慮,每動一次判斷一次位置是否重複
直到烏龜餓死或小魚喫光 break
需要考慮的問題:
- 隨機移動(方向、大小)的實現
- 運動的疊加(基於前一個位置),且每條魚各自獨立
- 判斷重複刪除小魚
- 一個類面對不同對象時如何區分
import random
x = 5 #烏龜初始位置x
y = 5 #烏龜初始位置y
print('小龜初始位置在({0},{1})'.format(x,y))
power = 100 # 初始體力
count = 0 # 用於跳出
# 小魚的初始位置
j1 = random.randint(0, 10)
k1 = random.randint(0, 10)
j2 = random.randint(0, 10)
k2 = random.randint(0, 10)
j3 = random.randint(0, 10)
k3 = random.randint(0, 10)
j4 = random.randint(0, 10)
k4 = random.randint(0, 10)
j5 = random.randint(0, 10)
k5 = random.randint(0, 10)
j6 = random.randint(0, 10)
k6 = random.randint(0, 10)
j7 = random.randint(0, 10)
k7 = random.randint(0, 10)
j8 = random.randint(0, 10)
k8 = random.randint(0, 10)
j9 = random.randint(0, 10)
k9 = random.randint(0, 10)
j10 = random.randint(0, 10)
k10 = random.randint(0, 10)
# 將小魚的位置做成字典存儲,當與小龜重複則刪掉
FISH = {'F1':(j1,k1),'F2':(j2,k2),'F3':(j3,k3),'F4':(j4,k4),'F5':(j5,k5),'F6':(j6,k6),'F7':(j7,k7),'F8':(j8,k8),'F9':(j9,k9),'F10':(j10,k10)}
while count == 0:
class Tortoise:
"""小烏龜的類"""
# 移動(距離方向(隨機))
x1 = random.randint(-2, 2) # x方向移動距離
y1 = random.randint(-2, 2) # y方向移動距離
def mov(self, x, y,power):
# 開始動了
x += self.x1
y += self.y1
power -= 1 # 移動一次體力減1
return x,y,power
def peng(self,x,y):
# 判斷是否碰壁(碰壁則自動吸附到壁上,後面隨機分離)
if x < 0:
x = 0
if x > 10:
x = 10
if y < 0:
y = 0
if y > 10:
y = 10
return x,y
class Fish:
"""小魚的類"""
def mov(self,j,k):
# 移動(方向(隨機)、距離(隨機)、判斷是否碰壁)
j1 = random.randint(-1, 1) # x方向移動距離
k1 = random.randint(-1, 1) # y方向移動距離
# 開始動了
j += j1
k += k1
return j,k
def peng(self,j,k):
# 判斷是否碰壁(碰壁則自動吸附到壁上,後面隨機分離)
if j < 0:
j = 0
if j > 10:
j = 10
if k < 0:
k = 0
if k > 10:
k = 10
return j,k
gui = Tortoise()
(x,y,power) = gui.mov(x,y,power) #龜動
(x,y) = gui.peng(x,y) #測龜碰
print("小龜移動到了({0},{1})".format(x,y))
print('體力:{0}'.format(power))
def fmp(name):
"""輸入字典中魚名;
根據對應舊位置輸出新位置"""
(j,k) = FISH[name] # 獲取舊魚位置
n = Fish()
(j,k) = n.mov(j,k)
return n.peng(j,k)
if 'F1' in FISH:
FISH['F1'] = fmp('F1') # 如果小魚F1還在則繼續動
if 'F2' in FISH:
FISH['F2'] = fmp('F2')
if 'F3' in FISH:
FISH['F3'] = fmp('F3')
if 'F4' in FISH:
FISH['F4'] = fmp('F4')
if 'F5' in FISH:
FISH['F5'] = fmp('F5')
if 'F6' in FISH:
FISH['F6'] = fmp('F6')
if 'F7' in FISH:
FISH['F7'] = fmp('F7')
if 'F8' in FISH:
FISH['F8'] = fmp('F8')
if 'F9' in FISH:
FISH['F9'] = fmp('F9')
if 'F10' in FISH:
FISH['F10'] = fmp('F10')
print('小魚位置{0}'.format(FISH))
# 判斷小魚是否被喫(根據值(位置)找到鍵(魚名)並把重複的刪除)
if (x,y) in FISH.values():
"""小魚被喫掉(刪掉小魚對象),體力增加20"""
F = []
for a in range(0, len(FISH)):
if list(FISH.values())[a] == (x,y):
F.append((list(FISH.keys())[a])) # 找到重複小魚的鍵放在一起
power += 20 # 體力加20
# 刪掉與烏龜同一位置的小魚
for del_fish in F:
print('小魚{0}被喫掉啦!烏龜在{1}'.format(del_fish,(x,y)))
del FISH[del_fish] # 刪掉位置重複的小魚
# 結束跳出
if power == 0:
"""判斷結束條件"""
print("小烏龜被餓死啦")
count = 1
if len(FISH) == 0:
print('小魚被喫光啦!')
count = 1