xxxxxxxxxx
281
let guy;
let enemy = [];
let knightright;
let knightrightattack;
let knightleft;
let knightleftattack;
let knightdeath;
let a;
//make an exists variable for guy!
//fix enemyRight
//just so's ya know, the dimensions of the knight are 80 x 140
function preload() {
knightright = loadImage("knightright.png");
knightrightattack = loadImage("knightrightattack.png");
knightleft = loadImage("knightleft.png");
knightleftattack = loadImage("knightleftattack.png");
knightdeath = loadImage("knightdeath.gif");
}
function setup() {
createCanvas(1000, 1000);
guy = new Guy(800, 810);
enemy[0] = new Enemy(20, 850);
a = 255;
}
function draw() {
//background
background(50, 100, 255);
//"lives" display
fill(0);
textSize(30);
text(guy.lives, 50, 100);
//health bar
if (guy.lives > 0) {
fill(255, 0, 0, a);
noStroke();
rect(100, 75, guy.health * 2, 30);
} else {
fill(0);
rect(100, 75, 200, 30);
}
//animation
for (let i = 0; i < enemy.length; i++) {
if (dist(guy.x, guy.y, enemy[i].x, enemy[i].y) <= 75) {
if (a < 100) {
a = 255;
} else {
a -= 5 - guy.lives;
}
}
}
noFill();
stroke(0);
strokeWeight(2);
rect(100, 75, 200, 30);
stroke(50, 100, 255);
fill(50, 100, 255);
triangle(300, 75, 300, 105, 262, 105);
stroke(0);
strokeWeight(1.5);
line(300, 75, 260, 105);
//ground
noStroke();
fill(125, 75, 50);
rect(0, 950, 1000, 50);
//guy is showing and updating
if (guy.lives > 0) {
guy.update();
}
guy.show();
//same for the enemy
for (let i = 0; i < enemy.length; i++) {
enemy[i].show();
enemy[i].fight();
}
}
class Guy {
constructor(x, y) {
//blah blah blah
this.x = x;
this.y = y;
this.health = 100;
this.lives = 5;
this.gravity = 0.7;
this.up = -15;
this.velocity = 0;
this.csl = true;
this.dir = "right";
this.attack = false;
}
show() {
//shows the image
if (this.lives > 0) {
if (this.dir == "right") {
if (this.attack) {
image(knightrightattack, this.x, this.y);
} else {
image(knightright, this.x, this.y);
}
}
if (this.dir == "left") {
if (this.attack) {
image(knightleftattack, this.x, this.y);
} else {
image(knightleft, this.x, this.y);
}
}
} else {
image(knightdeath, this.x, this.y);
}
}
update() {
//implements gravity/jumping
this.velocity += this.gravity;
this.y += this.velocity;
//makes sure the guy doesnt sink into the ground
if (this.y > 810) {
this.y = 810;
this.velocity = 0;
}
//makes you lose a life at 0 health
if (this.health <= 1){// && this.lives > 0) {
this.health = 100;
this.lives--;
a = 255;
}
//console.logs once if you die
if (this.lives <= 0 && this.csl) {
console.log("you dead, son!");
this.csl = false;
}
//ensures that you cant go offscreen
if (this.x < 0) {
this.x = 0;
}
if (this.x > 920) {
this.x = 920;
}
//movement
if (keyIsDown(39)) {
this.x += 5;
this.dir = "right";
}
if (keyIsDown(37)) {
this.x -= 5;
this.dir = "left";
}
//if (this.lives < 6) {
//this.health += 1;
//}
//if(this.health > 100) {
//this.lives += 1;
// this.health = 1;
//}
}
fight() {
//makes it so you can fight
for (let i = 0; i < enemy.length; i++) {
if (dist(enemy[i].x, enemy[i].y, this.x, this.y) <= 150) {
enemy[i].health -= 10;
for (let j = 0; j < 10; j++) {
if (!enemyRight(guy.x, enemy[i].x)) {
enemy[i].x -= 2;
}
if (enemyRight(guy.x, enemy[i].x)) {
enemy[i].x += 2;
}
}
}
}
}
jump() {
this.velocity += this.up;
}
}
class Enemy {
constructor(x, y) {
this.x = x;
this.y = y;
this.health = 100;
// this.exists = true;
}
show() {
if (this.health > 0) {
fill(255, 0, 0);
rect(this.x, this.y, 50, 100);
} else {
this.x = 400;
this.y = 850;
// this.exists = true;
this.health = 100;
}
}
fight() {
//hits you
if (dist(guy.x, guy.y, this.x, this.y) <= 75) {
guy.health -= 0.5;
}
//moves the enemy towards you
if (dist(guy.x, guy.y, this.x, this.y) >= 60) {
if (!enemyRight(guy.x, this.x)) {
this.x += 2;
}
if (enemyRight(guy.x + 160, this.x)) {
this.x -= 2;
}
}
}
}
function keyPressed() {
if (keyCode === 65) {
for (let i = 0; i < enemy.length; i++) {
if (
(enemyRight(guy.x, enemy[i].x) && guy.dir == "right") ||
(!enemyRight(guy.x, enemy[i].x) && guy.dir == "left")
) {
guy.fight();
}
}
}
if (keyCode === 32 && guy.y >= 810) {
guy.jump();
}
}
function enemyRight(guyx, enemyx) {
if (guyx > enemyx) {
return false;
}
if (guyx < enemyx) {
return true;
}
}
for (let i = 0; i < enemy.length; i++) {
if (
(enemyRight(guy.x, enemy[i].x) && guy.dir == "right") ||
(!enemyRight(guy.x, enemy[i].x) && guy.dir == "left")
) {
}
}