这样呢,现在不报错了,跑不出结果了#游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。
#假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
#游戏生成1只乌龟和10条鱼
#它们的移动方向均随机
#乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
#当移动到场景边缘,自动向反方向移动
#乌龟初始化体力为100(上限)
#乌龟每移动一次,体力消耗5
#当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
#鱼暂不计算体力
#当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
import easygui as g
import random
location_x=[1,10]
location_y=[1,10]
class Fish:
def __init__(self): #定义鱼的初始情况,__init__返回值为空
self.fish_location_save=[] #存放乌龟和鱼重合位置的类似于(1,2)形式的坐标
self.fish_x_location=[] #单独存放乌龟和鱼重合位置的X的坐标
self.fish_y_location=[] #单独存放乌龟和鱼重合位置的y的坐标
#一次性随即设定10个位置,即生成10条鱼
while len(self.fish_location_save)!=10 :
fish_now_x=random.randint(1,10)
fish_now_y=random.randint(1,10)
fish_location=(fish_now_x,fish_now_y) #生成元祖,类似于(x,y)形式,然后存于self.fish_location_save列表
if fish_location not in self.fish_location_save:
self.fish_location_save.append(fish_location)
self.fish_x_location.append(fish_now_x)
self.fish_y_location.append(fish_now_y)
def fish_move(self):
#10条鱼,他们都可动可不动
self.new_fish_location_save=[] #存放移动后新的乌龟和鱼重合位置的类似于(1,2)形式的坐标
self.new_fish_x_location=[] #单独移动后新的乌龟和鱼重合位置的X的坐标
self.new_fish_y_location=[] #单独移动后新的乌龟和鱼重合位置的y的坐标
#移动10条鱼的位置
while len(self.new_fish_location_save) != 10:
#鱼的最大移动为1,在初始坐标的基础上,x,y+1,或者不动,所以有三种情况
for i in range(0,10):
new_fish_x=random.choice([self.fish_x_location[i],self.fish_x_location[i]+1,self.fish_x_location[i]-1])
new_fish_y=random.choice([self.fish_y_location[i],self.fish_y_location[i]+1,self.fish_y_location[i]-1])
#判断是否超出边界
if new_fish_x not in location_x:
new_fish_x-=1
if new_fish_y not in location_y:
new_fish_x-=1
me=(new_fish_x,new_fish_y)
if me not in self.new_fish_location_save:
self.new_fish_location_save.append(me)
self.new_fish_x_location.append(new_fish_x)
self.new_fish_y_location.append(new_fish_y)
turtle=Turtle()
turtlr_location=turtle.turtle_move()
if turtlr_location in self.new_fish_location_save:
#乌龟和鱼的位置重合,删掉重合位置的鱼的坐标
for i in len(self.fish_location_save):
if fish_location == self.fish_location_save[i]:
me=i
break
self.fish_location_save.pop(me)
class Turtle:
def __init__(self): #定义乌龟的初始情况
#初始体力
self.turtle_begin=100
#随机设置初始位置
self.now_x=random.randint(1,10)
self.now_y=random.randint(1,10)
def turtle_move(self):
turtle_now_x=random.choice([self.now_x+1,self.now_x+2,self.now_x-1,self.now_x-2,self.now_x])
turtle_now_y=random.choice([self.now_y+1,self.now_y+2,self.now_y-1,self.now_y-2,self.now_y])
#判断是否超出边界
if turtle_now_x not in location_x:
#反向返回
turtle_now_x-=1
if turtle_now_y not in location_y:
turtle_now_y-=1
turtlr_location=(turtle_now_x,turtle_now_y)
fish=Fish()
fish_location_save=fish.fish_move()
if turtlr_location in fish_location_save:
self.turtle_begin += 20
print("一条鱼已经被吃掉,还剩%d条鱼"%len(fish_location_save))
return turtlr_location #返回重合位置的坐标
else:
return (0,0) #不重合的时候返回一个不在范围的坐标,避免鱼在盗用的时候报错
self.turtle_begin -= 5
if self.turtle_begin == 0:
g.msgbox("game over")
return self.turtle_begin
fish_use=Fish()
turtlr_use=Turtle()
fish_use.__init__ ()
turtlr_use.__init__()
turtlr_use.turtle_move()
fish_use.fish_move()
while turtle_begin !=0 or len(fish_location_save) != 0 :
turtlr_use.turtle_move()
fish_use.fish_move()
|