|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random as g
x=[0,10]
y=[0,10]
class All:
def __init__(self):
self.wangba=100
self.x1=g.randint(x[0],x[1]) #这里为什么要加括号啊,不加括号又报类型错误
self.y1=g.randint(y[0],y[1])
def back(self):
new_x=self.x1+g.choice([2,1,-1,-2])
new_y=self.y1+g.choice([2,1,-1,-2])
if new_x<x[0]: #这里x[0],不能直接写成0吗?
self.x1=x[0]-(new_x-x[0])
elif new_x>x[1]:
self.x1=x[1]-(new_x-x[1])
else:
self.x1=new_x
if new_y<y[0]:
self.y1=y[0]-(new_y-y[0])
elif new_y>y[1]:
self.y1=y[1]-(nwe_y-y[1])
else:
self.y1=new_y #类和对象这里它是可以自己进行循环吗?
self.wangba-=1
return (self.x1,self.y1)
def eat(self):
self.wangba=self.wangba+20
class Cll:
def __init__(self):
self.x1=g.randint(x[0],x[1])
self.y1=g.randint(y[0],y[1])
def move(self):
new_x=self.x1+(g.choice([1,-1]))
new_y=self.y1+(g.choice([1,-1]))
if new_x<x[0]:
self.x1=x[0]-(new_x-x[0])
elif new_x>x[1]:
self.x1=x[1]-(new_x-x[1])
else:
self.x1=new_x
if new_y<y[0]:
self.y1=y[0]-(new_y-y[0])
elif new_y>y[1]:
self.y1=y[1]-(new_y-y[1])
else:
self.y1=new_y
return (self.x1,self.y1)
a=All()
list1=[]
for i in range(10):
list2=Cll() #这里不用Cll.move()吗?
list1.append(list2)
while True:
if not len(list1)>0:
print('鱼被乌龟吃完了,乌龟胜利!')
break
if not a.wangba:
print('乌龟体力耗尽挂掉了')
break
pos=a.back()
for i in list1[:]:
if i.move()==pos:
print('有一条鱼被吃掉了,乌龟回复20血量')
a.eat()
list1.remove(i)
最后为什么这个代码运行不了?报错我还可以看哪里错误,可这个代码运行之后不报错误,也不出答案。
课后作业很多都不会做,要看答案才能看懂,要学自闭了
|
|