From 559621835f06edc6a9fe9af623790608901e9a04 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 30 Nov 2023 15:02:24 +0100 Subject: [PATCH] fixed timer, cleanup junk and improved bullets --- web/game.js | 37 ++++++++++++------------ web/js/basicbullet.js | 67 ++++++++++++++++--------------------------- 2 files changed, 44 insertions(+), 60 deletions(-) diff --git a/web/game.js b/web/game.js index 62121b7..23ae083 100644 --- a/web/game.js +++ b/web/game.js @@ -22,6 +22,9 @@ let hit = false; let shotPosX; let shotPosY; +let initialPlayerPosX = playerPosX; +let initialPlayerPosY = playerPosY; + let nextAttack; let bounceX = bossPosX; let bounceY = bossPosY; @@ -30,7 +33,7 @@ let predictiveBounceY; let bullets = []; let direction; let framerate = 120; - +let hasMoved = false; let time = 0; //let myBullet = new bullet(); @@ -40,7 +43,6 @@ function setup() { createCanvas(width, height); frameRate(framerate); - myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed); // disable the outline of shapes Movementloop() @@ -48,21 +50,13 @@ function setup() { } - -async function score() { - while (true) { +function score() { time += 1 / framerate; - await sleep(1000); - } } function life() { textAlign(CENTER); text(lives, 500, 20); - if (hit == true) { - hit = false; - lives -= 1; - } if (lives == 0) { fill(255, 0, 0); textSize(40); @@ -79,6 +73,12 @@ function life() { } } +function movementCheck() { + if (playerPosX != initialPlayerPosX && playerPosY != initialPlayerPosY) { + hasMoved = true; + } +} + function keyPressed() { if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) { playerPosX -= playerSpeed; @@ -161,14 +161,14 @@ function draw() { // draw player fill(0, 255, 255) circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize); - + movementCheck() // draw boss fill(255, 165, 0) circle(500, 100, 50); score(); textAlign(LEFT); - text(time, 10, 20); + text(int(time), 10, 20); randomAttackPattern() @@ -186,6 +186,7 @@ function draw() { function randomAttackPattern(){ if (shot == false || hit == true) { + spawnRandomBullet() spawnRandomBullet() spawnRandomBullet() @@ -202,29 +203,29 @@ function spawnRandomBullet(){ if (nextAttack == 1) { shotPosX = 500; shotPosY = 100; - bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); + bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved)); } if (nextAttack == 2) { shotPosX = random(0, 1000); shotPosY = 600; - bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); + bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved)); } if (nextAttack == 3) { shotPosX = 0; shotPosY = random(0, 600); - bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); + bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved)); } if (nextAttack == 4) { shotPosX = 1000; shotPosY = random(0, 600); - bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); + bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved)); } } if (patern == 2) { shotPosX = bounceX; shotPosY = bounceY; - bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); + bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved)); } } \ No newline at end of file diff --git a/web/js/basicbullet.js b/web/js/basicbullet.js index 3e08b72..3754c0a 100644 --- a/web/js/basicbullet.js +++ b/web/js/basicbullet.js @@ -1,68 +1,51 @@ class bullet { - //Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd. - constructor(targetx, targety, radius, speed, shotPosX, shotPosY) { - //"This" moet gebruikt worden om de variabelen aan te maken in de class en het zorgt er voor dat de variabelen niet alleen in de constructor gebruikt kunnen worden, - //maar ook in de rest van de class + constructor(targetx, targety, radius, speed, shotPosX, shotPosY, hasMoved) { // Add hasMoved parameter this.targetx = targetx; this.targety = targety; this.x = shotPosX; this.y = shotPosY; this.radius = radius; this.speed = speed; - - + this.bulletHit = false; this.directionX = null; this.directionY = null; if ((this.directionX === null) || (this.directionY === null)) { - this.directionX = this.targetx; - this.directionY = this.targety; + this.directionX = this.targetx; + this.directionY = this.targety; } - + this.hasMoved = hasMoved; // Set this.hasMoved to the value of hasMoved + this.projectile = createVector(this.x, this.y); - // Calculate the initial direction this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y); this.direction.normalize(); - - } - - - - - draw() { - //push en pop zorgen er voor dat de code tussen push en pop een eigen canvas heeft. Dus dat er meerdere projectielen tegelijk kunnen zijn. + } + + draw() { push(); fill(0, 255, 0); circle(this.projectile.x, this.projectile.y, this.radius); pop(); - - // Set the initial position of the projectile to the player's position - - - - - - } - - // // Move the projectile towards the movable circle - update(targetx, targety) { + } + + update(targetx, targety) { this.targetx = targetx; this.targety = targety; this.projectile.add(p5.Vector.mult(this.direction, this.speed)); - + let hit = false; let shot = true; - + if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) { - hit = true; - shot = false; - } - else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) { - // bounceX = this.projectile.x; - // bounceY = this.projectile.y; - shot = false; + hit = true; + shot = false; + if (this.hasMoved && lives != 0 && this.bulletHit == false) { + this.bulletHit = true; + lives -= 1; + } + } else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) { + shot = false; } + return { hit, shot }; - } - - - } \ No newline at end of file + } +} \ No newline at end of file