|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
'''
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
'''
import numpy as np
class Move():
def move(self):
self.siteas = 0
while self.siteas==0 :
self.moveto = np.random.random()
if self.moveto < 0.25:
self.sitenow = self.site + [1,0]
elif self.moveto < 0.5:
self.sitenow = self.site + [-1,0]
elif self.moveto < 0.75:
self.sitenow = self.site + [0,1]
else :
self.sitenow = self.site + [0,-1]
if self.sitenow[0] <= 10 and self.sitenow[0] >= 0 and self.sitenow[1] <= 10 and self.sitenow[1] >= 0:
self.siteas = 1
self.site = self.sitenow
else:
self.siteas = 0
return self.site
class Fish(Move):
def __init__(self):
#初始位置
self.site = np.random.randint(10,size=2)
#确认存活
self.live = 1
def aten(self):
self.live = 0
class Tort(Move):
def __init__(self):
#初始体力
self.power = 100
#初始位置
self.site = np.random.randint(10,size=2)
def eat(self):
self.power += 20
if self.power > 100:
self.power = 100
class Gamerule():
##生成动物
def creat(self):
tort1 = Tort()
fish = [0,0,0,0,0,0,0,0,0,0,]
for i in range(10):
fish[i] = Fish()
##动物移动
def animove(self):
tort1.move()
self.ifaten()
for i in range(10):
if fish[i]:
fish[i].move()
self.ifaten()
tort1.move()
self.ifaten()
##判定是否被吃
##增加体力和判定存活
def ifaten(self):
for i in range(10):
if fish[i] != 0:
if all(fish[i].site == tort1.site):
fish[i] = 0
tort1.eat()
print('a fish have been aten')
##判定游戏结束
def ifend(self):
if tort1.power == 0 or fish == [0,0,0,0,0,0,0,0,0,0,]:
return 0
else :
return 1
gamestart = Gamerule()
tort1 = Tort()
fish = [0,0,0,0,0,0,0,0,0,0,]
for i in range(10):
fish[i] = Fish()
print('a fish have been create ',i,type(fish[i]),fish[i].site)
a = 0
while gamestart.ifend():
a+=1
gamestart.animove()
print('part',a)
print('game over')
if tort1.power == 0:
print('fish win ')
else:
print('tort win') |
|