游戏编程:
(初学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