xxxxxxxxxx
93
let gameBullet;
let bulletArray = [];
function preload() {
bgpicture = loadImage("forest.jpg");
}
function setup() {
createCanvas(400, 400);
gameBullet = new Bullet(240,280);
}
// if mouse is pressed, add another bullet
function mousePressed(){
gameBullet = new Bullet(240,280);
bulletArray.push(gameBullet);
}
function draw() {
// image(bgpicture,height/2-mouseX, height/2-mouseY);
background(220);
// cross
stroke(0);
line(190,200,210,200);
line(200,190,200,210);
// target & background
noFill();
square(370-mouseX, 370-mouseY, 60);
circle(400-mouseX, 400-mouseY, 50);
circle(400-mouseX, 400-mouseY, 30);
circle(400-mouseX, 400-mouseY, 10);
line(400-mouseX, 430-mouseY, 400-mouseX, 500-mouseY)
square(190-mouseX, 370-mouseY, 60);
circle(220-mouseX, 400-mouseY, 50);
circle(220-mouseX, 400-mouseY, 30);
circle(220-mouseX, 400-mouseY, 10);
line(220-mouseX, 430-mouseY, 220-mouseX, 500-mouseY)
square(550-mouseX, 370-mouseY, 60);
circle(580-mouseX, 400-mouseY, 50);
circle(580-mouseX, 400-mouseY, 30);
circle(580-mouseX, 400-mouseY, 10);
line(580-mouseX, 430-mouseY, 580-mouseX, 500-mouseY)
line(0,500-mouseY,400,500-mouseY);
// bullets
for (let i=0; i<bulletArray.length; i++){
bulletArray[i].run();
}
}
class Bullet {
constructor(x,y){
this.originalX = x;
this.originalY = y;
this.posX = x;
this.posY = y;
this.posX2 = x;
this.posY2 = y;
this.speed = 4;
this.opacity = 255;
}
run() {
this.drawBullet();
this.fly();
}
drawBullet() {
noFill();
stroke(0,this.opacity);
line(this.posX,this.posY,this.posX2+10, this.posY2+20);
}
fly() {
this.posY -= this.speed;
this.posX -= this.speed/2;
this.posY2 -= this.speed*1.22;
this.posX2 -= this.speed*0.61;
this.opacity -= this.speed*2;
// if(this.posX2+10 - this.posX < 1){
// this.posX = this.originalX;
// this.posY = this.originalY;
// this.posX2 = this.originalX;
// this.posY2 = this.originalY;
// this.opacity = 255;
// }
}
}