Skip to content

Instantly share code, notes, and snippets.

@shyamalschandra
Created May 31, 2020 18:09
Show Gist options
  • Save shyamalschandra/f8ef1e76ab909e9f4bc19b0b65c097f6 to your computer and use it in GitHub Desktop.
Save shyamalschandra/f8ef1e76ab909e9f4bc19b0b65c097f6 to your computer and use it in GitHub Desktop.
A game with the grocery box trying to pick up the most white oranges coming from a fountain of orange generators.
import processing.sound.*;
import de.looksgood.ani.*;
import de.looksgood.ani.easing.*;
// Written by: Shyamal S. Chandra
// Date: May 31, 2020
// Code taken from example: https://processing.org/examples/simpleparticlesystem.html
PGraphics topLayer, bottomLayer;
Sound s;
ParticleSystem ps;
int upperLimit = 30;
int counter = upperLimit;
int x;
int numSeconds = 2;
float minX = 0;
float minY = 0;
int dia = 8;
int upperBound = 30;
int bucketwidth = 80;
int score = 0;
int index = -1;
void setup() {
frameRate(60);
size(800, 800);
ps = new ParticleSystem(new PVector(width/2, 50));
Ani.init(this);
topLayer = createGraphics(800,800);
bottomLayer = createGraphics(800,800);
}
void draw() {
counter--;
background(0);
if (counter == 0)
{
ps.addParticle();
counter = upperLimit;
dia += 1;
//bucketwidth += 2;
}
Particle testpart, minpart = null;
float minDistance = 32000;
for (int i = 0; i < ps.particles.size(); i++) {
testpart = ps.particles.get(i);
float distance = sqrt(pow(testpart.position.x-x, 2)+pow(testpart.position.y-700,2));
//print(distance);
if (minDistance > distance) {
minDistance = distance;
minX = testpart.position.x;
minY = testpart.position.y;
//print(minDistance);
index = i;
}
}
bottomLayer.beginDraw();
bottomLayer.background(0);
bottomLayer.textSize(32);
bottomLayer.text("Score: " + str(score) + "", 10,30);
bottomLayer.endDraw();
topLayer.beginDraw();
topLayer.background(0);
topLayer.line(minX, minY , x+40, 700);
topLayer.stroke(255);
topLayer.strokeWeight(1);
topLayer.endDraw();
image(topLayer, 0, 0);
image(bottomLayer, 0, 0);
fill(255);
rect(x, 700, bucketwidth, 80);
ps.run();
}
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector position) {
origin = position.copy();
particles = new ArrayList<Particle>();
}
void addParticle() {
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
// A simple Particle class
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
position = l.copy();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
position.add(velocity);
lifespan -= 1.0;
}
// Method to display
void display() {
stroke(255, lifespan);
fill(255, lifespan);
ellipse(position.x, position.y, dia, dia);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
void mousePressed(){
Ani.to(this, numSeconds, "x", mouseX);
if (minX > x && minY > 700 && minX < (x + bucketwidth) && minY < 780) {
if (index != -1.0f) {
Particle killerpart = ps.particles.get(index);
killerpart.lifespan = -1.0f;
SinOsc sin = new SinOsc(this);
sin.play(200, 0.2);
}
//println("Score: " + score++);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment