鱼和乌龟的作业我永远是乌龟死亡
按照小甲鱼的答案打的,感觉是哪出问题了import random as r
legal_x =
legal_y =
class WuGui:
def __init__(self):
self.x = r.randint(legal_x,legal_x)
self.y = r.randint(legal_y,legal_y)
self.power = 100
def move(self):
new_x = self.x + r.choice([-1,-2,1,2])
new_y = self.y + r.choice([-1,-2,1,2])
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x - legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
else:
self.y = new_y
self.power -= 1
return(new_x, new_y)
def eat(self):
self.power += 20
if self.power > 100:
self.power = 100
class Fish:
def __init__(self):
self.x = r.randint(legal_x,legal_x)
self.y = r.randint(legal_y,legal_y)
def move(self):
new_x = self.x + r.choice([-1,1,0])
new_y = self.y + r.choice([-1,1,0])
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x - legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
else:
self.y = new_y
return(self.x, self.y)
wugui = WuGui()
fish = []
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while True:
if not len(fish):
print('吃完了')
break
if not wugui.power:
print('乌龟体力耗尽,挂掉了')
break
pos = wugui.move()
for each_fish in fish[:]:
if each_fish.move == pos:
wugui.eat()
fish.remove(each_fish)
print('有一条鱼儿被吃掉了')
我懂了 有个地方函数少了个括号 for each_fish in fish[:]:
if each_fish.move() == pos:
wugui.eat()
fish.remove(each_fish)
print('有一条鱼儿被吃掉了')
这里少了对括号。正确代码:
import random as r
legal_x =
legal_y =
class WuGui:
def __init__(self):
self.x = r.randint(legal_x,legal_x)
self.y = r.randint(legal_y,legal_y)
self.power = 100
def move(self):
new_x = self.x + r.choice([-1,-2,1,2])
new_y = self.y + r.choice([-1,-2,1,2])
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x - legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
else:
self.y = new_y
self.power -= 1
return(new_x, new_y)
def eat(self):
self.power += 20
if self.power > 100:
self.power = 100
class Fish:
def __init__(self):
self.x = r.randint(legal_x,legal_x)
self.y = r.randint(legal_y,legal_y)
def move(self):
new_x = self.x + r.choice([-1,1,0])
new_y = self.y + r.choice([-1,1,0])
if new_x < legal_x:
self.x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x - legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y - legal_y)
else:
self.y = new_y
return(self.x, self.y)
wugui = WuGui()
fish = []
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while True:
if not len(fish):
print('吃完了')
break
if not wugui.power:
print('乌龟体力耗尽,挂掉了')
break
pos = wugui.move()
for each_fish in fish[:]:
if each_fish.move() == pos:
wugui.eat()
fish.remove(each_fish)
print('有一条鱼儿被吃掉了')
页:
[1]