processingでプログラムを書いてみた。どんな動作をするかは動かしてのおたのしみ。
boolean hitwall(float pos, int max, int size) {
float radius = size / 2;
if ( (pos+radius > max ) || (pos - radius < 0) ) {
return true;
}
return false;
}
class Ball {
float x, y;
float x_vec, y_vec;
float x_speed, y_speed;
int ballsize;
int r, g, b;
Ball(int tsize) {
x=width/2;
y=height/2;
x_vec = 1;
y_vec = 1;
x_speed=3;
y_speed=3;
ballsize=tsize;
r=(int)random(64, 255);
g = (int)random(64, 255);
b= (int)random(64, 255);
}
float nextPosX(float x, int size) {
if ( hitwall(x, width, size) ) {
float old_speed;
old_speed = x_speed;
x_vec *= -1;
x_speed=random(5, 8);
return x_vec*old_speed + x;
}
return x_vec*x_speed + x;
}
float nextPosY(float y, int size) {
if ( hitwall(y, height, size) ) {
float old_speed;
old_speed = y_speed ;
y_speed=random(6, 12);
y_vec *= -1;
return y_vec*old_speed + y;
}
return y_vec*y_speed + y;
}
void display() {
fill(r, g, b);
ellipse(x, y, ballsize, ballsize);
}
void move() {
x = nextPosX(x, ballsize);
y = nextPosY(y, ballsize);
}
}
int numBalls = 500;
Ball[] balls = new Ball[numBalls];
int maxballsize=30;
void setup() {
size(displayWidth, displayHeight-80);
numBalls = ((width * height) / (maxballsize*maxballsize))/4;
for (int i=0; i