|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Twilight6 于 2020-7-12 23:46 编辑
- import random
- class tortoise:
- direction_list = ["x","y"]
- current_location = {"x":0,"y":0}
- value = {"x":1,"y":1}
-
- def move(self):
- run = [self.direction_list[random.randint(0,1)],random.randint(1,2)]
- if 10<self.current_location[run[0]] or self.current_location[run[0]]<0 :
- self.value[run[0]] *= -1
- self.current_location[run[0]] = self.current_location[run[0]] + run[1] * self.value[run[0]]
- return self.current_location
- class fishc:
- fishc_list = ["x","y"]
- current_location = {"x":0,"y":0}
- value = {"x":1,"y":1}
-
- def move(self):
- run = [self.fishc_list[random.randint(0,1)],1]
- if 10<self.current_location[run[0]] or self.current_location[run[0]]<0 :
- self.value[run[0]] *= -1
- self.current_location[run[0]] = self.current_location[run[0]] + run[1] * self.value[run[0]]
- return self.current_location
- t = tortoise()
- yu = list(range(10))
- life_fishc = list(range(10))
- f = list(range(10))
- value = 100
- l = 0
- gui = {}
- for i in range(len(yu)):
- yu[i] = 1
- life_fishc[i] = fishc()
-
- while value > 0 and l<8 :
- if value<100 :
- gui = t.move()
- for i in range(len(yu)):
- if yu[i] :
- f[i] = life_fishc[i].move()
- if f[i] == gui and yu[i] != 0:
- l += 1
- yu[i] = 0
- value += 20
- print("第%d条鱼被吃了"%i)
- value -= 1
- print("有%d条鱼被吃了"%l)
复制代码
你定义的属性都错了,你这里:
class tortoise:
direction_list = ["x","y"]
current_location = {"x":0,"y":0}
value = {"x":1,"y":1}
和这里:
class fishc:
fishc_list = ["x","y"]
current_location = {"x":0,"y":0}
value = {"x":1,"y":1}
定义的都是类属性,而类属性是所有该类实例对象共享的,所以你创建再多的鱼,都是同个坐标出现的,你应该用 __init__方法来初始化位置坐标,配合 random 达到随机效果
其他的错误没怎么看
|
|