From fa41b78969805f0412bba0bccd824d21a8cead55 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 28 Nov 2023 15:39:15 +0100 Subject: [PATCH] =?UTF-8?q?Can=20draw=20green=20dot=20with=20bullet=20clas?= =?UTF-8?q?s=20=F0=9F=A4=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webdev/basicbullet.js | 72 ++++++++++++++++++++++++------------------- webdev/game.js | 8 +++-- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/webdev/basicbullet.js b/webdev/basicbullet.js index 5ab7750..08f4c9d 100644 --- a/webdev/basicbullet.js +++ b/webdev/basicbullet.js @@ -1,44 +1,54 @@ 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) { - //"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 - this.targetx = targetx; - this.targety = targety; - this.x = 500; - this.y = 100; - this.radius = radius; - this.speed = speed; - - // Set the initial position of the projectile to the player's position - let projectile = createVector(this.x, this.y); - this.x = directionX; - this.y = directionY; - - // Calculate the initial direction - direction = createVector(this.targetX - projectile.x, this.targetY - projectile.y); - direction.normalize(); + //Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd. + constructor(targetx, targety, radius, speed) { + //"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 + this.targetx = targetx; + this.targety = targety; + this.x = 500; + this.y = 100; + this.radius = 10; + this.speed = speed; -} + + + this.directionX = null; + this.directionY = null; + if ((this.directionX === null) || (this.directionY === null)) { + this.directionX = this.targetx; + this.directionY = this.targety; + } + + this.projectile = createVector(600, 100); + // Calculate the initial direction + this.direction = createVector(this.targetX - this.projectile.x, this.targetY - this.projectile.y); + this.direction.normalize(); + + this.projectile.add(p5.Vector.mult(this.direction, this.speed)); + } -draw() { + 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. - fill(255, 0, 0); - circle(500, 500, 5); - projectile.add(p5.Vector.mult(direction, shotSpeed)); - if (dist(projectile.x, projectile.y, directionX, directionY) <= this.radius) { - hit = true; + fill(0, 255, 0); + circle(500, 500, this.radius); + + // Set the initial position of the projectile to the player's position + + + + if (dist(this.projectile.x, this.projectile.y, this.directionX, this.directionY) <= this.radius) { + hit = true; } - else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) { - bounceX = projectile.x; - bounceY = projectile.y; - 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; } -} + } } \ No newline at end of file diff --git a/webdev/game.js b/webdev/game.js index 26681db..ef2ef3d 100644 --- a/webdev/game.js +++ b/webdev/game.js @@ -72,8 +72,7 @@ let playerPosX = 500; let playerPosY = 300; let bullets = []; let playerSpeed = 5; -let radius = 20; -let myBullet = new bullet(); +let radius = 40; async function Movementloop() { if (booleanArray[1]) { @@ -107,12 +106,15 @@ function setup(){ createCanvas(1250, 600); frameRate(244); // disable the outline of shapes + myBullet = new bullet(); + noStroke(); - myBullet(playerPosX, playerPosY, radius, 5); } // the function draw() is called every frame function draw(){ + keyPressed() + myBullet.draw(playerPosX, playerPosY, 10, 5); // clear the background with a transparent black color background(0,0,0,10);