乌龟一直被饿死???
为什么乌龟一直被饿死= = !import random
class Tortois:
x = random.randint(0,10)
y = random.randint(0,10)
position = (x,y)
mobility =
physical_strength = 100
def move(self):
distance = random.randint(1,2)
direction = random.randint(1,4)
if direction == 1:
if self.x - distance >= 0:
self.position = (self.x-distance,self.y)
else:
self.position = (self.x+distance,self.y)
elif direction == 2:
if self.x + distance <= 10:
self.position = (self.x+distance,self.y)
else:
self.position = (self.x-distance,self.y)
elif direction == 3:
if self.y - distance >= 0:
self.position = (self.x,self.y-distance)
else:
self.position = (self.x,self.y+distance)
else:
if self.y + distance >= 10:
self.position = (self.x,self.y+distance)
else:
self.position = (self.x,self.y-distance)
self.physical_strength -= 1
def eat(self):
self.physical_strength += 20
class Fish:
x = random.randint(0,10)
y = random.randint(0,10)
position = (x,y)
mobility = 1
def move(self):
direction = random.randint(1,4)
if direction == 1:
if self.x - self.mobility >= 0:
self.position = (self.x-self.mobility,self.y)
else:
self.position = (self.x+self.mobility,self.y)
elif direction == 2:
if self.x + self.mobility <= 10:
self.position = (self.x+self.mobility,self.y)
else:
self.position = (self.x-self.mobility,self.y)
elif direction == 3:
if self.y - self.mobility >= 0:
self.position = (self.x,self.y-self.mobility)
else:
self.position = (self.x,self.y+self.mobility)
else:
if self.y + self.mobility >= 10:
self.position = (self.x,self.y+self.mobility)
else:
self.position = (self.x,self.y-self.mobility)
t = Tortois()
f1 = Fish()
f2 = Fish()
f3 = Fish()
f4 = Fish()
f5 = Fish()
f6 = Fish()
f7 = Fish()
f8 = Fish()
f9 = Fish()
f10 = Fish()
list_fish =
game = 1
while game:
if t.physical_strength != 0 and list_fish != []:
for i in list_fish:
if t.position == i.position:
list_fish.romove(i)
t.physical_strength += 20
t.move()
for i in list_fish:
i.move()
elif t.physical_strength == 0:
print('乌龟被饿死了。。。')
game -= 1
else:
print('鱼被吃光了。。。')
game -= 1
本帖最后由 Twilight6 于 2020-7-24 12:08 编辑
哈哈哈,本来赢的概率就比较低的,代码如果没问题的话
而且你的 remove 拼成了 romove
你代码这里,Fish 类不能用类属性,类属性是所有 Fish 类的实例对象共享的
也就是你移动一个,全部都会改变,要用实例对象,而乌龟只有一只,所以用类和 实例差别不大,这里我帮你改了下代码:
import random
class Tortois:
def __init__(self):
self.x = random.randint(0, 10)
self.y = random.randint(0, 10)
self.position = (self.x, self.y)
self.mobility =
self.physical_strength = 1000000
def move(self):
distance = random.randint(1, 2)
direction = random.randint(1, 4)
if direction == 1:
if self.x - distance >= 0:
self.position = (self.x - distance, self.y)
else:
self.position = (self.x + distance, self.y)
elif direction == 2:
if self.x + distance <= 10:
self.position = (self.x + distance, self.y)
else:
self.position = (self.x - distance, self.y)
elif direction == 3:
if self.y - distance >= 0:
self.position = (self.x, self.y - distance)
else:
self.position = (self.x, self.y + distance)
else:
if self.y + distance >= 10:
self.position = (self.x, self.y + distance)
else:
self.position = (self.x, self.y - distance)
self.physical_strength -= 1
def eat(self):
self.physical_strength += 20
class Fish:
def __init__(self):
self.x = random.randint(0, 10)
self.y = random.randint(0, 10)
self.position = (self.x, self.y)
self.mobility = 1
def move(self):
direction = random.randint(1, 4)
if direction == 1:
if self.x - self.mobility >= 0:
self.position = (self.x - self.mobility, self.y)
else:
self.position = (self.x + self.mobility, self.y)
elif direction == 2:
if self.x + self.mobility <= 10:
self.position = (self.x + self.mobility, self.y)
else:
self.position = (self.x - self.mobility, self.y)
elif direction == 3:
if self.y - self.mobility >= 0:
self.position = (self.x, self.y - self.mobility)
else:
self.position = (self.x, self.y + self.mobility)
else:
if self.y + self.mobility >= 10:
self.position = (self.x, self.y + self.mobility)
else:
self.position = (self.x, self.y - self.mobility)
t = Tortois()
f1 = Fish()
f2 = Fish()
f3 = Fish()
f4 = Fish()
f5 = Fish()
f6 = Fish()
f7 = Fish()
f8 = Fish()
f9 = Fish()
f10 = Fish()
list_fish =
game = 1
while game:
if t.physical_strength != 0 and list_fish != []:
for i in list_fish:
if t.position == i.position:
list_fish.remove(i)
t.physical_strength += 20
t.move()
for i in list_fish:
i.move()
elif t.physical_strength == 0:
print('乌龟被饿死了。。。')
game -= 1
else:
print('鱼被吃光了。。。')
game -= 1
Twilight6 发表于 2020-7-24 11:58
哈哈哈,本来赢的概率就比较低的,代码如果没问题的话
而且你的 remove 拼成了 romove
额。。。。那它为什么不报错 def花 发表于 2020-7-24 12:04
额。。。。那它为什么不报错
你没遇到过而已,我刚刚运行碰搭配一次报错才发现,你看下楼上
你代码还有一些其他问题,细节方面我没看,不知道代码还有没有问题 小污龟的无线死亡{:10_334:} 本帖最后由 sunrise085 于 2020-7-24 12:22 编辑
二楼说的很对,你把乌龟体力值调高一些,吃一条鱼增加体力也调高一些,就能看到吃鱼了。
另外,吃鱼之后没有任何提示,所以你看不到吃鱼。
我在89行remove鱼后面加了一句
print("乌龟吃掉了一条鱼!")
我在你的程序基础上把体力值调整到1000,最多也就吃两条鱼,大部分时候吃不到鱼 Twilight6 发表于 2020-7-24 11:58
哈哈哈,本来赢的概率就比较低的,代码如果没问题的话
而且你的 remove 拼成了 romove
移动一个全部都会变,就是第一个鱼移动了,改变了类属性,然后第二个鱼初始的类属性也变了,是这个意思吗? def花 发表于 2020-7-24 12:21
移动一个全部都会变,就是第一个鱼移动了,改变了类属性,然后第二个鱼初始的类属性也变了,是这个意思吗 ...
对的,而且不止第二个,只要是这个类的实例,都会改变,而且我刚刚打印了下鱼的坐标发现坐标不是 1~10,而是 -1 ~11 乌龟真惨,一直被饿死。{:9_221:} 虐待动物,举报了{:10_256:}
页:
[1]