|
发表于 2019-8-6 10:22:04
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
class Fish:
xmax=10
ymax=10
all=10
step=1
x=[]
y=[]
for each in range(all):#随机出生all个鱼。
x.append([random.randint(0,xmax)])
y.append([random.randint(0,ymax)])
def move(self):
for each in range(self.all):
direction=random.randint(1,4)#1上2下3左4右
if direction==1:
self.y[each]+=self.step
elif direction==2:
self.y[each]-=self.step
elif direction==3:
self.x[each]-=self.step
elif direction==4:
self.x[each]+=self.step
if self.x[each]<0:
self.x[each]=-self.x[each]
elif self.y[each]<0:
self.y[each]=-self.y[each]
elif self.x[each]>self.xmax:
self.x[each]=self.x[each]-2*self.step
elif self.y[each]>self.ymax:
self.y[each]=self.y[each]-2*self.step
for each in range(self.all):
print('鱼%d的位置是(%d,%d)'%(each,self.x[each],self.y[each]))
f=Fish()
f.move()
- import random
- class Fish:
- xmax=10
- ymax=10
- all=10
- step=1
- x=[]
- y=[]
- for each in range(all):#随机出生all个鱼。
- x.append(random.randint(0,xmax))
- y.append(random.randint(0,ymax))
- def move(self):
- for each in range(self.all):
- direction=random.randint(1,4)#1上2下3左4右
- if direction==1:
- self.y[each]+=self.step
- elif direction==2:
- self.y[each]-=self.step
- elif direction==3:
- self.x[each]-=self.step
- elif direction==4:
- self.x[each]+=self.step
- if self.x[each]<0:
- self.x[each]=-self.x[each]
- elif self.y[each]<0:
- self.y[each]=-self.y[each]
- elif self.x[each]>self.xmax:
- self.x[each]=self.x[each]-2*self.step
- elif self.y[each]>self.ymax:
- self.y[each]=self.y[each]-2*self.step
- for each in range(self.all):
- print('鱼%d的位置是(%d,%d)'%(each,self.x[each],self.y[each]))
- f=Fish()
- f.move()
复制代码
|
|