xxxxxxxxxx
237
/*
keyboard interaction
coin sound by
https://freesound.org/people/ProjectsU012/sounds/341695/
step sound by
https://freesound.org/people/MATRIXXX_/sounds/515783/
death sound by
https://freesound.org/people/thearchiveguy99/sounds/642431/
*/
var stepSound;
var coinSound;
var hatImage;
var deathSound;
var winnerImage;
var coinsImage;
function preload() {
coinSound = loadSound("coin.wav");
stepSound = loadSound("step.wav");
hatImage = loadImage("hat.png");
deathSound = loadSound("death.flac");
winnerImage = loadImage("winner.png");
coinsImage = loadImage("coins.png");
}
function setup() {
createCanvas(400, 400);
textSize(20);
}
function keyPressed() {
if (keyCode === 32) {
// space
coinSound.play();
} else if (
keyCode === 65 ||
keyCode === 83 ||
keyCode === 68 ||
keyCode === 70
) {
// a,s,d,f
stepSound.play();
} else {
// any other key
deathSound.play();
}
}
function draw() {
background(220);
if (keyIsPressed) {
if (keyCode === 65) {
// a
background("rgb(145,197,241)");
fill("black");
textSize(25);
stroke(1);
text("Good job, keep going!", 80, 50);
square(350, 130, 70); // coin box
rect(150, 260, 35, 70); //torso
rect(170, 260, 50, 20); //right arm
rect(110, 260, 50, 20); //left arm
rect(210, 235, 17, 45); //right arm up
rect(110, 260, 17, 45); //left arm down
rect(170, 300, 15, 70); //right leg
rect(150, 300, 15, 55); //left leg
circle(165, 240, 50); //head
fill("white");
circle(155, 235, 10); // right eye
circle(175, 235, 10); //left eye
arc(165, 247, 15, 15, 0, 3.14);
image(hatImage, -50, 20, 400, 400);
} else if (keyCode === 83) {
// s
background("rgb(116,196,88)");
fill("black");
textSize(25);
stroke(1);
text("You're doing great", 100, 50);
square(320, 130, 70);// coin box
fill("black");
rect(150, 260, 35, 70); //torso
rect(170, 260, 50, 20); //right arm
rect(110, 260, 50, 20); //left arm
rect(210, 260, 17, 45); //right arm down
rect(110, 230, 17, 45); //left arm up
rect(150, 300, 15, 70); //left leg
rect(170, 300, 15, 55); //right leg
circle(165, 240, 50); //head
fill("white");
circle(155, 235, 10); // right eye
circle(175, 235, 10); //left eye
arc(165, 247, 15, 15, 0, 3.14);
image(hatImage, -50, 20, 400, 400);
} else if (keyCode === 68) {
// d
background("rgb(217,128,233)");
fill("black");
textSize(25);
stroke(1);
text("Box is getting closer!", 80, 50);
square(250, 130, 70);// coin box
rect(150, 260, 35, 70); //torso
rect(170, 260, 50, 20); //right arm
rect(110, 260, 50, 20); //left arm
rect(210, 235, 17, 45); //right arm up
rect(110, 260, 17, 45); //left arm down
rect(170, 300, 15, 70); //right leg
rect(150, 300, 15, 55); //left leg
circle(165, 240, 50); //head
fill("white");
circle(155, 235, 10); // right eye
circle(175, 235, 10); //left eye
arc(165, 247, 15, 15, 0, 3.14);
image(hatImage, -50, 20, 400, 400);
} else if (keyCode === 70) {
// f
background("rgb(19,29,100)");
fill("black");
textSize(25);
stroke(1);
text("Now, press Space key!", 80, 50);
square(200, 130, 70);// coin box
rect(210, 260, 17, 45); //right arm down
rect(110, 230, 17, 45); //left arm up
circle(165, 240, 50); //head
rect(150, 260, 35, 70); //torso
rect(170, 260, 50, 20); //right arm
rect(110, 260, 50, 20); //left arm
rect(150, 300, 15, 50); //left leg
rect(170, 300, 15, 55); //right leg
rect(150, 300, 15, 70); //left leg
fill("white");
circle(155, 235, 10); // right eye
circle(175, 235, 10); //left eye
arc(165, 247, 15, 15, 0, 3.14);
stroke(1);
line(00, 340, 400, 340);
image(hatImage, -50, 20, 400, 400);
} else if (keyCode !== 32) {
// any other key
background("rgb(218,70,70)");
fill("rgb(0,0,0)");
textSize(18);
stroke(1);
text("I told you to FOLLOW THE ORDER!", 60, 50);
textSize(28);
text("now, you're DEAD",90,160);
fill("black");
circle(165, 240, 50); //head
rect(210, 235, 17, 45); //right arm up
rect(110, 235, 17, 45); //left arm up
circle(165, 240, 50); //head
rect(150, 260, 35, 70); //torso
rect(170, 260, 50, 20); //right arm
rect(110, 260, 60, 20); //left arm
rect(96, 315, 70, 15); //right leg
rect(170, 315, 70, 15); //left leg
rect(96, 300, 15, 25); //right foot
rect(225, 300, 15, 25); //left foot
fill("rgb(172,3,3)");
textSize(15);
text("X X", 150, 238);
circle(165, 250, 16); //mouth
}
}
if (!keyIsDown(70)) {
// f
fill("rgb(0,0,0)");
noStroke();
stroke(1);
line(00, 340, 400, 340);
}
if (keyIsDown(32)) {
//space key
fill(0);
text(keyIsPressed, 20, 20);
text(key, 100, 20);
text(keyCode, 200, 20);
fill("rgb(170,170,81)");
square(170, 130, 70); // coin box
image(winnerImage, 127, 175, 75, 65);
fill("black");
circle(165, 240, 50); //head
rect(150, 260, 35, 70); //torso
rect(170, 260, 50, 20); //right arm
rect(110, 260, 50, 20); //left arm
rect(210, 220, 17, 60); //right arm up
rect(110, 260, 17, 40); //left arm down
rect(170, 300, 15, 70); //right leg
rect(150, 300, 15, 55); //left leg
fill("rgb(170,170,81)");
circle(155, 235, 10); // right eye
circle(175, 235, 10); //left eye
circle(165, 250, 20); //mouth
// draw if coin sound is playing
if (coinSound.isPlaying()) {
image(coinsImage, 5, 5, 400, 400);
fill("yellow");
noStroke();
circle(200, 110 - 25, 50); //outer coin
fill("rgb(170,170,81)");
circle(200, 110 - 25, 35); //inner coin
fill("black");
textSize(15);
stroke(1);
text("Congrats, you found COIN BOX!", 100, 50);
}
}
fill("black");
textSize(15);
text("In order, press keys, 'A,S,D,F and lastly Space'", 50, 370);
text("to hear and see what happens. ", 100, 390);
}