xxxxxxxxxx
572
let SCREEN_WIDTH = 600;
let SCREEN_HEIGHT = 600;
//class for all objects that will be created in the game
class Character {
constructor(r, x, y) {
this.radius = r;
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.health = 0;
}
update() {
this.y += this.vy;
this.x += this.vx;
}
//method to handle display of the object
display() {
this.update();
fill(255, 0, 0);
noStroke();
ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
}
hit(amount) {
this.health = this.health - amount;
}
gen_health(num) {
let prob = round(random(0, num));
if (prob == 1) {
main_game.powerups.push(new OzonePowerUp(this.x, this.y));
}
}
}
// class for main player
class Tree extends Character {
constructor(r, x, y) {
super(r, x, y);
this.img = tree;
// This is used to adjust the movement speed
this.move_speed = 10;
this.key_handler = { a: false, d: false, w: false, s: false };
//Used to control how fast our player shoots
this.projFrequency = 45;
this.health = 100;
this.lives = 3;
this.projSpeed = 10;
this.img2 = apple;
this.shoot_sound = shoot;
}
update() {
//Adding movement
this.y += this.vy;
this.x += this.vx;
//Input control for movement
if (this.key_handler.a) {
this.vx = -this.move_speed;
} else if (this.key_handler.d) {
this.vx = this.move_speed;
} else {
this.vx = 0;
}
if (this.key_handler.w) {
this.vy = -this.move_speed;
} else if (this.key_handler.s) {
this.vy = this.move_speed;
} else {
this.vy = 0;
}
//Restricting movement to screen bounds
if (this.x - this.radius < 0) {
this.x = this.radius;
} else if (this.x + this.radius > SCREEN_WIDTH) {
this.x = SCREEN_WIDTH - this.radius;
} else if (this.y - this.radius < 0) {
this.y = this.radius;
} else if (this.y + this.radius > SCREEN_HEIGHT) {
this.y = SCREEN_HEIGHT - this.radius;
}
//Instantiate projectiles in front of the player ship
// if (frameCount % this.projFrequency == 0) {
// this.shoot();
// this.shoot_sound.play();
// }
}
display() {
this.update();
image(this.img, this.x - 50, this.y - 50, 100, 100);
fill(255, 0, 0);
noStroke();
ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
}
//method that instantiates the projectiles
shoot() {
main_game.playerProjectiles.push(
new Projectile(
5,
main_game.player.x - 2,
main_game.player.y - 10,
12,
this.projSpeed,
this.img2
)
);
}
hit(amount) {
this.health = this.health - amount;
if (this.health <= 0 && this.lives > 0) {
this.lives -= 1;
this.health = 100;
} else if (this.health <= 0 && this.lives == 0) {
main_game.gameOver = true;
}
}
collision(target) {
var distance = ((this.x - target.x) ** 2 + (this.y - target.y) ** 2) ** 0.5;
//Check collision with target
if (distance <= this.radius + target.radius) {
// If a collision is detected, then the projectile is used, it won't be displayed anymore
return true;
} else {
return false;
}
}
}
//class for enemy objects
class Enemy extends Character {
constructor(r, x, y) {
super(r, x, y);
this.health = 10;
this.vy = 0.5;
this.projSpeed = 5;
//this.imgArray = ['plane.png','chainsaw.png']
this.img = chainsaw;
this.img2 = dynamite;
this.img3 = random(imgArray);
this.used = false;
}
update() {
//Adding movement
this.y += this.vy;
if (this.y > 300) {
this.vy = 1;
}
// if (frameCount % 60 == 0) {
// let prob = round(random(0, 3));
// if (prob == 1) {
// this.shoot();
// }
// }
}
shoot() {
main_game.enemyProjectiles.push(
new Projectile(5, this.x, this.y + 25, 6, this.projSpeed, this.img2)
);
}
collision(target) {
var distance = ((this.x - target.x) ** 2 + (this.y - target.y) ** 2) ** 0.5;
//Check collision with target
if (distance <= this.radius + target.radius) {
// If a collision is detected, then the projectile is used, it won't be displayed anymore
this.used = true;
return true;
} else {
return false;
}
}
display() {
this.update();
image(this.img3, this.x - 10, this.y - 30, 100, 50);
fill(255, 0, 0);
noStroke();
ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
if (this.health <= 0) {
main_game.score += 10;
this.gen_health(3);
}
}
}
//PROJECTILE class
class Projectile {
constructor(r, x, y, dir, s, img) {
this.radius = r;
this.x = x;
this.y = y;
//This direction is set based on the clock. So 12 means up, 6 means down, 4 means south east etc...
this.dir = dir;
this.speed = s;
this.vx = 0;
this.vy = 0;
//This variable is used to detect when a projectile has already collided once
this.used = false;
this.img = img;
}
update() {
//The following if conditions sets the right velocity based on the direction the shots should move in
if (this.dir == 12) {
this.vx = 0;
this.vy = -this.speed;
} else if (this.dir == 6) {
this.vx = 0;
this.vy = this.speed;
} else if (this.dir == 4) {
this.vx = this.speed;
this.vy = this.speed;
} else if (this.dir == 7) {
this.vx = -this.speed;
this.vy = this.speed;
} else if (this.dir == 1) {
this.vx = this.speed;
this.vy = -this.speed;
} else if (this.dir == 11) {
this.vx = -this.speed;
this.vy = -this.speed;
}
this.x += this.vx;
this.y += this.vy;
}
display() {
// Only if the projectile hasn't collided already, the following code will execute
if (this.used == false) {
this.update();
image(this.img, this.x - 12, this.y - 10, 25, 25);
// fill(0, 255, 126);
// noStroke();
// ellipse(this.x, this.y, this.radius * 2, this.radius * 2);
}
}
collision(target) {
var distance = ((this.x - target.x) ** 2 + (this.y - target.y) ** 2) ** 0.5;
//Check collision with target
if (distance <= this.radius + target.radius) {
// If a collision is detected, then the projectile is used, it won't be displayed anymore
this.used = true;
return true;
} else {
return false;
}
}
}
class OzonePowerUp {
constructor(x, y) {
this.x = x;
this.y = y;
this.radius = 30;
this.healAmount = 30;
this.used = false;
this.img = ozone;
}
give_health() {
main_game.player.health += this.healAmount;
if (main_game.player.health > 100) {
main_game.player.health = 100;
}
}
collision(target) {
var distance = ((this.x - target.x) ** 2 + (this.y - target.y) ** 2) ** 0.5;
//Check collision with target
if (distance <= this.radius + target.radius) {
// If a collision is detected, then the projectile is used, it won't be displayed anymore
this.used = true;
return true;
} else {
return false;
}
}
display() {
image(this.img, this.x - 25, this.y - 30, 50, 50);
// fill(0, 255, 0);
// noStroke();
// ellipse(this.x, this.y, this.radius, this.radius);
}
}
//instantiates all other classes and keeps them together
class Game {
constructor() {
this.player = new Tree(40, 550, 500);
this.enemyProjectiles = [];
this.playerProjectiles = [];
this.powerups = [];
this.enemies = [];
this.playerProjectileLimit = 10;
this.timer = 0;
this.score = 0;
this.gameStarted = false;
this.gameOver = false;
this.gameWon = false;
this.bg_img = sky;
bgsound.play();
this.wave1spawned = false;
this.wave2spawned = false;
this.wave3spawned = false;
}
display_game() {
if (this.gameStarted && this.gameOver == false && this.gameWon == false) {
image(this.bg_img, 0, 0, 600, 600);
image(earth, 0, 450, 600, 300)
this.player.display();
for (let i = 0; i < this.enemies.length; i++) {
this.enemies[i].display();
if (
this.enemies[i].used == false &&
this.enemies[i].collision(this.player)
) {
this.player.hit(10);
}
}
for (let j = 0; j < this.enemies.length; j++) {
//Check if the projectile collides with any enemy
if (this.player.collision(this.enemies[j])
) {
this.enemies[j].hit(10);
}
}
for (let i = 0; i < this.enemyProjectiles.length; i++) {
this.enemyProjectiles[i].display();
if (
this.enemyProjectiles[i].used == false &&
this.enemyProjectiles[i].collision(this.player)
) {
this.player.hit(10);
}
}
for (let i = 0; i < this.enemyProjectiles.length; i++) {
if (this.enemyProjectiles[i].used == true) {
this.enemyProjectiles.splice(i, 1);
break;
}
}
for (let i = 0; i < this.enemies.length; i++) {
if (this.enemies[i].health <= 0) {
this.enemies.splice(i, 1);
break;
} else if (this.enemies[i].y > 650) {
this.enemies.splice(i, 1);
this.score -= 10;
break;
}
}
for (let i = 0; i < this.playerProjectiles.length; i++) {
this.playerProjectiles[i].display();
for (let j = 0; j < this.enemies.length; j++) {
//Check if the projectile collides with any enemy
if (
this.playerProjectiles[i].used == false &&
this.playerProjectiles[i].collision(this.enemies[j])
) {
this.enemies[j].hit(10);
}
}
}
for (let i = 0; i < this.powerups.length; i++) {
this.powerups[i].display();
if (
this.powerups[i].used == false &&
this.powerups[i].collision(this.player)
) {
this.powerups[i].give_health();
}
}
for (let i = 0; i < this.powerups.length; i++) {
if (this.powerups[i].used == true) {
this.powerups.splice(i, 1);
break;
}
}
this.show_score();
if (frameCount % 60 == 0) {
this.timer += 1;
//print(this.timer);
}
if (this.timer == 1 && this.wave1spawned == false) {
for (let i = 0; i < 5; i++) {
this.enemies[i] = new Enemy(25, 200 + 70 * i, 100);
}
this.enemies.push(new Enemy(25, 100 + 70 * 0, 10));
this.enemies.push(new Enemy(25, 100 + 70 * 1, 20));
this.enemies.push(new Enemy(25, 100 + 70 * 2, 30));
this.enemies.push(new Enemy(25, 100 + 70 * 3, 40));
this.wave1spawned = true;
} else if (this.timer == 11 && this.wave2spawned == false) {
this.enemies.push(new Enemy(25, 100 + 70 * 0, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 1, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 2, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 3, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 4, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 5, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 6, 0));
this.wave2spawned = true;
} else if (this.timer == 20 && this.wave3spawned == false) {
this.enemies.push(new Enemy(25, 100 + 70 * 0, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 1, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 2, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 3, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 4, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 5, 0));
this.enemies.push(new Enemy(25, 100 + 70 * 6, 0));
this.wave3spawned = true;
}
else if (this.timer == 30 && this.player.lives >= 1) {
main_game.gameWon = true;
}
}
else if (this.gameOver){
image(this.bg_img, 0, 0, 600, 600);
textSize(35);
text("Game Over", 230, 300);
textSize(25);
text("Score: " + str(this.score), 260, 330);
text("Press RETURN to begin " , 200, 360);
bgsound.stop();
}
else if (this.gameWon){
image(this.bg_img, 0, 0, 600, 600);
textSize(25);
text("You Have Saved The Earth!", 200, 300);
textSize(20);
text("Score: " + str(this.score), 300, 330);
text("Press RETURN to begin " , 240, 360);
bgsound.stop();
}
else if (
this.gameStarted == false &&
this.gameOver == false &&
this.gameWon == false
) {
image(this.bg_img, 0, 0, 600, 600);
textSize(45);
fill(6,57,112);
textFont(font);
text("CHAIN SLAYER ", 150, 300);
textSize(20);
//textFont('Georgia');
text("Use WASD keys to move " , 200, 350);
text("Press RETURN to begin " , 205, 380);
}
}
show_score() {
fill(255);
textSize(18);
textFont('Helvetica');
text("Score: " + str(this.score), 490, 40);
text("Ozone: " + str(this.player.health), 490, 60);
text("Lives: " + str(this.player.lives), 490, 80);
}
}
function preload() {
bgsound = loadSound("bgsound.mp3");
shoot = loadSound("apple.mp3");
font = loadFont('font3.ttf');
earth = loadImage("earth3.png");
tree = loadImage("tree.png");
apple = loadImage("apple.png");
chainsaw = loadImage("chain.webp");
dynamite = loadImage("dynamite.png");
ozone = loadImage("ozone.png");
pplane = loadImage("plane.png");
sky = loadImage("sky.jpeg");
//car = loadImage("car1.png");
imgArray = [chainsaw]
}
function setup() {
createCanvas(SCREEN_WIDTH, SCREEN_HEIGHT);
//Instantiating the game class
main_game = new Game();
}
function draw() {
background(0);
main_game.display_game();
}
//controls movements of main character based on keypressed
function keyPressed() {
if (key == "a") {
main_game.player.key_handler.a = true;
} else if (key == "d") {
main_game.player.key_handler.d = true;
} else if (key == "s") {
main_game.player.key_handler.s = true;
} else if (key == "w") {
main_game.player.key_handler.w = true;
}
if (main_game.gameStarted == false && keyCode == RETURN){
main_game.gameStarted = true
}
if (main_game.gameOver == true && keyCode == RETURN || main_game.gameWon == true && keyCode == RETURN){
main_game = new Game()
}
}
function keyReleased() {
if (key == "a") {
main_game.player.key_handler.a = false;
} else if (key == "d") {
main_game.player.key_handler.d = false;
} else if (key == "s") {
main_game.player.key_handler.s = false;
} else if (key == "w") {
main_game.player.key_handler.w = false;
}
}