xxxxxxxxxx
615
let song;
let img;
let icon;
let fft;
let particles = [];
let myColors = ["#c14b1b","#4e3b2d","#a16124","#f4c656", "#b39e73", "#f6bd3a", "#eee8d8", "#6d1100", "#601818", "#0c0703", "#d5603d"];
let randColor;
let isOn = false;
let angle = 0;
let ellipseX = 0;
let ellipseY = 0;
let notes = [];
let result = "";
let timer = 0;
let score = 0;
function preload() {
song = loadSound('audio.mp3');
img = loadImage('img.jpg');
icon = loadImage('icon.png');
}
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
imageMode(CENTER);
rectMode(CENTER);
ellipseMode(CENTER);
fft = new p5.FFT(0.3);
img.filter(BLUR, 12);
//music cues
song.addCue(1.03, createNote);
song.addCue(2.63, createNote);
song.addCue(4.33, createNote);
song.addCue(5.73, createNote);
song.addCue(6.03, createNote);
song.addCue(7.53, createNote);
song.addCue(8.93, createNote);
song.addCue(9.32, createNote);
song.addCue(12.32, createNote);
song.addCue(13.90, createNote);
song.addCue(14.79, createNote);
song.addCue(15.61, createNote);
song.addCue(16.44, createNote);
song.addCue(17.31, createNote);
song.addCue(18.12, createNote);
song.addCue(18.86, createNote);
song.addCue(19.70, createNote);
song.addCue(20.57, createNote);
song.addCue(20.91, createNote);
song.addCue(21.43, createNote);
song.addCue(21.84, createNote);
song.addCue(22.17, createNote);
song.addCue(22.63, createNote);
song.addCue(23.03, createNote);
song.addCue(23.46, createNote);
song.addCue(23.86, createNote);
song.addCue(24.26, createNote);
song.addCue(24.71, createNote);
song.addCue(25.14, createNote);
song.addCue(25.46, createNote);
song.addCue(25.93, createNote);
song.addCue(26.30, createNote);
song.addCue(26.73, createNote);
song.addCue(27.17, createNote);
song.addCue(27.39, createNote);
song.addCue(27.89, createNote);
song.addCue(28.39, createNote);
song.addCue(28.80, createNote);
song.addCue(29.27, createNote);
song.addCue(29.70, createNote);
song.addCue(30.13, createNote);
song.addCue(30.57, createNote);
song.addCue(30.78, createNote);
song.addCue(31.15, createNote);
song.addCue(31.63, createNote);
song.addCue(32.06, createNote);
song.addCue(32.53, createNote);
song.addCue(32.95, createNote);
song.addCue(33.41, createNote);
song.addCue(33.85, createNote);
song.addCue(34.03, createNote);
song.addCue(34.88, createNote);
song.addCue(35.26, createNote);
song.addCue(35.54, createNote);
song.addCue(35.90, createNote);
song.addCue(36.31, createNote);
song.addCue(36.76, createNote);
song.addCue(37.24, createNote);
song.addCue(37.65, createNote);
song.addCue(38.04, createNote);
song.addCue(38.46, createNote);
song.addCue(38.86, createNote);
song.addCue(39.29, createNote);
song.addCue(39.67, createNote);
song.addCue(40.05, createNote);
song.addCue(40.48, createNote);
song.addCue(40.89, createNote);
song.addCue(41.31, createNote);
song.addCue(41.73, createNote);
song.addCue(42.12, createNote);
song.addCue(42.56, createNote);
song.addCue(42.98, createNote);
song.addCue(43.39, createNote);
song.addCue(43.82, createNote);
song.addCue(44.23, createNote);
song.addCue(44.61, createNote);
song.addCue(45.02, createNote);
song.addCue(45.44, createNote);
song.addCue(45.88, createNote);
song.addCue(46.33, createNote);
song.addCue(46.73, createNote);
song.addCue(47.16, createNote);
song.addCue(50.28, createNote);
song.addCue(53.36, createNote);
song.addCue(55.57, createNote);
song.addCue(56.92, createNote);
song.addCue(58.53, createNote);
song.addCue(59.38, createNote);
song.addCue(60.25, createNote);
song.addCue(60.64, createNote);
song.addCue(61.03, createNote);
song.addCue(61.49, createNote);
song.addCue(61.91, createNote);
song.addCue(62.37, createNote);
song.addCue(62.77, createNote);
song.addCue(63.21, createNote);
song.addCue(63.65, createNote);
song.addCue(64.04, createNote);
song.addCue(64.45, createNote);
song.addCue(64.83, createNote);
song.addCue(65.23, createNote);
song.addCue(65.84, createNote);
song.addCue(66.04, createNote);
song.addCue(66.46, createNote);
song.addCue(66.90, createNote);
song.addCue(67.30, createNote);
song.addCue(67.73, createNote);
song.addCue(68.14, createNote);
song.addCue(68.58, createNote);
song.addCue(68.98, createNote);
song.addCue(69.40, createNote);
song.addCue(69.83, createNote);
song.addCue(70.24, createNote);
song.addCue(70.62, createNote);
song.addCue(71.02, createNote);
song.addCue(71.43, createNote);
song.addCue(71.86, createNote);
song.addCue(72.69, createNote);
song.addCue(72.95, createNote);
song.addCue(73.38, createNote);
song.addCue(73.65, createNote);
song.addCue(74.10, createNote);
song.addCue(74.52, createNote);
song.addCue(74.90, createNote);
song.addCue(75.26, createNote);
song.addCue(75.65, createNote);
song.addCue(76.05, createNote);
song.addCue(76.46, createNote);
song.addCue(76.88, createNote);
song.addCue(77.29, createNote);
song.addCue(77.70, createNote);
song.addCue(78.14, createNote);
song.addCue(78.53, createNote);
song.addCue(78.94, createNote);
song.addCue(79.37, createNote);
song.addCue(79.63, createNote);
song.addCue(80.08, createNote);
song.addCue(80.56, createNote);
song.addCue(80.96, createNote);
song.addCue(81.39, createNote);
song.addCue(81.80, createNote);
song.addCue(82.21, createNote);
song.addCue(82.66, createNote);
song.addCue(83.08, createNote);
song.addCue(83.48, createNote);
song.addCue(83.91, createNote);
song.addCue(84.37, createNote);
song.addCue(84.75, createNote);
song.addCue(85.17, createNote);
song.addCue(85.60, createNote);
song.addCue(86.00, createNote);
song.addCue(86.36, createNote);
song.addCue(86.78, createNote);
song.addCue(87.66, createNote);
song.addCue(88.43, createNote);
song.addCue(89.10, createNote);
song.addCue(89.96, createNote);
song.addCue(90.76, createNote);
song.addCue(91.19, createNote);
song.addCue(91.66, createNote);
song.addCue(92.16, createNote);
song.addCue(92.59, createNote);
song.addCue(93.03, createNote);
song.addCue(93.42, createNote);
song.addCue(93.78, createNote);
song.addCue(94.20, createNote);
song.addCue(94.43, createNote);
song.addCue(94.84, createNote);
song.addCue(95.38, createNote);
song.addCue(95.84, createNote);
song.addCue(96.33, createNote);
song.addCue(96.74, createNote);
song.addCue(97.14, createNote);
song.addCue(97.54, createNote);
song.addCue(97.75, createNote);
song.addCue(98.19, createNote);
song.addCue(98.68, createNote);
song.addCue(99.08, createNote);
song.addCue(99.50, createNote);
song.addCue(99.94, createNote);
song.addCue(100.38, createNote);
song.addCue(100.83, createNote);
song.addCue(101.02, createNote);
song.addCue(101.50, createNote);
song.addCue(102.04, createNote);
song.addCue(102.23, createNote);
song.addCue(102.64, createNote);
song.addCue(103.00, createNote);
song.addCue(103.41, createNote);
song.addCue(103.75, createNote);
song.addCue(104.19, createNote);
song.addCue(104.58, createNote);
song.addCue(105.02, createNote);
song.addCue(105.43, createNote);
song.addCue(105.83, createNote);
song.addCue(106.36, createNote);
song.addCue(106.55, createNote);
song.addCue(106.94, createNote);
song.addCue(107.40, createNote);
song.addCue(107.82, createNote);
song.addCue(108.23, createNote);
song.addCue(108.64, createNote);
song.addCue(109.09, createNote);
song.addCue(109.52, createNote);
song.addCue(109.95, createNote);
song.addCue(110.38, createNote);
song.addCue(110.81, createNote);
song.addCue(111.22, createNote);
song.addCue(111.62, createNote);
song.addCue(112.03, createNote);
song.addCue(112.43, createNote);
song.addCue(112.96, createNote);
song.addCue(113.15, createNote);
song.addCue(113.63, createNote);
song.addCue(114.12, createNote);
song.addCue(117.28, createNote);
song.addCue(120.11, createNote);
song.addCue(122.15, createNote);
song.addCue(123.26, createNote);
song.addCue(124.16, createNote);
song.addCue(124.87, createNote);
song.addCue(125.60, createNote);
song.addCue(126.41, createNote);
song.addCue(127.23, createNote);
song.addCue(127.68, createNote);
song.addCue(128.10, createNote);
song.addCue(128.50, createNote);
song.addCue(128.91, createNote);
song.addCue(129.32, createNote);
song.addCue(129.71, createNote);
song.addCue(130.09, createNote);
song.addCue(130.46, createNote);
song.addCue(130.96, createNote);
song.addCue(131.36, createNote);
song.addCue(131.75, createNote);
song.addCue(132.15, createNote);
song.addCue(132.58, createNote);
song.addCue(132.98, createNote);
song.addCue(133.14, createNote);
song.addCue(133.49, createNote);
song.addCue(133.90, createNote);
song.addCue(134.30, createNote);
song.addCue(134.73, createNote);
song.addCue(135.14, createNote);
song.addCue(135.53, createNote);
song.addCue(135.98, createNote);
song.addCue(136.38, createNote);
song.addCue(136.80, createNote);
song.addCue(137.24, createNote);
song.addCue(137.62, createNote);
song.addCue(138.04, createNote);
song.addCue(138.42, createNote);
song.addCue(138.93, createNote);
song.addCue(139.78, createNote);
song.addCue(140.00, createNote);
song.addCue(140.38, createNote);
song.addCue(140.59, createNote);
song.addCue(141.05, createNote);
song.addCue(141.45, createNote);
song.addCue(141.82, createNote);
song.addCue(142.20, createNote);
song.addCue(142.61, createNote);
song.addCue(142.84, createNote);
song.addCue(143.06, createNote);
song.addCue(143.28, createNote);
song.addCue(143.65, createNote);
song.addCue(143.87, createNote);
song.addCue(144.28, createNote);
song.addCue(144.67, createNote);
song.addCue(144.94, createNote);
song.addCue(145.28, createNote);
song.addCue(145.50, createNote);
song.addCue(145.91, createNote);
song.addCue(146.34, createNote);
song.addCue(146.79, createNote);
song.addCue(147.19, createNote);
song.addCue(147.59, createNote);
song.addCue(148.01, createNote);
song.addCue(148.43, createNote);
song.addCue(148.83, createNote);
song.addCue(149.10, createNote);
song.addCue(149.43, createNote);
song.addCue(149.70, createNote);
song.addCue(150.02, createNote);
song.addCue(150.38, createNote);
song.addCue(150.82, createNote);
song.addCue(151.27, createNote);
song.addCue(151.73, createNote);
song.addCue(152.20, createNote);
song.addCue(152.60, createNote);
song.addCue(153.05, createNote);
song.addCue(153.48, createNote);
song.addCue(153.88, createNote);
song.addCue(154.70, createNote);
song.addCue(158.07, createNote);
song.addCue(160.26, createNote);
song.addCue(162.09, createNote);
song.addCue(163.84, createNote);
song.addCue(165.61, createNote);
song.addCue(167.74, createNote);
song.addCue(169.90, createNote);
}
function draw() {
translate (width/2, height/2);
fft.analyze()
amp = fft.getEnergy(20, 200);
//bg img
push();
if (amp > 200) {
scale (1.03);
}
image(img, 0, 0, width, height);
pop();
//alpha
let alpha = map(amp, 0, 255, 160, 130);
fill(0, alpha);
noStroke();
rect (0, 0, width, height);
//center circle
stroke(220);
strokeWeight(3);
// fill (180,120,140);
noFill();
ellipse(0,0,450,450);
// push();
// translate(150,0);
// rotate(angle);
// angle++;
icon.resize(170,170);
image(icon,150,0);
// pop();
image(icon,-150,0);
image(icon,0,150);
image(icon,0,-150);
points(0,-150,150,150);
points(0, 150,150,150);
points(150, 0,150,150);
points(-150, 0,150,150);
for (let i = 0; i < notes.length; i++){
notes[i].move();
notes[i].show();
notes[i].miss(0,0);
}
//moveable circle
fill(255,60,60,80);
stroke(220);
strokeWeight(3);
ellipse(ellipseX, ellipseY, 150);
//function for when key is pressed
//top
if (keyIsDown(87)){
ellipseY = -150;
}
//left
if (keyIsDown(65)){
ellipseX = -150;
}
//bottom
if (keyIsDown(83)){
ellipseY = 150;
}
//bottom
if (keyIsDown(88)){
ellipseY = 150;
}
//right
if (keyIsDown(68)){
ellipseX = 150;
}
textSize(32);
stroke(10);
fill(200)
text (score, -360,290);
//score text
if (timer > 0){
textSize(32);
text(result,-40,290);
timer -=1;
}
//particles
let p = new Particle();
particles.push(p);
for (let i = particles.length - 1; i >= 0; i--){
if (!particles[i].edges()){
particles[i].update(amp > 200);
particles[i].show();
} else {
particles.splice(i, 1);
}
}
}
//function to return square to original position when key is released
function keyReleased() {
if (ellipseX === 200) {
ellipseX = ellipseX;
} else {
ellipseX = 0;
}
if (ellipseY === 200) {
ellipseY = ellipseY;
} else {
ellipseY = 0;
}
return false;
}
function keyPressed() {
//top
if (keyCode === 87){
result = notes[0].contains(0,-150);
notes.splice(0,1);
}
//left
if (keyCode === 65){
result = notes[0].contains(-150,0);
notes.splice(0,1);
}
//bottom
if (keyCode === 83 || keyCode === 88){
result = notes[0].contains(0,150);
notes.splice(0,1);
}
//right
if (keyCode === 68){
result = notes[0].contains(150,0);
notes.splice(0,1);
}
}
function points (x,y,s,tl){
// fill(180,140,180);
noFill();
noStroke();
ellipse (x,y,s,tl);
}
function createNote (){
let direction = int(random(4));
//top
if (direction == 0){
append(notes, new note (0,-500,0,4.0,133,131,58));
}
//bottom
if (direction == 1){
append(notes, new note (0,500,0,-4.0,245,180,34));
}
//left
if (direction == 2){
append(notes, new note (-500,0,4.0,0,175,71,32));
}
//right
if (direction == 3){
append(notes, new note (500,0,-4.0,0,190,170,119));
}
}
//play&pause song
function mouseClicked(){
if (song.isPlaying()){
song.pause();
noLoop();
isOn = false;
} else {
song.play();
loop();
isOn = true;
mode = 1;
}
}
//Particles
class Particle {
constructor() {
this.pos = p5.Vector.random2D().mult(250);
this.vel = createVector(0,0);
this.acc = this.pos.copy().mult(random(0.0001, 0.00001));
this.w = random(3, 5);
}
update(cond){
this.vel.add(this.acc);
this.pos.add(this.vel);
if (cond) {
this.pos.add(this.vel);
this.pos.add(this.vel);
this.pos.add(this.vel);
}
}
edges() {
if (this.pos.x < -width /2 || this.pos.x > width/2 || this.pos.y < -height /2 || this.pos.y > height/2){
return true;
} else{
return false;
}
}
show() {
noStroke();
randColor=random(myColors.length);
randColor=floor(randColor);
fill(myColors[randColor]);
ellipse(this.pos.x, this.pos.y, this.w);
}
}
class note {
constructor (x,y,xs,ys,r,g,b) {
this.x = x;
this.y = y;
this.s = 150;
this.xspeed = xs;
this.yspeed = ys;
this.c = color(r,g,b);
}
move(){
this.x += this.xspeed;
this.y += this.yspeed;
}
show(){
fill(this.c);
ellipse(this.x, this.y, this.s);
}
contains (ellipseX,ellipseY){
let distance = dist(this.x, this.y, ellipseX, ellipseY);
if (distance < 20){
timer = 50
score += 50
return "Perfect";
} else if (distance < 50){
score += 25
timer = 50
return "Okay";
} else {
score += 0
timer = 50
return "Terrible";
}
}
miss (ellipseX,ellipseY){
let distance = dist(this.x, this.y, ellipseX, ellipseY);
if (distance < 20){
score += 0
timer = 50
result = "Miss";
notes.splice(0,1);
}
}
}