xxxxxxxxxx
140
/*
keyboard interaction
coin sound by
https://freesound.org/people/ProjectsU012/sounds/341695/
step sound by
https://freesound.org/people/MATRIXXX_/sounds/515783/
*/
var stepSound;
var coinSound;
function preload() {
coinSound = loadSound('coin.wav');
stepSound = loadSound('step.wav');
}
function setup() {
createCanvas(400, 400);
textSize(20);
}
function keyPressed() {
if (keyCode === 32) { // space
coinSound.play();
}
if (keyCode === 65,83,68,70) { // a
stepSound.play();
}
}
function draw() {
background(220);
if (keyIsPressed) {
if (keyCode === 65) { // a
background('rgb(145,197,241)');
square(350,130,70)
rect(210,235,17,45)//right arm up
rect(110,260,17,45)//left arm down
rect(170,300,15,70)//right leg
}
if (keyCode === 83) { // s
background('rgb(116,196,88)');
square(320,130,70)
fill('black')
rect(210,260,17,45)//right arm down
rect(110,230,17,45)//left arm up
rect(150,300,15,70)//left leg
}
if (keyCode === 68) { // d
background('rgb(217,128,233)');
square(250,130,70)
rect(210,235,17,45)//right arm up
rect(110,260,17,45)//left arm down
rect(170,300,15,70)//right leg
}
if (keyCode === 70) { // f
background('rgb(216,29,81)');
square(200,130,70)
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,50)//right leg
rect(150,300,15,70)//left leg
stroke(1)
line(00, 340, 400, 340);
}
}
if (!keyIsDown(70)) { // f
fill('rgb(0,0,0)');
noStroke();
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,50)//right leg
stroke(1)
line(00, 340, 400, 340);
}
fill(0);
text(keyIsPressed, 20, 20);
text(key, 100, 20);
text(keyCode, 200, 20);
// draw if coin sound is playing
if (coinSound.isPlaying()) {
fill('yellow');
noStroke();
circle(200, 110 - 25, 50);
fill('rgb(170,170,81)')
circle(200, 110 - 25, 35)
square(170,130,70)
circle(155,235,10)
circle(175,235,10)
circle(165,250,20)
fill('black')
rect(210,220,17,60)//right arm up
rect(110,260,17,40)//left arm down
rect(170,300,15,70)//right leg
}
textSize(15);
text("In order, press keys, 'A,S,D,F and lastly Space'", 50, 50)
text("to hear and see what happens. ", 100, 70);
}