xxxxxxxxxx
219
/**
** -------------------- ZOMBIES!!!!! --------------------------
**
** 1. Help the player move to escape the zombies!
**
** 2. Kill the player if a zombie catches them :(
**
** 3. Let the player escape if they reach the exit
**
** Extra:
**
** 4. Reset the level when they reach the exit
**
** 5. Increase the difficulty with each new level!
**
** ------------------------------------------------------------
*/
// Setup a player object with these proprties:
var player = {
hitPoints: 10,
size: 35,
speed: 2,
x: 0,
y: 0,
};
// Setup an escape hatch to exit the level:
var exit = {
size: 50,
x: 0,
y: 0,
};
// Determine the level difficulty based on zombie properties:
var numberOfZombies = 3;
var zombieMinSpeed = 0.50;
var zombieMaxSpeed = 1.00;
var backgroundColor = 255;
// Create an array to hold a list of zombies
var zombies = [];
/**
** ------- SETUP & DRAW ---------------------------------------
*/
function setup() {
createCanvas(600, 600);
rectMode(CENTER);
player.x = width / 2;
player.y = height / 2;
resetLevel();
}
function draw() {
background(backgroundColor);
strokeWeight(0);
moveZombies();
movePlayer();
drawExit();
drawZombies();
drawPlayer();
checkZombieTouchesPlayer();
checkPlayerCanEscape();
}
/**
** ------- PLAYER FUNCTIONS ---------------------------------------
*/
function drawPlayer() {
if (player.hitPoints > 0) {
// Draw the player, if they're alive
strokeWeight(0);
fill(224, 36, 124);
rect(player.x, player.y, player.size, player.size);
} else {
// Draw the game over screen if the player is dead!
background(0);
fill(255);
textSize(72);
textAlign(CENTER);
text("You died!!", width / 2, (height / 2), width, 100);
}
}
function movePlayer() {
// This function doesn't do anything yet!!
// Add some code to help the player move
// Hint: look at the keyIsDown() docs
}
function checkZombieTouchesPlayer() {
// This function doesn't do anything yet!!
// Add some code to detect if a zombie touches the player
// Hint 1: you can use a loop to check each zombie position
// Hint 2: check the reference for the dist() function
// Hint 3: set the player.hitPoints to 0 to kill the player
}
function checkPlayerCanEscape() {
// This function doesn't do anything yet!!
// Add some code to detect if the player reaches the exit
// Then, reset the level (and increase the difficulty?)
}
/**
** ------- ZOMBIE FUNCTIONS ---------------------------------------
*/
function drawZombies() {
for (var i = 0; i < zombies.length; i++) {
// Get the current zombie in the array
var zombie = zombies[i];
// Draw the zombie
strokeWeight(0);
fill(zombie.color);
ellipse(zombie.x, zombie.y, zombie.size, zombie.size);
}
}
function moveZombies() {
for (var i = 0; i < zombies.length; i++) {
// Get the current zombie in the array
var zombie = zombies[i];
// Check the distance between zombie and player
var distX = player.x - zombie.x;
var distY = player.y - zombie.y;
var distanceBetween = sqrt((distX * distX) + (distY * distY));
// Move the zombie towards the player
if (distanceBetween > zombie.speed) {
var ratio = zombie.speed / distanceBetween;
zombie.x += random(ratio * distX);
zombie.y += random(ratio * distY);
}
}
}
function createZombie() {
var randomX, randomY;
// Generate a random position that is not too close to the player
do {
randomX = random(width);
randomY = random(height);
} while (dist(randomX, randomY, player.x, player.y) < 50);
// Create a zombie with randomized color, speed and position
var zombie = {
speed: lerp(zombieMinSpeed, zombieMaxSpeed, random(10) / 10),
color: color(80 + random(-30, 30), 100 + random(-30, 30), 40 + random(-30, 30)),
size: 35,
x: randomX,
y: randomY,
};
return zombie;
}
/**
** ------- LEVEL FUNCTIONS ---------------------------------------
*/
function drawExit() {
// Draw box
noFill();
stroke(backgroundColor * 0.8);
strokeWeight(4);
rect(exit.x, exit.y, exit.size, exit.size);
// Draw text
fill(backgroundColor * 0.8);
strokeWeight(0);
textSize(14);
textAlign(CENTER);
text("EXIT", exit.x, exit.y + 5);
}
function resetLevel() {
// Reset the player
player.hitPoints = 10;
// Level gets darker
backgroundColor -= 10;
// Move the exit, but not too close to the player
do {
exit.x = random(exit.size, width - exit.size);
exit.y = random(exit.size, height - exit.size);
} while (dist(exit.x, exit.y, player.x, player.y) < 50);
// Clear all zombies
zombies = [];
// Create new zombies
for (var i = 0; i < numberOfZombies; i++) {
zombies[i] = createZombie();
}
}