xxxxxxxxxx
165
let brans = [];
let neds = [];
let hay;
let numberOfBran = 3; //number of Brans to be on screen at the same time
let numberOfNed = 2; //number of Neds to be on screen at the same time
let score = 0;
let music;
let myFont;
let branImg, towerImg, hayImg, nedImg, bgImg;
let startTime; //track when the game starts
let timeLimit = 45; //countdown timer duration
let countdown;
let gameState = "start";
function preload() {
music = loadSound("GOT Main Title.mp3");
myFont = loadFont("Game of Thrones.ttf");
branImg = loadImage("bran.PNG");
towerImg = loadImage("tower.PNG");
hayImg = loadImage("hay.PNG");
nedImg = loadImage("ned.PNG");
bgImg = loadImage("bg.PNG");
}
function setup() {
createCanvas(600, 400);
for (let i = 0; i < numberOfBran; i++) {
brans[i] = new Bran();
}
for (let j = 0; j < numberOfNed; j++) {
neds[j] = new Ned();
}
hay = new Hay();
}
function draw() {
if (gameState == "start") {
drawStartScreen();
} else if (gameState == "playing") {
drawGame();
} else if (gameState == "end") {
drawEndScreen();
}
music.playMode("untilDone");
music.play();
}
function drawStartScreen() {
background(0);
textAlign(CENTER);
fill(255);
textFont(myFont);
textSize(40);
text("SAVE BRAN STARK", width / 2, height / 3);
imageMode(CENTER);
branImg.resize(0, 70);
image(branImg, 170, 175);
nedImg.resize(0, 70);
image(nedImg, 430, 175);
textSize(15);
textFont("Times new roman");
text(
"Move the hay pile with your mouse to catch Bran Stark",
width / 2,
230
);
text("Avoid catching the severed head of Ned Stark", width / 2, 260);
text("The fate of Westeros lies in your hands", width / 2, 300);
textSize(23);
textFont(myFont);
text("Press any key to start", width / 2, 350);
}
function drawGame() {
//draw game background and hay
bgImg.resize(600, 0);
image(bgImg, width / 2, height / 2, width, height);
rectMode(CORNER);
hay.display();
//start 45 second countdown
let currentTime = int((millis() - startTime) / 1000);
countdown = timeLimit - currentTime;
//generate falling Brans and Neds
tint(255, 255);
for (let i = 0; i < brans.length; i++) {
brans[i].display();
brans[i].move();
//increase score and reset Bran after collision
if (checkBranCollision(brans[i], hay)) {
score += 1;
brans[i].reset();
}
}
for (let j = 0; j < neds.length; j++) {
neds[j].display();
neds[j].move();
//decrease score and reset Ned after collision
if (checkNedCollision(neds[j], hay)) {
score -= 1;
neds[j].reset();
}
}
//end game after time is up
if (countdown < 0) {
gameState = "end";
}
//display score and countdown in real time
fill(0);
textFont("times new roman");
text("Score:" + score, 500, 60);
text("Time:" + countdown, 500, 80);
}
function restartGame() {
//reset countdown and Brans and Neds
score = 0;
startTime = millis();
timeLimit = 45;
for (let i = 0; i < brans.length; i++) {
brans[i].reset();
}
for (let j = 0; j < neds.length; j++) {
neds[j].reset();
}
countdown = timeLimit;
gameState = "start";
}
function keyPressed() {
if (gameState == "start") {
// Start the game
startTime = millis();
gameState = "playing";
} else if (gameState == "end") {
restartGame();
}
// preventing the key press from being counted again
return false;
}
function drawEndScreen() {
//display end screen with final score and restart instruction
background(0);
fill(255);
textFont("times new roman");
text("Score:" + score, width / 2, height / 2);
textFont(myFont);
text("Press any key to restart", width / 2, 350);
}
//check for collision between Bran and hay cushion
function checkBranCollision(bran, hay) {
let d = dist(bran.x, bran.y, mouseX, hay.y);
return d < bran.diameter / 2;
}
//check for collision between Ned and hay cushion
function checkNedCollision(ned, hay) {
let d2 = dist(ned.x, ned.y, mouseX, hay.y);
return d2 < ned.diameter / 2;
}