NATT'S DESIGN JOURNEY
DES240 -Final code (two)
PImage[] circleImages = new PImage[2];//fish and jelly
Circle[] circles = new Circle[12];//Having multiple jellyfish and fish
PImage backgroundImage; //the underwater floor
PImage[] images = new PImage[4]; //Bacteria images
ArrayList<Particle> particles = new ArrayList<Particle>(); //for the particles system
PVector circlePosition = new PVector(500, 1000); //placement of hand, followed by particle system
int frameCounter;
boolean stopAnimation = false; // stops the jellyfish and fish
int animationStartTime; //when the hand starts
boolean particleSystemActive; //the bacterias scatter
boolean handMovingDown; //the hand moving down
float particleSpeed = 2.7; //how fast the bacteria moves
float particleSpeedDecay = 0.007; //how fast it desolves/disappears
float handFloatSpeed = 5.8; //how fast the hand moves downwards
float minY = 0; //how high the jellyfish and fish can go
float maxY = 600; //how low the jellyfish and fish can go
void setup() {
size(1080, 1080);
frameRate(60);
// Load images
backgroundImage = loadImage("background.jpeg");
circleImages[0] = loadImage("fish.png");
circleImages[1] = loadImage("jelly.png");
images[0] = loadImage("bthree.png");
images[1] = loadImage("btwo.png");
images[2] = loadImage("bone.png");
images[3] = loadImage("hand.png");
// Initialize circles
initializeCircles(); //links to display - main function
// Initialize animation
animationStartTime = millis(); //main function
}
void draw() {
image(backgroundImage, 0, 0, 1080, 1080);
if (!stopAnimation) { //flag is not set.
boolean allLeftScreen = true;
for (Circle circle : circles) { //circles are basically jellyfish and fish, think
circle.update();
circle.display();
if (!(circle.x > width || circle.x < -circle.diameter)) {
allLeftScreen = false; //at least one jellyfish/fish is still on the screen
}
}
if (allLeftScreen) { // check if the jellyfish/fish have left the screen
stopAnimation = true; // flag set to true
animationStartTime = millis();
}
} else if (millis() - animationStartTime > 1000) { //animation as in the hand and bacteria apsect
if (!particleSystemActive) {
drawCircle();
}
if (particleSystemActive) {
frameCounter++; //makes the particle system work
createParticle();
particleSpeed = max(0, particleSpeed - particleSpeedDecay); //how wide the bacteria spreads
handMovingDown = frameCount >= 70 * 2; // how long the hand stays paused in the screen
}
if (handMovingDown) {
circlePosition.y += handFloatSpeed;
}
if (circlePosition.y > height + 369) {
resetAnimation(); //the loop
}
image(images[3], circlePosition.x - 234, circlePosition.y - 127, 600, 600);
updateAndDrawParticles();
}
}
void resetAnimation() {
initializeCircles();
animationStartTime = millis();
stopAnimation = false;
frameCounter = 0;
particleSystemActive = false;
circlePosition.y = height + 269; // Move the hand off-screen
particleSpeed = 2.7;
handMovingDown = false;
minY = 0; // Reset minY
maxY = 600; // Reset maxY
//handFloatSpeed = 3.6; //how fast the hand moves downwards
}
void initializeCircles() { //jellyfish and fish functions
for (int i = 0; i < 12; i++) {
float x = (i < 4 || i >= 7) ? -222 : width;
float y = random(minY, maxY);
float speed = (i < 4 || i >= 7) ? random(5, 7) * 7 : random(-5, -7) * 7;
PImage circleImage = (i < 4 || i >= 7) ? circleImages[0] : circleImages[1];
float diameter = 222;
circles[i] = new Circle(x, y, circleImage, speed, diameter);
}
}
void drawCircle() { //hand
image(images[3], circlePosition.x - 235, circlePosition.y - 130, 600, 600); //size of hand
circlePosition.y -= 4; // speed of hand going up
particleSystemActive = circlePosition.y <= 669;
}
void createParticle() {
if (frameCounter >= 7) {
Particle p = new Particle(circlePosition.copy(), images[int(random(3))]); // three bacterias, think
p.speed = particleSpeed;
particles.add(p);
frameCounter = 0;
}
}
void updateAndDrawParticles() { // bacteria after first round
for (int i = particles.size() - 1; i >= 0; i--) {
Particle particle = particles.get(i);
particle.update();
particle.display();
if (particle.isDead()) {
particles.remove(i);
}
}
}
class Circle { //jellyfish and fish
float x, y, speed, diameter;
PImage img;
float scale = 1.7;
Circle(float x, float y, PImage img, float speed, float diameter) {
this.x = x;
this.y = y;
this.img = img;
this.speed = speed;
this.diameter = diameter;
}
void update() {
x += speed;
y = constrain(y, minY, maxY);
if (speed < 0 && x > width) {
x = -diameter;
speed = random(-5, 3) * 3;
}
}
void display() {
float scaledDiameter = diameter * scale;
float offsetX = (diameter - scaledDiameter) / 7;
float offsetY = (diameter - scaledDiameter) / 7;
image(img, x + offsetX, y + offsetY, scaledDiameter, scaledDiameter);
}
}
class Particle { //bacteria, i think
PVector position, velocity;
float lifespan = 200, speed;
PImage particleImage;
Particle(PVector _position, PImage img) {
position = _position;
velocity = PVector.random2D().mult(random(2, 5));
particleImage = img;
speed = 0.0005;
}
void update() {
speed -= particleSpeedDecay;
velocity.normalize().mult(speed);
position.add(velocity);
lifespan -=4.5;
}
void display() {
noStroke();
if (lifespan <= 0) return;
image(particleImage, position.x - 0.007, position.y - 0.2, 111, 111);
}
boolean isDead() {
return lifespan < 0; //stops
}
}​​