Skip to content

Instantly share code, notes, and snippets.

@jkwok91

jkwok91/Ring.pde Secret

Created April 29, 2014 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/66229991180326071fde to your computer and use it in GitHub Desktop.
Save jkwok91/66229991180326071fde to your computer and use it in GitHub Desktop.
edona's water toy
class Ring {
PVector location, velocity, acceleration;
color colr;
float radius;
Ring() {
location = new PVector(random(width),0);
velocity = new PVector(0,0);
acceleration = new PVector(0,0.005);
colr = color(255,0,0);
radius = 5;
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.x = 0;
acceleration.y = 0.005;
bounce();
}
void applyForce(PVector f) {
acceleration.add(f);
update();
}
void bounce() {
if (location.x < radius) {
velocity.x *= -.9;
location.x = radius;
} else if (location.x > width-radius) {
velocity.x *= -.9;
location.x = width-radius;
}
if (location.y < radius) {
velocity.y *= -.9;
location.y = radius;
}
}
void display() {
noFill();
stroke(colr);
strokeWeight(3);
ellipse(location.x,location.y,radius*2,radius*2);
}
}
/*
i have no fucking ideas
april 28th, 2014
*/
int goalL, goalR;
int launched, score;
PVector left_jet, right_jet;
ArrayList<Ring> rings;
ArrayList<Ring> landed;
void setup() {
size(300, 300);
goalL = 50;
goalR = 250;
left_jet = new PVector(goalL, height);
right_jet = new PVector(goalR, height);
reset();
}
void mousePressed() {
reset();
}
void reset() {
rings = new ArrayList<Ring>();
landed = new ArrayList<Ring>();
score = 0;
//restart
rings = new ArrayList<Ring>();
for (int i = 0; i < random(5,10); i++) {
Ring r = new Ring();
rings.add(r);
}
launched = rings.size();
}
void draw() {
background(255);
// draw instructions
fill(0);
text("aim for between the jets", width/3, height/4-10);
text("score: "+score, width/3, height/4);
text("missed: "+(launched-(rings.size()+score)), width/3, height/4+10);
noStroke();
// and jets
rect(goalL-10, left_jet.y-30, 20, 30);
rect(goalR-10, right_jet.y-30, 20, 30);
for (int i = rings.size()-1; i >= 0 ; i--) {
Ring r = rings.get(i);
r.update();
if ((r.location.y > height-10 ) && (r.location.x > goalL && r.location.x < goalR)) {
rings.remove(r);
landed.add(r);
score++;
}
if (r.location.y > height) {
rings.remove(r);
//missed++;
}
r.display();
}
for (Ring r : landed) {
r.colr = color(0, 255, 0);
r.update();
r.display();
if (r.location.y > height) {
rings.remove(r);
}
}
// create new each frame
if (random(0, 1) > 0.99) {
Ring r = new Ring();
rings.add(r);
launched++;
}
}
void jetPower(PVector jet) {
// make bubbles appear above the jet
bubbles(jet.x);
for (Ring r : rings) {
// calculate the distance from ring to jet
float dist = dist(r.location.x, r.location.y, jet.x, jet.y);
dist *= 2; // to weaken the force
float dx = r.location.x-jet.x; // negative because it pushes the ring upwards
float dy = r.location.y-jet.y;
r.applyForce(new PVector(dx/dist, dy/dist));
}
}
void bubbles(float x) {
strokeWeight(1);
noFill();
stroke(color(0,20,200));
line(x+10,height-30,x+10,height-50);
line(x,height-30,x,height-50);
line(x-10,height-30,x-10,height-50);
}
void shake() {
for (Ring r : rings) {
r.applyForce(new PVector(random(-1, 1), random(-1, 0)));
}
}
void keyPressed() {
if (key == 'f') {
jetPower(left_jet);
}
else if (key == 'j') {
jetPower(right_jet);
}
else if (key == ' ') {
shake();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment