Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (45 sloc) 1.32 KB
int numBalls = 100; // ボールの数
float[] x = new float[numBalls]; // x座標の配列
float[] y = new float[numBalls]; // y座標の配列
float[] vx = new float[numBalls]; // x軸方向の速度の配列
float[] vy = new float[numBalls]; // y軸方向の速度の配列
int[] radius = new int[numBalls]; // 半径の配列
color[] c = new color[numBalls]; // 色の配列
void setup() {
size(960, 540);
pixelDensity(displayDensity());
// 初期値を設定
for (int i = 0; i < numBalls; i++) {
radius[i] = (int)random(10, 20);
x[i] = random(radius[i], width-radius[i]);
y[i] = random(radius[i], height-radius[i]);
vx[i] = random(-5, 5);
vy[i] = random(-5, 5);
c[i] = color(random(255), random(255), random(255));
}
}
void draw() {
background(255);
for (int i = 0; i < numBalls; i++) {
// ボールの位置を更新
x[i] += vx[i];
y[i] += vy[i];
// 左右の壁との当たり判定
if (x[i]-radius[i] <= 0 || x[i]+radius[i] >= width) {
vx[i] *= -1;
}
// 上下の壁との当たり判定
if (y[i]-radius[i] <= 0 || y[i]+radius[i] >= height) {
vy[i] *= -1;
}
// ボールを描画
noStroke();
fill(c[i]);
ellipse(x[i], y[i], 2*radius[i], 2*radius[i]);
}
}
void keyPressed() {
if (key == 's') {
saveFrame("sketch07.jpg");
}
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.