xxxxxxxxxx
301
let enem = [];
let stars = [];
let shipW = 100;
let shipL = 40;
let shipY = 210;
let shipX = 10;
let ship;
let enemyimg;
let enemX = 600;
let enemY = 10;
let enemW = 90;
let enemL = 40;
let user;
let laser;
let laserW = 30;
let laserL = 15;
let userShots = [];
let minorDmg;
let explo;
let exploX;
let exploY;
let blasts = [];
let timeEllapse;
class Laser{
constructor(image, _x, _y, W, H) {
this.img = image;
this.x=_x;
this.y=_y;
this.w = W;
this.h = H
this.speed = 5;
this.active = 1;
}
update() {
this.x += this.speed;
}
display() {
if (this.active == 1) {
image(this.img, this.x, this.y, this.w, this.h);
}
}
contact(){
let noiseValue = noise(frameCount * 0.01, 0.05);
let scaledNoise = (noiseValue - 0.5) * 30;
for (let i = 0; i < enem.length; i++) {
let max = enem[i].y + enem[i].h + scaledNoise;
let min = enem[i].y + scaledNoise;
if (this.y + this.h/2 > min && this.y + this.h/2 < max) {
let minX = enem[i].x;
let maxX = enem[i].x + enem[i].w;
if (this.x >= minX && this.x < maxX) {
if (this.active == 1){
enem[i].dmg();
this.active = 0;
timeEllapse = millis()/1000;
}
}
}
}
}
run(){
this.contact();
this.update();
this.display();
}
}
class Enemy{
constructor(image, _x, _y, W, H) {
this.img = image;
this.x=_x;
this.y=_y;
this.w = W;
this.h = H
this.speed = -0.1;
this.hp = 50;
this.dmgAnim = 0;
this.iExp = 0;
this.jExp = 0;
this.death = 0;
}
update() {
this.x += this.speed;
}
display() {
if (this.hp > 0) {
let noiseValue = noise(frameCount * 0.01, 0.05);
let scaledNoise = (noiseValue - 0.5) * 30;
image(this.img, this.x, this.y + scaledNoise, this.w, this.h);
if (this.dmgAnim == 1) {
image(minorDmg, this.x, this.y + scaledNoise, this.w/2, this.h);
if (millis()/1000 >= timeEllapse + 0.3) {
this.dmgAnim = 0;
}
}
}
//destruction animation
else if (this.hp <= 0 && this.death == 0) {
image(blasts[this.iExp][this.jExp], this.x + this.w/8, this.y - 5);
this.jExp++;
if (this.jExp > 7) {
this.jExp = 0;
this.iExp++;
if (this.iExp > 2) {
this.death = 1;
}
}
}
}
checkEdges() {
if (this.x < 5/7 * width) {
this.x = 5/7 * width;
}
}
dmg(){
this.dmgAnim = 1;
this.hp -= 25;
}
run(){
this.update();
this.display();
this.checkEdges();
}
}
class User{
constructor(image, _x, _y, W, H) {
this.img = image;
this.x=_x;
this.y=_y;
this.w = W;
this.h = H
this.speed = 4;
this.hp = 100;
}
update() {
if (isKeyPressed) {
if (keyCode == LEFT_ARROW) {
this.x -= this.speed;
}
if (keyCode == RIGHT_ARROW) {
this.x += this.speed;
}
if (keyCode == UP_ARROW) {
this.y -= this.speed;
}
if (keyCode == DOWN_ARROW) {
this.y += this.speed;
}
}
}
display() {
let noiseValue = noise(frameCount * 0.01, 0.05);
let scaledNoise = (noiseValue - 0.5) * 30;
image(this.img, this.x, this.y + scaledNoise, this.w, this.h);
fill (255, 0, 0);
stroke(255);
let healthBar = (this.w - 20)*(this.hp/100);
let dmg = (this.w - 20) - healthBar;
rect(this.x +10, this.y - 5 + scaledNoise, healthBar, 5);
fill (0);
rect(this.x + healthBar + 10, this.y - 5 + scaledNoise, dmg, 5);
if (frameCount % 60 == 0) {
userShots.push(new Laser(laser, this.x + shipW - 5, this.y + shipL/2 + scaledNoise, laserW, laserL));
}
}
checkEdges() {
if (this.x < 0) {
this.x = 0;
}
if (this.y < 15) {
this.y = 15;
}
if (this.x > 0.5 * width) {
this.x = 0.5 * width;
}
if (this.y > height - this.h - 10) {
this.y = height - this.h - 10;
}
}
run(){
this.update();
this.display();
this.checkEdges();
}
}
class Stars{
constructor(_x, _y, _s) {
this.x=_x;
this.y=_y;
this.size = _s;
this.xSpeed = -0.5;
}
update() {
this.x += this.xSpeed;
}
display() {
circle(this.x, this.y, this.size);
}
checkEdges() {
if (this.x < -this.size/2) {
this.x = width+this.size/2;
}
}
run(){
this.update();
this.display();
this.checkEdges();
}
}
function preload() {
ship = loadImage("spaceship.jpg");
enemyimg = loadImage("enemy.jpg");
laser = loadImage("laser.jpg");
minorDmg = loadImage("dmg.jpg");
explo = loadImage("explosion2.jpg");
}
function setup() {
createCanvas(700, 500);
fill (255);
for (let i = 0; i < 300; i++) {
stars.push(new Stars(random(width), random(height), random(0, 5)));
}
user = new User(ship, shipX, shipY, shipW, shipL);
for (let i = 0; i < 10; i++) {
enem.push(new Enemy(enemyimg, enemX, enemY + (i*48), enemW, enemL));
}
//10 length
//24 width
//3 rows, 24 collumns
// get the width and hieght of each sprite
let w = explo.width/15;
let h = explo.height/5;
// there are four rows, create a for loop to iterate through them
for (let y = 0; y < 3; y++) {
// create another emoty array for that row
blasts[y] = [];
// there are 12 images in a row, iterate through them
for (let x = 0; x < 8; x++) {
// get the image subsection there and then stor in the array
let xCor = 2*x*w;
let yCor = 2*y*h;
blasts[y][x] = explo.get(xCor, yCor, w, h);
}
}
exploX = width/2;
exploY = height/2;
}
function draw() {
background (0, 26, 77);
for (let i = 0; i < stars.length; i++) {
stars[i].run();
}
for (let i = 0; i < enem.length; i++) {
enem[i].run();
}
user.run();
for (let i = 0; i < userShots.length; i++) {
userShots[i].run();
}
fill(255);
}