|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我做37的的作业时候我想把鱼的位置用列表里面列表的方式打出来
空位为0 有鱼的地方放1
code如下
- import random, time, os
- scene_x = [1, 10]
- scene_y = [1, 10]
- class Fish():
- def __init__(self):
- self.x = random.randint(scene_x[0], scene_x[1])
- self.y = random.randint(scene_y[0], scene_y[1])
- def move(self):
- #move to new position
- new_x = self.x + random.choice([-1, 1])
- new_y = self.y + random.choice([-1, 1])
- #checking if x is over scence
- if new_x < scene_x[0]:
- self.x = self.x - (new_x - self.x)
- elif new_x > scene_x[1]:
- self.x = self.x - (new_x - self.x)
- else:
- self.x = new_x
- #checking if y is over scence
- if new_y < scene_x[0]:
- self.y = self.y - (new_y - self.y)
- elif new_y > scene_x[1]:
- self.y = self.y - (new_y - self.y)
- else:
- self.y = new_y
-
- def get_position(self):
- return (self.x, self.y)
- #make a table, 0 represent empty
- empty_list = [0] * 10
- axix = []
- for i in range(10):
- axix.append(empty_list)
-
- # find where the fish is, and change to 1, if position is occupied, then creat a new fish
- num = 0
- print(axix)
- for eachfish in fish_family:
- (tem_a, tem_b) = eachfish.get_position()
- tem_a = tem_a -1
- tem_b = tem_b -1
- if axix[tem_a][tem_b] == 0:
- axix[tem_a][tem_b] = 1
-
-
复制代码
我实验下来
a = [[0,0,0], [0,0,0]]
a[0][1] = 1
可以得到 a = [[0,1,0], [0,0,0]]
我if的意思是这个随机编码如果是0的话就可以放鱼,否则就不行
如图是我编码的结果,明明只有10条鱼,怎么出现那么多
|
-
|