added code from game.js

This commit is contained in:
sam
2023-11-28 13:19:06 +01:00
parent 3b877fc3f8
commit 9c862e7b7b

View File

@@ -1,14 +1,21 @@
class bullet {
//Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd.
constructor(x, y, radius, speed) {
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.x = x;
this.y = y;
this.x = targetx;
this.y = targety;
this.radius = radius;
this.speed = speed;
// Set the initial position of the projectile to the player's position
let projectile = createVector(shotPosX, shotPosY);
x = directionX;
y = directionY;
// Calculate the initial direction
direction = createVector(targetX - projectile.x, targetY - projectile.y);
direction.normalize();
}
@@ -19,6 +26,16 @@ 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) <= 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;
}
}