乌龟吃鱼小游戏(电脑对电脑)
import randomclass Life:
#设置类
def __init__(self,name,sort):
if sort==0:
self.sort=sort
self.name='小甲鱼'
self.life=100
self.x=5
self.y=5
if sort==1:
self.sort=sort
self.name=name
self.life=200
listp=
self.x=random.choice(listp)
self.y=random.choice(listp)
def setName(self,name):
self.name=name
def eatfish(self):
self.life+=20
def move_1(self):
dirc=random.randint(1,5)
if dirc==1:
if self.y==0:
self.y+=1
else:
self.y-=1
elif dirc==2:
if self.y==10:
self.y-=1
else:
self.y+=1
elif dirc==3:
if self.x==0:
self.x+=1
else:
self.x-=1
elif dirc==4:
if self.x==10:
self.x-=1
else:
self.x+=1
def move_2(self):
self.move_1()
self.move_1()
def move(self):
if self.sort==0:
self.life-=1
pace=random.randint(1,3)
if pace==1:
self.move_1()
else:
self.move_2()
else:
self.move_1()
def game_eat_fish():
#生成一龟十鱼
p=Life('',0)
fishs=[]
for i in range(10):
fishc=Life('fish'+str(i+1),1)
fishs.append(fishc)
ch_n=input('是否起名(y or n):')
if ch_n=='y':
name=input('请起名:')
p.setName(name)
cnt=0
print (cnt)
#win_print():
#输出界面
while 1:
#开始游戏
p.move()
for j in fishs:
j.move()
if p.x==j.x and p.y==j.y:
p.eatfish()
fishs.remove(j)
cnt=cnt+1
print (cnt)
#win_print():
#输出界面
fish_num=len(fishs)
if p.life==0 or fish_num==0:
print('life:'+str(p.life))
print('fish:'+str(fish_num))
#结束游戏
print('游戏结束')
break
感觉不能玩啊,你这确定能玩么
页:
[1]