xxxxxxxxxx
791
function preload() {
// load all assets and character images
background_img = loadImage("/assets/background.png");
start_img = loadImage("/assets/start1.png");
rules_img = loadImage("/assets/rules.png");
bullet_img = loadImage("/assets/bullet.png");
explode_img = loadImage("/assets/exploding.png");
character_img = loadImage("/characters/ship.png");
enemy_img = loadImage("characters/enemy.png");
}
//variables for controls
let move_up = 0;
let move_down = 0;
let shoot = 0;
let still = 0;
let mode = 0;
let white_up = 0;
let white_down = 0;
let red = 0;
function setup() {
createCanvas(1600, 1000);
textSize(18);
game = new Game();
}
function draw() {
background(205);
fill(0);
// if (!serialActive) {
// text("Press Space Bar to select Serial Port", 20, 30);
// } else {
// text("Connected", 20, 30);
// game.display();
// print(game.enemies.length);
// // }
game.display();
// print(game.player.bullets.length);
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
if (keyCode == RIGHT_ARROW) {
game.player.shoot();
}
if (key == "s") {
game.start_status = (game.start_status + 1) % 2;
}
if (key == "p" && game.start_status == 0) {
game.start = false;
game.game_playing = true;
}
if (key == "r") {
print(game.player.score + "- score..." + game.player.health + ":health")
}
}
class Game {
constructor() {
this.start = true;
this.start_img = start_img;
this.start_status = 0;
this.rules = false;
this.rules_img = rules_img;
this.game_playing = false;
this.game_over = false;
this.wave = 0;
this.player = new Player(character_img, 200, 200);
this.background_img = background_img;
this.enemies = [];
this.spawn_delay = 120; //initial spawn delay for first wave (1.5 seconds)
this.wave_time = 300; //length of wave of enemies coming (45 seconds)
this.button_pressed = false;
this.red_button_pressed = false;
}
handle_menu_buttons() {
if (this.start) {
if (white_up == 1 || white_down == 1) {
if (!this.button_pressed) {
// button was not pressed, change the state
this.start_status = (this.start_status + 1) % 2;
this.button_pressed = true; // Set the button state to pressed
}
} else {
// button is not pressed, set the state to not pressed
this.button_pressed = false;
}
}
if (red == 1 && this.start_status == 0) {
if (!this.red_button_pressed) {
this.start = false;
this.game_playing = true;
this.red_button_pressed = true;
}
} else {
this.red_button_pressed = false;
}
}
check_mode() {
if (this.game_playing) {
if (mode == 1) {
move_up = white_up;
move_down = white_down;
shoot = red;
} else {
move_up = move_up;
move_down = move_down;
shoot = shoot;
}
}
}
spawn_enemies() {
if (this.wave != 0) {
if (frameCount % this.spawn_delay == 0) {
this.enemies.push(new Enemy(this.wave));
}
}
}
display() {
if (this.start) {
this.handle_menu_buttons();
if (this.start_status == 0) {
image(this.start_img, 0, 0);
} else if (this.start_status == 1) {
image(this.rules_img, 0, 0);
}
} else if (this.game_playing) {
//background
this.check_mode();
image(this.background_img, 0, 0);
//player
this.player.display();
// print(this.player.health + ": health, " + this.player.score);
// print(this.player.bullets.length + ": bullets, " + this.enemies.length);
// print(game.player.bullets.length);
// print(game.enemies.length);
//waves
if (frameCount % this.wave_time == 0) {
this.wave++;
}
//enemies
for (let enemy = this.enemies.length - 1; enemy >= 0; enemy--) {
this.enemies[enemy].display();
}
if (this.wave != 0) {
this.spawn_enemies();
}
}
}
}
class Player {
constructor(img, img_w, img_h) {
this.img = img;
this.w = img_w;
this.h = img_h;
this.x = 500 - this.w / 2;
this.y = height / 2 + 100 - this.h / 2;
this.vy = 0;
this.bullets = [];
this.bullet_img = bullet_img;
this.health = 3;
this.score = 0;
}
move() {
// controls
if (move_up == 1 && this.y > 0 + this.h / 2) {
this.vy = -10;
} else if (move_down == 1 && this.y < height - this.h / 2) {
this.vy = 10;
} else if (still == 1) {
this.vy = 0;
}
if (keyCode == UP_ARROW && this.y > 0 + this.h / 2) {
this.vy = -10;
} else if (keyCode == DOWN_ARROW && this.y < height - this.h / 2) {
this.vy = 10;
} else {
this.vy = 0;
}
// shooting bullets
if (shoot == 1) {
this.shoot();
}
//update movement
this.y += this.vy;
// print(this.bullets.length);
}
/*
check_collision() {
// Check for bullet-enemy collision/
for (let b = this.bullets.length - 1; b >= 0; b--) {
if (this.bullets[b].exists) {
// Check if bullet exists
for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
if (game.enemies[enemy].alive) {
// Check if enemy exists
let enemy_x = game.enemies[enemy].x;
let enemy_y = game.enemies[enemy].y;
let bullet_x = this.bullets[b].x;
let bullet_y = this.bullets[b].y;
// Check if the enemy is alive before doing any further checks
if (
game.enemies[enemy].alive &&
dist(bullet_x, bullet_y, enemy_x, enemy_y) < 200
) {
game.enemies[enemy].explode();
// Don't remove the bullet immediately, mark it as non-existent
this.bullets[b].exists = false;
if (!game.enemies[enemy].alive) {
this.score += 1;
print(this.score);
}
}
}
}
}
}
// Remove non-existent bullets outside the loop
this.bullets = this.bullets.filter((bullet) => bullet.exists);
// Check for enemies reaching the left side of the screen
for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
if (game.enemies[enemy]) {
// Check if enemy exists
let enemy_x = game.enemies[enemy].x;
// Check if the enemy is alive before doing any further checks
if (game.enemies[enemy].alive && enemy_x <= 0) {
game.enemies[enemy].explode();
if (!game.enemies[enemy].alive) {
this.health -= 1;
game.enemies.splice(enemy, 1);
}
}
}
}
// Check for collisions between enemies and the player
for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
if (game.enemies[enemy]) {
// Check if enemy exists
let enemy_x = game.enemies[enemy].x;
let enemy_y = game.enemies[enemy].y;
let player_x = this.x;
let player_y = this.y;
let player_r = this.w / 2;
let enemy_r = game.enemies[enemy].img_w / 2;
// Check if the enemy is alive before doing any further checks
if (
game.enemies[enemy].alive &&
dist(player_x, player_y, enemy_x, enemy_y) < player_r + enemy_r
) {
game.enemies[enemy].explode();
this.health -= 1;
if (!game.enemies[enemy].alive) {
game.enemies.splice(enemy, 1);
}
}
}
}
// Remove exploded enemies outside the loop
game.enemies = game.enemies.filter((enemy) => enemy && enemy.alive);
}
*/
// check_collision() {
// // Check for bullet-enemy collision
// for (let b = this.bullets.length - 1; b >= 0; b--) {
// if (this.bullets[b].active) {
// // Check if bullet is active
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy].alive) {
// // Check if enemy exists
// let enemy_x = game.enemies[enemy].x;
// let enemy_y = game.enemies[enemy].y;
// let bullet_x = this.bullets[b].x;
// let bullet_y = this.bullets[b].y;
// // Check if the enemy is alive before doing any further checks
// if (
// game.enemies[enemy].alive && this.bullets[b].active &&
// dist(bullet_x, bullet_y, enemy_x, enemy_y) < 200
// ) {
// game.enemies[enemy].explode();
// this.score += 1;
// game.enemies.splice(enemy, 1);
// // if (!game.enemies[enemy].alive) {
// // this.score += 1;
// // print(this.score);
// // game.enemies.splice(enemy, 1);
// } else if (!game.enemies[enemy].alive) {
// this.score += 1;
// print(this.score);
// }
// }
// }
// }
// }
// // Check for enemies reaching the left side of the screen
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy]) {
// // Check if enemy exists
// let enemy_x = game.enemies[enemy].x;
// // Check if the enemy is alive before doing any further checks
// if (game.enemies[enemy].alive && enemy_x <= 0) {
// game.enemies[enemy].explode();
// if (!game.enemies[enemy].alive) {
// this.health -= 1;
// game.enemies.splice(enemy, 1);
// }
// }
// }
// }
// // Check for collisions between enemies and the player
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy]) {
// // Check if enemy exists
// let enemy_x = game.enemies[enemy].x;
// let enemy_y = game.enemies[enemy].y;
// let player_x = this.x;
// let player_y = this.y;
// let player_r = this.w / 2;
// let enemy_r = game.enemies[enemy].img_w / 2;
// // Check if the enemy is alive before doing any further checks
// if (
// game.enemies[enemy].alive &&
// dist(player_x, player_y, enemy_x, enemy_y) < player_r + enemy_r
// ) {
// game.enemies[enemy].explode();
// this.health -= 1;
// if (!game.enemies[enemy].alive) {
// // this.health -= 1;
// game.enemies.splice(enemy, 1);
// }
// }
// }
// }
// }
// check_collision() {
// let player_x = this.x;
// let player_y = this.y;
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy].alive) {
// let enemy_x = game.enemies[enemy].x
// let enemy_y = game.enemies[enemy].y
// // check collision bullets with enemy or end of screen
// for (let b = this.bullets.length - 1; b >= 0; b--) {
// if (this.bullets[b].active) {
// let bullet_x = this.bullets[b].x;
// let bullet_y = this.bullet[b].y;
// //check if bullet active
// if (game.enemies[enemy].alive && dist(bullet_x, bullet_y, enemy_x, enemy_y) < 150) {
// this.bullets[b].inactive();
// }
// if (this.bullets[b] >= width) {
// this.bullets.splice(b, 1);
// } else if (this.bullets[b].active && dist(bullet_x, bullet_y, enemy_x, enemy_y) < 150) {
// game.enemies[enemy].explode();
// this.score += 1;
// this.bullets[b].inactive();
// }
// } else if (!this.enemies[enemy].alive) {
// game.enemies[enemy].splice(enemy, 1);
// }
// }
// }
// }
// }
///////////////////////////////// THIS WORKSSS BUT HEALTH ISSUEEEEEEE
// check_collision() {
// // Check for collisions between bullets and enemies
// for (let b = this.bullets.length - 1; b >= 0; b--) {
// if (this.bullets[b].active) {
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy].alive) {
// let enemy_x = game.enemies[enemy].x;
// let enemy_y = game.enemies[enemy].y;
// let bullet_x = this.bullets[b].x;
// let bullet_y = this.bullets[b].y;
// if (dist(bullet_x, bullet_y, enemy_x, enemy_y) < 150) {
// // Bullet hits the enemy
// game.enemies[enemy].explode();
// this.score += 1;
// this.bullets[b].inactive();
// }
// }
// }
// }
// }
// // Remove inactive bullets that go beyond the right edge
// this.bullets = this.bullets.filter(bullet => bullet.active && bullet.x < width);
// // Check for collisions between enemies and the player
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy].alive) {
// let enemy_x = game.enemies[enemy].x;
// let enemy_y = game.enemies[enemy].y;
// let player_x = this.x;
// let player_y = this.y;
// let player_r = this.w / 2;
// let enemy_r = game.enemies[enemy].img_w / 2;
// if (dist(player_x, player_y, enemy_x, enemy_y) < player_r + enemy_r) {
// // Enemy hits the player
// game.enemies[enemy].explode();
// if (!game.enemies[enemy].alive) {
// this.health -= 1;
// }
// }
// // Check for enemies reaching the left side of the screen
// if (enemy_x <= 0) {
// game.enemies[enemy].explode();
// if (!game.enemies[enemy].alive) {
// this.health -= 1;
// }
// }
// }
// }
// // Remove exploded enemies
// game.enemies = game.enemies.filter(enemy => enemy.alive);
// }
check_collision() {
// Check for collisions between bullets and enemies
for (let b = this.bullets.length - 1; b >= 0; b--) {
if (this.bullets[b].active) {
for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
if (game.enemies[enemy].alive) {
let enemy_x = game.enemies[enemy].x;
let enemy_y = game.enemies[enemy].y;
let bullet_x = this.bullets[b].x;
let bullet_y = this.bullets[b].y;
if (dist(bullet_x, bullet_y, enemy_x, enemy_y) < 150) {
// Bullet hits the enemy
game.enemies[enemy].explode();
this.score += 1;
this.bullets[b].inactive();
}
}
}
}
}
// Remove inactive bullets that go beyond the right edge
this.bullets = this.bullets.filter(
(bullet) => bullet.active && bullet.x < width
);
// Check for collisions between enemies and the player
for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
if (game.enemies[enemy].alive) {
let enemy_x = game.enemies[enemy].x;
let enemy_y = game.enemies[enemy].y;
let player_x = this.x;
let player_y = this.y;
let player_r = this.w / 2;
let enemy_r = game.enemies[enemy].img_w / 2;
// Check if the enemy has already collided with the player in this frame
if (!game.enemies[enemy].collidedWithPlayer) {
if (dist(player_x, player_y, enemy_x, enemy_y) < player_r + enemy_r) {
// Enemy hits the player
game.enemies[enemy].explode();
game.enemies[enemy].collidedWithPlayer = true;
this.health -= 1;
}
}
// Check for enemies reaching the left side of the screen
if (enemy_x <= 0 && !game.enemies[enemy].collidedWithPlayer) {
game.enemies[enemy].explode();
game.enemies[enemy].collidedWithPlayer = true;
this.health -= 1;
}
}
}
// Remove exploded enemies
game.enemies = game.enemies.filter((enemy) => enemy.alive);
}
// check_collision() {
// let hitThisFrame = false; // Flag to track if player was hit in this frame
// // Check for collisions between bullets and enemies
// for (let b = this.bullets.length - 1; b >= 0; b--) {
// if (this.bullets[b].active) {
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy].alive) {
// let enemy_x = game.enemies[enemy].x;
// let enemy_y = game.enemies[enemy].y;
// let bullet_x = this.bullets[b].x;
// let bullet_y = this.bullets[b].y;
// if (dist(bullet_x, bullet_y, enemy_x, enemy_y) < 150) {
// // Bullet hits the enemy
// game.enemies[enemy].explode();
// this.score += 1;
// this.bullets[b].inactive();
// }
// }
// }
// }
// }
// // Remove inactive bullets that go beyond the right edge
// this.bullets = this.bullets.filter(bullet => bullet.active && bullet.x < width);
// // Check for collisions between enemies and the player
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy].alive) {
// let enemy_x = game.enemies[enemy].x;
// let enemy_y = game.enemies[enemy].y;
// let player_x = this.x;
// let player_y = this.y;
// let player_r = this.w / 2;
// let enemy_r = game.enemies[enemy].img_w / 2;
// if (dist(player_x, player_y, enemy_x, enemy_y) < player_r + enemy_r) {
// // Enemy hits the player
// if (!hitThisFrame) {
// game.enemies[enemy].explode();
// this.health -= 1;
// hitThisFrame = true; // Set the flag to true to avoid multiple health decrements
// }
// }
// // Check for enemies reaching the left side of the screen
// if (enemy_x <= 0) {
// if (!hitThisFrame) {
// game.enemies[enemy].explode();
// this.health -= 1;
// hitThisFrame = true; // Set the flag to true to avoid multiple health decrements
// }
// }
// }
// }
// // Remove exploded enemies
// game.enemies = game.enemies.filter(enemy => enemy.alive);
// }
display() {
//move and display ship
this.move();
this.check_collision();
image(this.img, this.x - this.w / 2, this.y - this.h / 2, this.w, this.h);
//display bullets
for (let i = this.bullets.length - 1; i >= 0; i--) {
if (this.bullets[i].active) {
this.bullets[i].display();
}
}
}
shoot() {
if (this.bullets.length < 2) {
this.bullets.push(
new Projectile(this.bullet_img, 200, 200, this.x + 40, this.y)
);
}
}
}
class Projectile {
constructor(img, img_w, img_h, x, y) {
this.img = img;
this.img_w = img_w;
this.img_h = img_h;
this.x = x;
this.y = y;
this.vx = 20;
this.active = true;
}
move() {
if (this.active) {
this.x += this.vx;
}
}
// check if bullet is active
// for (let enemy = game.enemies.length - 1; enemy >= 0; enemy--) {
// if (game.enemies[enemy].alive) {
// // Check if enemy exists
// let enemy_x = game.enemies[enemy].x;
// let enemy_y = game.enemies[enemy].y;
// let bullet_x = this.x;
// let bullet_y = this.y;
// // Check if the enemy is alive before doing any further checks
// if (
// game.enemies[enemy].alive &&
// dist(bullet_x, bullet_y, enemy_x, enemy_y) < 150
// ) {
// this.inactive();
// }
// }
// }
display() {
this.move();
image(
this.img,
this.x - this.img_w / 2,
this.y - this.img_h / 2,
this.img_w,
this.img_h
);
}
inactive() {
this.active = false;
}
}
class Enemy {
constructor(wave) {
this.wave = wave;
this.alive = true;
this.collidedWithPlayer = false;
this.img_w = 200;
this.img_h = 200;
this.x = width + 200;
this.vx = 0;
this.y = int(random(100, height - 100));
this.img = enemy_img;
this.explode_img = explode_img;
this.exploding = false;
this.exploded = false;
this.explosion_imgs = [];
this.explosion_idx = 0;
this.explosion_time = 0;
this.explosion_interval = 90;
this.alive = true;
// load explosion frames
for (let i = 0; i < 5; i++) {
this.explosion_imgs.push(this.explode_img.get(i * 200, 0, 200, 200));
}
}
move() {
// move if not exploding
if (!this.exploding) {
this.vx = -5 - (this.wave - 1) * 2;
this.x += this.vx;
}
}
explode() {
if (!this.exploded) {
this.vx = 0;
this.exploding = true;
// game.player.health -= 1;
this.exploded = true;
}
}
display() {
this.move();
if (this.exploding) {
this.show_explosion();
} else {
image(
this.img,
this.x - this.img_w / 2,
this.y - this.img_h / 2,
this.img_w,
this.img_h
);
}
}
show_explosion() {
if (millis() - this.explosion_time > this.explosion_interval) {
this.explosion_idx++;
this.explosion_time = millis();
if (this.explosion_idx >= this.explosion_imgs.length) {
// If reached the end of the explosion frames, reset
this.exploded = true;
this.exploding = false;
this.explosion_idx = this.explosion_imgs.length - 1;
if (this.explosion_idx == this.explosion_imgs.length - 1) {
this.alive = false;
}
}
}
image(
this.explosion_imgs[this.explosion_idx],
this.x - this.img_w / 2,
this.y - this.img_h / 2,
this.img_w,
this.img_h
);
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
let fromArduino = split(trim(data), ",");
if (fromArduino.length == 8) {
move_up = int(fromArduino[0]);
move_down = int(fromArduino[1]);
shoot = int(fromArduino[2]);
still = int(fromArduino[3]);
mode = int(fromArduino[4]);
white_up = int(fromArduino[5]);
white_down = int(fromArduino[6]);
red = int(fromArduino[7]);
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = game.player.health + "\n";
writeSerial(sendToArduino);
}
}