float x, y, vx, vy, r = 10, e = 0.9; float pw = 40, ph = 10; int score = 0; class target { float x, y, w = 30, h=10; boolean panic = false; int timer; target(int i) { x = random(width); y = 20 * i + 20; } void paint() { x += 2; if (x > width) x = 0; fill(0,200,0); rect(x-w/2,y-h/2,w,h); if (panic) { timer --; if (timer <= 0) panic = false; fill(255,0,0,timer * 5); float d = 52 - timer; ellipse(x,y,d,d); } } boolean bumped(float u, float v) { if (v < y+h/2 - r || v > y+h/2 + r) return false; if (u > x-w/2 && u < x+w/2) return true; if (dist(u, v, x-w/2, y) < r) return true; if (dist(u, v, x+w/2, y) < r) return true; return false; } void hit() { panic = true; timer = 50; } } int nTargets = 10; target targets[] = new target[nTargets]; void reset() { x = random(r,width-r); y = r; vx = random(-5.0,5.0); vy = 0; } void setup() { size(300,400); noStroke(); for (int i = 0; i < nTargets; i ++) targets[i] = new target(i); reset(); } void draw() { background(200,255,220); vy += 0.2; x += vx; y += vy; if (vy < 0) for (int i = 0; i < nTargets; i ++) if (targets[i].bumped(x, y)) targets[i].hit(); if (x < r) { vx = -vx * e; x = 2 * r - x; } else if (x > width-r) { vx = -vx * e; x = 2 * (width-r) - x; } if (y < r) { vy = -vy * e; y = 2 * r - y; } else if (y > height-r-ph && x > mouseX-pw && x < mouseX+pw) { vy = -vy * (mousePressed? 1.2 : e); y = 2 * (height - r - ph) - y; vx += (x - mouseX) * 0.1; score ++; } else if (y > height-r) reset(); fill((score * 10) % 256); ellipse(x,y,2*r,2*r); fill(0,0,200); rect(mouseX - pw, height - ph, pw * 2, ph); for (int i = 0; i < nTargets; i ++) targets[i].paint(); }