class Food {
int s = 10;
float xPos = random(width);
float yPos = random(height);
color colour;
Food(color colour) {
this.colour = colour;
}
void drawFood() {
fill(colour);
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.*;
color snakeColour = color(255, 255, 255);
int currentColour = 0;
color[] colours = {
color(191, 2, 2),
color(0, 78, 209),
color(242, 247, 0),
color(5, 170, 3),
color(245, 147, 0),
color(149, 2, 152),
color(0, 191, 214),
color(0, 103, 1)
};
Food currentFood;
Minim minim = new Minim(this);
int currentSound = 0;
boolean soundUp = true;
String[] xyloNames = {
"F_01",
"F_02",
"G_01",
"G_02",
"A_01",
"A_02",
"B",
"C_01"
};
AudioSample[] xylos = new AudioSample[xyloNames.length];
float[] x = new float[100];
float[] y = new float[100];
float segLength = 0.1;
int b = 0;
//color of the food
int c = 0;
//up or down the color selection
boolean up = true;
void setup() {
size(500, 500);
smooth();
noCursor();
for (int i = 0; i < xylos.length; i++) {
xylos[i] =
minim.loadSample("Xylophone_Single_Note_" + xyloNames[i] + ".mp3");
}
currentFood = new Food(colours[0]);
}
void draw() {
//clear clutter
background(0, 0, 0);
//worm
strokeWeight(10);
stroke(snakeColour);
dragSegment(0, mouseX, mouseY);
for (int i = 1; i < x.length; i++) {
dragSegment(i, x[i - 1], y[i - 1]);
}
noStroke();
//worm eye
fill(snakeColour);
ellipse(mouseX, mouseY, 15, 15);
fill(0);
ellipse(mouseX, mouseY, 10, 10);
fill(snakeColour);
ellipse(mouseX, mouseY, 5, 5);
//eat food and spawn new one
if (currentFood.isTouching(mouseX, mouseY)) {
snakeColour = currentFood.colour;
nextAudio().trigger();
//play x amounts of notes
currentFood = new Food(nextFoodColour());
//reset worm once too long
if (segLength > 10) {
segLength = 0.1;
} else {
segLength = segLength + 0.1;
}
}
//draw current meal
currentFood.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();
}
color nextFoodColour() {
currentColour += 1;
if (currentColour >= colours.length) {
currentColour = 0;
}
return colours[currentColour];
}
AudioSample nextAudio() {
if (soundUp) {
currentSound += 1;
} else {
currentSound -= 1;
}
if (currentSound < 0 || currentSound >= xylos.length) {
if (currentSound < 0) {
currentSound = 0;
} else {
currentSound = xylos.length - 1;
}
soundUp = !soundUp;
return nextAudio();
}
return xylos[currentSound];
}
//close sounds
void stop() {
for (AudioSample s : xylos) {
s.close();
}
minim.stop();
super.stop();
}
credit as per previous
and to Tim Jones for clearing parts up and helping when I got stuck