哈哈,乌龟吃鱼的写出来了
import randomclass Turtle:
def initial_position(self):
self.x = random.randint(0,10)
self.y = random.randint(0,10)
return
def move(self):
self.x -= random.choice([-2, -1, 1, 2])
self.y -= random.choice([-2, -1, 1, 2])
if self.x <= 0:
self.x += random.choice()
elif self.x >=10:
self.x -= random.choice()
if self.y <= 0:
self.y += random.choice()
elif self.y >= 10:
self.y -= random.choice()
def move_position(self):
self.initial_position =
return self.initial_position
class Fish:
def initial_position(self):
self.x = random.randint(0,10)
self.y = random.randint(0,10)
def move(self):
self.x -= random.choice([-1, 1])
self.y -= random.choice([-1, 1])
if self.x <= 0:
self.x += 1
elif self.x >=10:
self.x -= 1
if self.y <= 0:
self.y += 1
elif self.y >= 10:
self.y -= 1
def move_position(self):
self.initial_position =
return self.initial_position
tt = Turtle()
tt.initial_position()
fish_position_list =
fish_list = []
for i in range(10):
exec('fish' + str(i) + '= Fish()')
for i in range(10):
exec('fish' + str(i) + '.initial_position()')
fish_list.append(i)
stamina =100
count = 0
pool = []
ch = 0
while stamina:
stamina -= 1
for i in fish_list:
if i not in pool:
exec('fish' + str(i) +'.move()')
exec('fish_position_list =' + "fish" + str(i) + ".move_position()")
tt.move()
pos = tt.move_position()
ch += 1
if pos in fish_position_list:
n = fish_position_list.count(pos)
stamina += 20 * n
if stamina > 100:
stamina = 100
count += n
for j in range(n):
a = fish_position_list.index(pos)
fish_position_list = None
pool.append(fish_list)
print(fish_position_list)
if count == 10:
print('---TURTLE WIN !---')
print('剩余体力%d,共捕食%d次' % (stamina, ch))
break
else:
print('跑了%d次这么久,小乌龟累死啦,饿死啦..' % ch)
666啊 为什么都喜欢用exec呢 hrp 发表于 2020-5-30 17:58
为什么都喜欢用exec呢
百度出来的,还有什么方法吗老板 失文 发表于 2020-5-30 20:00
百度出来的,还有什么方法吗老板
import random
class Turtle:
def initial_position(self):
self.x = random.randint(0,10)
self.y = random.randint(0,10)
return
def move(self):
self.x -= random.choice([-2, -1, 1, 2])
self.y -= random.choice([-2, -1, 1, 2])
if self.x <= 0:
self.x += random.choice()
elif self.x >=10:
self.x -= random.choice()
if self.y <= 0:
self.y += random.choice()
elif self.y >= 10:
self.y -= random.choice()
def move_position(self):
self.initial_position =
return self.initial_position
class Fish:
def initial_position(self):
self.x = random.randint(0,10)
self.y = random.randint(0,10)
def move(self):
self.x -= random.choice([-1, 1])
self.y -= random.choice([-1, 1])
if self.x <= 0:
self.x += 1
elif self.x >=10:
self.x -= 1
if self.y <= 0:
self.y += 1
elif self.y >= 10:
self.y -= 1
def move_position(self):
self.initial_position =
return self.initial_position
tt = Turtle()
tt.initial_position()
fish_position_list =
fish_list = []
for i in range(10):
# exec('fish' + str(i) + '= Fish()')
fish_list.append(Fish())
# for i in range(10):
for i in fish_list:
# exec('fish' + str(i) + '.initial_position()')
# fish_list.append(i)
i.initial_position()
stamina =100
count = 0
pool = []
ch = 0
while stamina:
stamina -= 1
# for i in fish_list:
for i in range(len(fish_list)):
# if i not in pool:
if fish_list not in pool:
# exec('fish' + str(i) +'.move()')
fish_list.move()
# exec('fish_position_list =' + "fish" + str(i) + ".move_position()")
fish_position_list = fish_list.move_position()
tt.move()
pos = tt.move_position()
ch += 1
if pos in fish_position_list:
n = fish_position_list.count(pos)
stamina += 20 * n
if stamina > 100:
stamina = 100
count += n
for j in range(n):
a = fish_position_list.index(pos)
fish_position_list = None
pool.append(fish_list)
print(fish_position_list)
if count == 10:
print('---TURTLE WIN !---')
print('剩余体力%d,共捕食%d次' % (stamina, ch))
break
else:
print('跑了%d次这么久,小乌龟累死啦,饿死啦..' % ch)
hrp 发表于 2020-5-30 20:52
这是否就相当于把一个类的实例化对象的地址直接传入列表,直接调用这个地址也就相当于调用这个函数;
老板麻烦移步看我刚发的一个帖子,和这个很相似,困扰好久了,感谢! 失文 发表于 2020-5-30 21:05
这是否就相当于把一个类的实例化对象的地址直接传入列表,直接调用这个地址也就相当于调用这个函数;
老 ...
还有这个fish_list.append(Fishs()),添加到列表的地址,为什么每次都不一样,明明是重复调用,同样的,哪怕把一个数值赋值给不同变量也是指向同一个地址的啊,这两个为什么不同 失文 发表于 2020-5-30 21:05
这是否就相当于把一个类的实例化对象的地址直接传入列表,直接调用这个地址也就相当于调用这个函数;
老 ...
是的,fish_list里存的都是Fish类的实例化对象地址,实例对象该怎么用就怎么用,不需要特意通过exec建一大堆变量名去保存实例。 失文 发表于 2020-5-30 21:07
还有这个fish_list.append(Fishs()),添加到列表的地址,为什么每次都不一样,明明是重复调用,同样的, ...
Fish()是类的实例化,不是函数调用,每次Fish()都会生成一个实例。
页:
[1]