class Food {
int s = 10;
float xPos = random(width);
float yPos = random(height);
void drawFood(){
int n = int(random(5));
if (n == 0) { fill(170, 0, 11); //dark red
} else if (n == 1) { fill (255, 0, 0); //bright red
} else if (n == 2) { fill (242, 247, 0); //yellow
} else if (n == 3) { fill (250, 66, 94); //red
} else if (n == 4) { fill (245, 147, 0); //orange
} ellipse(xPos, yPos, s, s);
}
boolean isTouching(int x, int y) {
return dist(x, y, xPos, yPos) <= s;
}
}
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
Minim minim;
AudioSample bite;
AudioSample move;
AudioSample bomb;
ArrayList<Food> list = new ArrayList<Food>();
float[] x = new float[100];
float[] y = new float[100];
float segLength = 0.1;
void setup() {
size(500, 500);
smooth();
minim = new Minim(this);
bite = minim.loadSample("Dragon Bite.wav");
move = minim.loadSample("Sliding.wav");
bomb = minim.loadSample("Explosion 2.wav");
}
void draw() {
//clear clutter
background(225);
//wormy
strokeWeight(10);
stroke(0);
dragSegment(0, mouseX, mouseY);
for(int i=0; i<x.length-1; i++) {
dragSegment(i+1, x[i], y[i]);
}
noStroke();
//wormy eye
fill(0);
ellipse(mouseX, mouseY, 15, 15);
fill(255);
ellipse(mouseX, mouseY, 10, 10);
fill(0);
ellipse(mouseX, mouseY, 5, 5);
//eat food
for (int i = 0; i < list.size(); ++i) {
Food meal = list.get(i);
if (meal.isTouching(mouseX, mouseY)) {
list.remove(meal);
bite.trigger();
if (segLength > 10) { bomb.trigger();
for (int f = 0; f < list.size(); ++f) {
meal = list.get(f);
if (f > -1) {
list.remove(meal);
f -= 1;
}
} segLength = 0.1;
} else if (segLength > 5) { segLength = segLength + 0.2;
} else {segLength = segLength + 0.1;
} i -= 1;
}
}
if (mousePressed && (mouseButton == RIGHT)){
Food meal = new Food();
list.add(meal);
}
//draw current meals
for(Food meal : list){
meal.drawFood();
}
}
//how the worm flows
void dragSegment(int i, float xin, float yin) {
float dx = xin - x[i];
float dy = yin - y[i];
float angle = atan2(dy, dx);
x[i] = xin - cos(angle) * segLength;
y[i] = yin - sin(angle) * segLength;
segment(x[i], y[i], angle);
}
//each segment of the worm
void segment(float x, float y, float a) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
popMatrix();
}
//spawn more food
void mouseClicked() {
Food meal = new Food();
list.add(meal);
}
//void mouseMoved() {
// move.trigger();
//}
void stop() {
if (bite != null) bite.close();
if (move != null) move.close();
minim.stop();
super.stop();
}
based on some principles of the game Snake. also based on code from http://www.processing.org/learning/topics/follow3.html .
Rather than random colours, I've reduced them to fire coloured ones which helps the reasoning behind him exploding after eating too much. Also added right click & hold to keep generating food.