两天时间写出来的乌龟吃鱼的作业,呜呜呜好难
'''假设游戏场景为范围(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 +
elif self.moveto < 0.5:
self.sitenow = self.site + [-1,0]
elif self.moveto < 0.75:
self.sitenow = self.site +
else :
self.sitenow = self.site +
if self.sitenow <= 10 and self.sitenow >= 0 and self.sitenow <= 10 and self.sitenow >= 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 =
for i in range(10):
fish = Fish()
##动物移动
def animove(self):
tort1.move()
self.ifaten()
for i in range(10):
if fish:
fish.move()
self.ifaten()
tort1.move()
self.ifaten()
##判定是否被吃
##增加体力和判定存活
def ifaten(self):
for i in range(10):
if fish != 0:
if all(fish.site == tort1.site):
fish = 0
tort1.eat()
print('a fish have been aten')
##判定游戏结束
def ifend(self):
if tort1.power == 0 or fish == :
return 0
else :
return 1
gamestart = Gamerule()
tort1 = Tort()
fish =
for i in range(10):
fish = Fish()
print('a fish have been create ',i,type(fish),fish.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') 注释只需要一个#啊…… 没想明白为什么要用 numpy 的随机数而不用自带的随机数模块 推荐一个,我写的 --> 戳我前进
页:
[1]