xxxxxxxxxx
227
let mic;
let pitchValue = 0;
let character;
let obstacles = [];
let collectibles = [];
let micLevel;
let score = 0;
let obstacleInterval = 60; // Speed of creating obstacles- lower means more obstacles
let collectibleInterval = 180; // Speed of creating collectibles- lower means more collectibles
let isGameOver = false;
let gameStart = false;
function setup() {
song = loadSound('gameover.WAV');
createCanvas(800, 400);
characterY = height / 2; //character's y position starts from the middle
//get mic input
mic = new p5.AudioIn();
mic.start();
//create character
character = new Character();
//create game start button
gameStartButton = createButton("Start Game");
gameStartButton.class("game-button"); //to have button style applied
gameStartButton.position(width / 2 - 50, height / 2 + 110);
gameStartButton.mousePressed(beginGame);
gameStartButton.hide();
//create game retry button
gameOverButton = createButton("Retry");
gameOverButton.class("game-button"); //to have button style applied
gameOverButton.position(width / 2 - 35, height / 2 + 50);
gameOverButton.mousePressed(restartGame);
gameOverButton.hide();
}
function draw() {
background(220); //draw background to hide traces of character, obstacles, collectibles
if (isGameOver) { //Game Over Page
background(0);
textSize(48);
textAlign(CENTER);
fill(255, 0, 0);
text("Game Over", width / 2, height / 2 - 20);
textSize(32);
text("Score: " + score, width / 2, height / 2 + 20);
gameOverButton.show()
}
else if (!gameStart) { //Beginning Page
background(220);
//game instructions
textSize(48);
textAlign(CENTER);
fill(0);
text("Hey, Breathe", width / 2, height / 2 - 70);
textSize(20);
text("Move your character up and down by pressing up/down arrow keys.", width / 2, height / 2 - 10);
text("Dodge obstacles and collect collcecitlbes, the green circles.", width / 2, height / 2 + 20);
text("The speed of the map depends on the volume of your voice:", width / 2, height / 2 + 50);
text("Scream louder if you want to move faster.", width / 2, height / 2 + 80);
gameStartButton.show();
}
else if (gameStart) {
let yPos = characterY;
character.move(yPos);
character.display();
micLevel = mic.getLevel() * 15;
console.log(mic.getLevel());
console.log(micLevel);
let characterSpeed = map(micLevel, 0, 1, 1, 10);
if (frameCount % obstacleInterval === 0) { //create obstacles
obstacles.push(new Obstacle());
}
if (frameCount % collectibleInterval === 0) { //create collectibles
collectibles.push(new Collectible());
}
// Move and display obstacles
for (let i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move(characterSpeed);
obstacles[i].show();
if (gameStart && character.hits(obstacles[i])) { //check if it hits obstacles
console.log("Game Over");
isGameOver = true;
song.play(); //sound effect for when game over
}
if (obstacles[i] && obstacles[i].offscreen()) { //if offscreen, delete the obstacle from array
obstacles.splice(i, 1);
}
}
// Move and display collectibles
for (let i = collectibles.length - 1; i >= 0; i--) {
collectibles[i].move(characterSpeed);
collectibles[i].show();
if (character.collect(collectibles[i])) { //eat collectibles
collectibles.splice(i, 1);
score++;
}
if (collectibles[i] && collectibles[i].offscreen()) { //if offscreen, delete collectible from array
collectibles.splice(i, 1);
}
}
// score text in left top corner
textSize(32);
fill(0);
text("Score: " + score, 60, 30);
}
// key management
if (keyIsDown(UP_ARROW) && characterY > 20) {
characterY -= 1;
}
if (keyIsDown(DOWN_ARROW) && characterY < 380) {
characterY += 1;
}
}
class Character {
constructor() {
this.x = 50;
this.y = height / 2;
this.radius = 20;
this.speed = 5;
}
move(yPos) {
this.y = constrain(yPos, this.radius, height - this.radius);
//constrain(n, low, high) ==> limit the range of n as low ~ high
}
display() {
fill(255, 0, 0);
ellipse(this.x, this.y, this.radius * 2);
}
show() {
fill(255, 0, 0);
ellipse(50, this.y, 40, 40);
}
hits(obstacle) { //check distance between character and obstacle
let d1 = dist(50, characterY, obstacle.x, obstacle.y); //left top corner of obstacle
if (d1 < 20){
return (true);
}
let d2= dist(50, characterY, obstacle.x, obstacle.y + 40); //left down corner of obstacle
if (d2 < 20){
return (true);
}
return (false);
}
collect(collectible) {
let d1 = dist(50, characterY, collectible.x, collectible.y);
if (d1 < 20){
return (true);
}
else {
return (false);
}
}
}
class Obstacle {
constructor() {
this.x = width;
this.y = random(height);
}
move(characterSpeed) {
this.x -= characterSpeed; //Speed of obstacles moving
}
show() {
fill(0);
rect(this.x, this.y, 20, 40);
}
offscreen() { //check if it was created off screen
return this.x < -20;
}
}
class Collectible {
constructor() {
this.x = width;
this.y = random(height);
}
move(characterSpeed) {
this.x -= characterSpeed; //Speed of collectibles moving
}
show() {
fill(0, 255, 0);
ellipse(this.x, this.y, 20, 20);
}
offscreen() { //check if it was created off screen
return this.x < -20;
}
}
function beginGame() {
gameStart = true;
gameStartButton.hide();
obstacles = [];
collectibles = [];
score = 0;
loop();
}
function restartGame() {
startGame = true;
isGameOver = false;
gameOverButton.hide();
obstacles = [];
collectibles = [];
score = 0;
loop();
}