xxxxxxxxxx
208
let hand;
let hands,food;
let avocado;
let knife;
let blood;
let drops=[];
let score=[];
let counter=0;
let distance1;
let cutFinger=false;
let cutFood =false;
let bg;
//
let button;
let injured;
function preload(){
hand =loadImage("assets/hand.png");
avocado = loadImage("assets/avocado.png");
knife=loadImage("assets/cutlet.png");
blood = loadImage("assets/blood.png");
soundFormats("mp3","wav");
bg = loadSound("sound/scene1.mp3");
injured = loadSound("sound/hurt.mp3");
}
function setup() {
createCanvas(400, 400);
textSize(20);
textAlign(CENTER);
imageMode(CENTER);
rectMode(CENTER);
print(score.length);
button =createButton("startOver");
button.position(300,400);
reset();
hands=new Hands(120,200);
food=new Food(0,0);
bg.loop();
//injured.loop();
}
function mouseClicked(){
if(distance1<20){
cutFood=true;
print(counter,cutFood);
}
}
function mouseDragged(){
let distance2= dist(hands.x+30,hands.y,mouseX,mouseY);
if(distance2<20+38){
drops.push(new Blood(hands.x,hands.y,int(random(10,32))));
score.splice(0,1);
print(score.length,"Lose");
injured.play();
}
}
function mouseReleased(){
injured.stop();
}
function draw() {
button.mousePressed(reset);
scene();
}
class Hands{
constructor(x,y){
this.x=x;
this.y=y;
}
display(){
fill(255,100);
noStroke();
ellipse(this.x+30,this.y,80,80);
image(hand,this.x,this.y,150,150);
}
jitter(t,a){
this.x+=random(-a,a);
this.y+=random(-a,a);
}
}
function reset(){
clear();
counter=0;
for(let j =5;j>=0;j--){
score.push(new Bar(j*50+25,50));
}
}
function scene(){
if(counter===5&&score.length>0){
clear();
background("#F596AA");
fill(0);
noStroke();
text("You Win",width/2,height/2);
}else if(score.length<=0){
clear();
background("#F596AA");
fill(0);
noStroke();
text("You Lose",width/2,height/2);
text("Press button to startOver.",width/2,height/2+20);
}else{
background("#F596AA");
food.display2();
hands.display();
text(counter,350,60);
for(let j =0;j<score.length;j++){
score[j].display();
}
image(knife,mouseX,mouseY,150,150);
distance1= dist(food.x+208,food.y+200,mouseX,mouseY);
if(distance1<30){
hands.jitter(millis()/1000,.5);
food.jitter(millis()/1000,1);
}
for(let i =0;i<drops.length;i++){
drops[i].display();
drops[i].direction();
if(drops.length>50){
drops.splice(i,1);
}
}
if(cutFood){
counter+=1;
cutFood=false;
print(cutFood);
}
}
}
class Food extends Hands{
constructor(x,y) {
super();
this.x=x;
this.y=y;
}
display2(){
fill(255,100);
noStroke();
push();
translate(200,200);
ellipse(this.x+8,this.y,38,38);
rotate(-PI/6);
image(avocado,this.x,this.y,150,150);
pop();
}
}
class Blood{
constructor(x,y,size){
this.x=x;
this.y=y;
this.v= createVector(random(-5,5),random(1,5));
this.angle=random(-PI/6,PI/6);
this.size =size;
}
display(){
push();
translate(this.x,this.y);
rotate(this.angle);
image(blood,0,0,this.size,this.size);
pop();
}
direction(){
this.x+=this.v.x;
this.y+=this.v.y;
}
}
class Bar{
constructor(x,y){
this.x=x;
this.y=y;
}
display(){
fill(255,0,0);
stroke(255);
rect(this.x,this.y,50,20);
}
}