用Processing来形成背景图


思路

用random函数生成随机数,根据其值来决定是往哪个方向移动

dx,dy用于记录四个方向

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int dis = 10;
int nowX, nowY, preX, preY;

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

void setup()
{
size(1920, 1080);
nowX = preX = width / 2;
nowY = preY = height / 2;
frameRate(30);
}

void draw()
{
int val = int(random(0, 4));
nowX += dx[val] * dis;
nowY += dy[val] * dis;
line(nowX, nowY, preX, preY);
preX = nowX;
preY = nowY;
}

Author: BY 水蓝
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source BY 水蓝 !
  TOC