hp12c
30 January 2011

fun of Processing!

Processingは楽しいな
キレイな色玉を飛ばして
しばらくじっと眺めてみよう
色玉が静かに底に沈んだら
今度はマウスでクリックね
メッセージを押してもう一度
Processingは楽しいな
ArrayList letters;
PFont font;
int numLetters;
void setup(){
 size(320, 460);
 letters = new ArrayList();
 numLetters = 100;
 font = createFont("Helvetica", 18, true);
 smooth();
 noLoop();
}
void draw(){
 if (letters.size() < numLetters){
 letters.add(new Letter());
 }
 
 background(0);
 
 boolean allStop = true;
 for (int i = 0; i<letters.size(); i++){
 Letter l = (Letter) letters.get(i);
 l.collide(letters);
 l.move();
 l.display();
 if (allStop && mag(l.spx, l.spy) < 2) allStop = true;
 else allStop = false; 
 }
 if (allStop){
 fill(204, 255, 0, 90);
 textFont(font);
 textSize(18);
 text("Processing is fun!", width/2, height/3);
 }
}
class Letter 
{
 char[] alphabet= {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
 'W', 'X', 'Y', 'Z'};
 char letter;
 float x, y, spx, spy, r, tsize;
 color c;
 float FRICTION = -0.7;
 float SPRING = 0.3;
 float GRAVITY = 0.07;
 boolean on_collition;
 
 public Letter () {
 letter = alphabet[int(random(alphabet.length))];
 r = 12;
 tsize = 10;
 x = 0;
 y = height;
 spx = random(-0.5, 0.5);
 spy = -0.5;
 c = randomColor();
 on_collition = false;
 }
 
 void move() {
 spy += GRAVITY;
 x += spx;
 y += spy;
 if (x + r > width) {
 x = width - r;
 spx *= FRICTION; 
 }
 else if (x - r < 0) {
 x = r;
 spx *= FRICTION;
 }
 if (y + r > height) {
 y = height - r;
 spy *= FRICTION; 
 } 
 if (mag(spx, spy) < 0.8 && on_collition){
 spx = spy = 0;
 }
 on_collition = false;
 }
 void bounce() {
 float minx = r;
 float maxx = width-r;
 float maxy = height -r;
 if (x < minx || x > maxx){
 spx = -spx * FRICTION;
 if (x < minx) { x = 2*minx - x;}
 else { x = 2*maxx -x; }
 }
 if (y > maxy){
 spy = -spy * FRICTION;
 y = 2*maxy - y;
 }
 }
 void collide(ArrayList others) {
 for (int i = 0; i<others.size(); i++){
 Letter other = (Letter) others.get(i);
 if (this == other) return;
 float ox = other.x;
 float oy = other.y;
 float distance = dist(x, y, ox, oy);
 float minDist = other.r + r;
 if (distance <= minDist){
 float angle = atan2(oy-y, ox-x);
 float targetX = x + cos(angle) * minDist;
 float targetY = y + sin(angle) * minDist;
 float ax = (targetX - ox) * SPRING;
 float ay = (targetY - oy) * SPRING;
 spx -= ax;
 spy -= ay;
 other.spx += ax;
 other.spy += ay;
 on_collition = true;
 }
 }
 }
 void display() {
 fill(c, 80);
 noStroke();
 ellipseMode(CENTER);
 ellipse(x, y, r*2, r*2);
 textAlign(CENTER);
 textSize(tsize);
 fill(c);
 text(letter, x, y+r/3);
 on_collition = false;
 }
 private color randomColor() {
 color c = color(random(255), random(255), random(255));
 return c;
 }
}
void mousePressed() {
 float d = dist(mouseX, mouseY, width/2, height/3);
 if (d < 100) { setup(); loop(); };
 for (int i = 0; i<letters.size(); i++){
 Letter letter = (Letter) letters.get(i);
 float dd = dist(mouseX, mouseY, letter.x, letter.y);
 if (dd < letter.r){
 letter.spx += random(-2, 2);
 letter.spy -= 12;
 }
 }
}

(追記:2011年1月31日)メッセージのクリックでリスタートできるようにしました。



Please enable JavaScript to view the comments powered by Disqus. blog comments powered by Disqus
ruby_pack8

100円〜で好評発売中!
M'ELBORNE BOOKS


AltStyle によって変換されたページ (->オリジナル) /