|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么乌龟一直被饿死= = !
import random
class Tortois:
x = random.randint(0,10)
y = random.randint(0,10)
position = (x,y)
mobility = [1,2]
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 = [f1,f2,f3,f4,f5,f6,f7,f8,f9,f10]
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 编辑
[b]哈哈哈,本来赢的概率就比较低的,代码如果没问题的话
而且你的 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 = [1, 2]
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 = [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]
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
[/b]
|
|