xxxxxxxxxx
var richie;
var honker = [];
var gameMode = 0;
var contactedHonker = 0; //used for remembering sound of last Honker touched
var deadHonkerCounter = 0; //used for testing if all Honkers are dead
var wonLastGame = false; //used when resetting the game
var score = 0; //self-explanatory
//loading type
var centuryGothic;
//images for honkers
var honkOpen;
var honkShut;
//images for Richie
var richieOpen;
var richieShut;
//sounds
var soundHonk;
var soundBoing;
var soundNoisemaker;
var soundName;
function preload() {
centuryGothic = loadFont("century_gothic.ttf");
soundHonk = loadSound("honk.mp3");
soundBoing = loadSound("boioing.mp3");
soundNoisemaker = loadSound("noisemaker.mp3");
honkOpen = loadImage("honk_open.png");
honkShut = loadImage("honk_shut.png");
richieOpen = loadImage("richie_open.png");
richieShut = loadImage("richie_shut.png");
}
function setup() {
createCanvas(720, 720);
textFont(centuryGothic);
textSize(24);
smooth();
background(0);
textAlign(CENTER);
// rectMode(CENTER);
ellipseMode(CENTER);
imageMode(CENTER);
richie = new Protagonist();
for (var i = 0; i < 3; i++) {
honker.push(new Honker());
}
gameMode = 0;
soundName = "honk";
}
function draw() {
// First mode: Title page
if (gameMode === 0) {
background(150);
text("Welcome to Honkerville!", width/2, height/2-280);
text("Each Honker that chases you makes a sound.", width/2, height/2-230);
text("Your sound must match the Honker's sound", width/2, height/2 -150);
text("before you touch it.", width/2, height/2-126);
text("Move with the arrow keys.", width/2, height/2-50);
text("Change your sound by hitting", width/2, height/2);
text("number keys 1, 2, or 3.", width/2, height/2+24);
text("Try out some sounds now by hitting 1, 2, or 3.", width/2, height/2 + 65);
text("When you switch sounds during the game,", width/2, height/2+148);
text("you'll open your mouth and make that sound.", width/2, height/2+172);
text("Hit return to start the game.", width/2, height/2+220);
}
// Game play
else if (gameMode === 1 || gameMode === 2) {
background(0);
fill(0);
stroke(255, 0, 0);
strokeWeight(4);
rect(0, 0, width, height);
noStroke();
fill(255);
text("Score: " + score, 500, 36);
if (gameMode === 1) {
fill(255);
text("Score: " + score, 500, 36);
text("Your current sound is the " + soundName, width/2, height - 20);
richie.move();
richie.display();
//need to make sure Honkers won't cover each other;
//include if statement here to prevent overlap before implementing their normal move functions
//section below determines where the honkers will go
for (let a = 0; a < honker.length-1; a++) {//this compares all the honkers
for (let b = a+1; b < honker.length; b++) { //against all the other ones
if (honker[a].alive && (honker[a].x < richie.x) && (abs((honker[a].x + honker[a].xSpeed) - honker[b].x) < 40)) { // comparing when honker to left of richie;
//if they would be less than 30 px apart after their next move, honker[a] won't move
// note, may be more appropriate actually to check all comparisons, since using potential moves
honker[a].xPause = true;
}
if (honker[a].alive && honker[a].x > richie.x && abs((honker[a].x - honker[a].xSpeed) - honker[b].x) < 40) { // comparing when honker to the right of richie
// if they would be less than 30 px apart after their next move, honker[a] won't move
// note, may be more appropriate actually to check all comparisons, since using potential moves
honker[a].xPause = true;
}
if (honker[a].alive && honker[a].y < richie.y && abs((honker[a].y + honker[a].ySpeed) - honker[b].y) < 40) { // comparing when honker above richie;
//if they would be less than 30 px apart after their next move, honker[a] won't move
// note, may be more appropriate actually to check all comparisons, since using potential moves
honker[a].yPause = true;
}
if (honker[a].alive && honker[a].y > richie.y && abs((honker[a].y - honker[a].ySpeed) - honker[b].y) < 40) { // comparing when honker below richie
// if they would be less than 30 px apart after their next move, honker[a] won't move
// note, may be more appropriate actually to check all comparisons, since using potential moves
honker[a].yPause = true;
}
}
}
for (let i = 0; i < honker.length; i++) {
//this draws all the honkers
honker[i].moveX();
honker[i].moveY();
honker[i].display();
honker[i].xPause = false;
honker[i].yPause = false;
//characters make contact
let distance = int(dist(honker[i].x, honker[i].y, richie.x, richie.y));
if (distance < 65) {
// when Richie touches a Honker, this stores which one
contactedHonker = i;
// if the sounds don't match, Richie dies, the game ends
if (richie.sound !== honker[i].sound) {
gameMode = 2;
}
// if the sounds match, the Honker dies
if (richie.sound === honker[i].sound) {
honker[i].alive = false;
deadHonkerCounter = 0;
score += 1;
// need to determine whether there are no more honkers
// if the last Honker just died, enter gameMode 2 no matter whether you won or lost
for (let j=0; j<honker.length; j++) {
if (honker[j].alive == false) {
deadHonkerCounter++;
}
}
if (deadHonkerCounter == honker.length) {
gameMode=2;
}
}
}
}
}
else if (gameMode===2) {
for (let i = 0; i < honker.length; i++) { //kills all Honkers so sound stops repeating
honker[i].alive = false;
}
if (richie.sound==honker[contactedHonker].sound) {
//victory image here?
text("You win!", width/2, height/2);
text("Your score so far is " + score, width/2, height/2+24);
text("Press S to keep going.", width/2, height/2+72);
wonLastGame = true;
}
if (richie.sound!=honker[contactedHonker].sound) {
fill(255);
text("Sorry, you lose -- try again!", width/2, height/2);
text("Press S to start a new game.", width/2, height/2+24);
wonLastGame = false;
}
}
}
}
//changing the sound that Richie makes
//Richie plays that sound
function keyPressed() {
if (getAudioContext().state !== 'running') {
getAudioContext().resume();
}
//starting game
if (keyCode === ENTER || keyCode === RETURN) {
gameMode = 1;
}
//number keys control the sound Richie makes
else if (key === '1' || key === '2' || key === '3') {
if (gameMode === 2) {
return;
}
richie.mouthOpen = true;
richie.timeOpenCurrent = millis();
if (key === '1') {
richie.sound = 1;
richie.timeMouthOpen = 400;
soundHonk.play();
soundName = "honk";
}
else if (key === '2') {
richie.sound = 2;
richie.timeMouthOpen = 1070;
soundBoing.play();
soundName = "boing";
}
else if (key === '3') {
richie.sound = 3;
richie.timeMouthOpen = 750;
soundNoisemaker.play();
soundName = "noisemaker";
}
}
//restarting game if desired
else if (key === 's' || key === 'S') {
richie.reset();
soundName = "honk";
for (let i = 0; i < honker.length; i++) {
honker[i].reset();
if (wonLastGame) {
honker[i].xSpeed += 0.2; //honkers speed up on replay if prior game was a winner
honker[i].ySpeed += 0.2; //honkers speed up on replay if prior game was a winner
}
if (!wonLastGame) {
let randomSpeed = random(0.4, 1.2);
honker[i].xSpeed = randomSpeed;
honker[i].ySpeed = randomSpeed;
score = 0;
}
}
gameMode = 1;
}
}