xxxxxxxxxx
226
var hero;
var bgImg;
var bgX1 = 0;
var bgX2;
var barrierX = bgX1;
var scrollSpeed = 0;
var score = 0;
var fireball;
var heroHealth = 100;
function placeholderText(dummyText) {
//ANY TEXT DISPLAY - Function to draw the a placeholder text for testing
push();
textSize(windowWidth / 10);
fill(255);
stroke(0);
textAlign(CENTER, CENTER)
text(str(dummyText), windowWidth / 2, windowHeight / 2);
pop();
}
function playerScore(counter) {
//Function to display player score
var fontScale = (windowWidth / windowHeight * 50);
score += counter * 0.1;
fill(255);
textSize(fontScale);
textStyle(BOLD);
textFont(scoreFont);
text(int(score), windowWidth/1.2, windowHeight/7);
return score;
}
function preload() {
//BACKGROUND IMAGE
bgImg = loadImage("wall.jpg");
scoreFont = loadFont("scorefont.ttf");
//HP BAR
healthbar = new Sprite(windowWidth/1.5, 100, 255, 81, "s");
healthbar.spriteSheet = "healthbar.png";
healthbar.anis.offset.x = 0;
healthbar.anis.frameDelay = 8;
healthbar.scale = 1;
healthbar.addAnis({
full: { row: 0, frames: 1 },
seventy: { row: 1, frames: 1 },
fifty: { row: 2, frames: 1 },
thirty: { row: 3, frames: 1 },
ten: { row: 4, frames: 1 },
empty: { row: 5, frames: 1 },
});
healthbar.changeAni("full");
//FIREBALL
fireball = new Sprite(100, 100, 40, 32);
fireball.spriteSheet = "fireball_spritesheet3.png";
fireball.anis.offset.x = 0;
fireball.anis.frameDelay = 12;
fireball.rotationLock = true;
fireball.debug = true;
fireball.addAnis({
default: { row: 0, frames: 3 },
});
fireball.changeAni("default");
//INVISIBLE BLOCK
barrier = new Sprite(barrierX, windowHeight/1.8, 600, 100);
barrier.collider = "static";
barrier.opacity = "1";
barrier.debug = true;
barrier2 = new Sprite(barrierX, windowHeight/0.95, 600, 100);
barrier2.collider = "static";
barrier2.opacity = "1";
barrier2.debug = true;
//CHARACTER SPRITE
hero = new Sprite(0, windowHeight/1.3, 78, 80, "d");
hero.spriteSheet = "knightfull_spritesheetSM.png";
hero.anis.offset.x = 0;
hero.anis.frameDelay = 8;
hero.debug = true;
hero.rotationLock = true;
hero.scale = 1;
hero.addAnis({
down: { row: 0, frames: 4 },
run: { row: 1, frames: 7 },
up: { row: 2, frames: 4 },
});
hero.changeAni("run");
hero.mirror.x = true;
}
function setup() {
new Canvas(windowWidth, windowHeight);
allSprites.pixelPerfect = true;
bgX2 = width;
}
function draw() {
clear();
//CASTLE BACKGROUND
image(bgImg, bgX1, 0, width, height);
image(bgImg, bgX2, 0, width, height);
bgX1 -= scrollSpeed;
bgX2 -= scrollSpeed;
if (bgX1 < -width) {
bgX1 = width - 10;
}
if (bgX2 < -width) {
bgX2 = width - 10;
}
//PLAYER MOVEMENTS
var normalSpeed = 2.3;
var diagonalSpeed = normalSpeed * 1.4;
if (hero.y > 240 || hero.y < 380) {
if (kb.pressing("d")) hero.vel.x = normalSpeed + 0.5;
else if (kb.pressing("w")) hero.vel.y = -diagonalSpeed;
else if (kb.pressing("s")) hero.vel.y = diagonalSpeed;
else if (kb.pressing("space")) hero.vel.x = 0;
else (hero.vel.x = normalSpeed) & (hero.vel.y = 0);
}
if (kb.pressing("d")) {
hero.changeAni("run");
hero.mirror.x = true;
} else if (kb.pressing("w")) {
hero.changeAni("up");
} else if (kb.pressing("s")) {
hero.changeAni("down");
} else {
hero.changeAni("run");
hero.mirror.x = true;
}
//FIREBALL MOVEMENTS
fireball.x = mouse.x;
fireball.y = mouse.y;
//COLLISION CHECK
if (hero.overlaps(fireball)) {
if (heroHealth <= 0) {
print("GAME OVER!");
gameState = false;
print(gameState);
} else {
heroHealth = heroHealth - 10;
print("Your health is now " + heroHealth);
}
}
//print("Player location is " + str(hero.y));
//CAMERA
camera.x = hero.x + (width/3);
scrollSpeed = hero.vel.x * 2;
barrier.x = hero.x;
barrier2.x = hero.x;
healthbar.x = hero.x + (windowWidth/10);
//HEALTH CHECK
if (heroHealth >= 100){
healthbar.changeAni("full");
//print(gameState);
} else if (heroHealth >= 70) {
healthbar.changeAni("seventy");
//print(gameState);
} else if (heroHealth >= 50) {
healthbar.changeAni("fifty");
//print(gameState);
} else if (heroHealth >= 30) {
healthbar.changeAni("thirty");
//print(gameState);
} else if (heroHealth >= 10) {
healthbar.changeAni("ten");
//print(gameState);
} else {
healthbar.changeAni("empty");
clear();
hero.vel.x = 0
for (let i = allSprites.length; i--;){
allSprites[i].remove();
}
background(0);
placeholderText('GAME OVER');
hero.vel.x = 0;
playerScore(0);
}
//SCORE
playerScore(hero.vel.x);
if (score >= 1000) {
clear();
hero.vel.x = 0
for (let i = allSprites.length; i--;){
allSprites[i].remove();
}
background(0);
placeholderText('YOU WIN!');
hero.vel.x = 0;
playerScore(0);
} else{
}
//BOUNDING BOX
noFill();
strokeWeight(2);
stroke('red');
rectMode(CENTER, CENTER);
rect((windowWidth/2), (windowHeight/2), windowWidth/1.1, windowHeight/1.1);
print("Window Width: " + windowWidth + " Window Height:" +windowHeight);
}