Can draw green dot with bullet class 🤬
This commit is contained in:
@@ -7,18 +7,24 @@ class bullet {
|
|||||||
this.targety = targety;
|
this.targety = targety;
|
||||||
this.x = 500;
|
this.x = 500;
|
||||||
this.y = 100;
|
this.y = 100;
|
||||||
this.radius = radius;
|
this.radius = 10;
|
||||||
this.speed = speed;
|
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;
|
|
||||||
|
|
||||||
|
|
||||||
|
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
|
// Calculate the initial direction
|
||||||
direction = createVector(this.targetX - projectile.x, this.targetY - projectile.y);
|
this.direction = createVector(this.targetX - this.projectile.x, this.targetY - this.projectile.y);
|
||||||
direction.normalize();
|
this.direction.normalize();
|
||||||
|
|
||||||
|
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -26,15 +32,19 @@ class bullet {
|
|||||||
|
|
||||||
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.
|
//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);
|
fill(0, 255, 0);
|
||||||
circle(500, 500, 5);
|
circle(500, 500, this.radius);
|
||||||
projectile.add(p5.Vector.mult(direction, shotSpeed));
|
|
||||||
if (dist(projectile.x, projectile.y, directionX, directionY) <= 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;
|
hit = true;
|
||||||
}
|
}
|
||||||
else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) {
|
else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
|
||||||
bounceX = projectile.x;
|
bounceX = this.projectile.x;
|
||||||
bounceY = projectile.y;
|
bounceY = this.projectile.y;
|
||||||
shot = false;
|
shot = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -72,8 +72,7 @@ let playerPosX = 500;
|
|||||||
let playerPosY = 300;
|
let playerPosY = 300;
|
||||||
let bullets = [];
|
let bullets = [];
|
||||||
let playerSpeed = 5;
|
let playerSpeed = 5;
|
||||||
let radius = 20;
|
let radius = 40;
|
||||||
let myBullet = new bullet();
|
|
||||||
|
|
||||||
async function Movementloop() {
|
async function Movementloop() {
|
||||||
if (booleanArray[1]) {
|
if (booleanArray[1]) {
|
||||||
@@ -107,12 +106,15 @@ function setup(){
|
|||||||
createCanvas(1250, 600);
|
createCanvas(1250, 600);
|
||||||
frameRate(244);
|
frameRate(244);
|
||||||
// disable the outline of shapes
|
// disable the outline of shapes
|
||||||
|
myBullet = new bullet();
|
||||||
|
|
||||||
noStroke();
|
noStroke();
|
||||||
myBullet(playerPosX, playerPosY, radius, 5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the function draw() is called every frame
|
// the function draw() is called every frame
|
||||||
function draw(){
|
function draw(){
|
||||||
|
keyPressed()
|
||||||
|
myBullet.draw(playerPosX, playerPosY, 10, 5);
|
||||||
// clear the background with a transparent black color
|
// clear the background with a transparent black color
|
||||||
background(0,0,0,10);
|
background(0,0,0,10);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user