xxxxxxxxxx
35
let bullets = [];
function setup() {
createCanvas(400, 400);
}
function draw() {
noCursor();
background(220);
rectMode(CENTER);
let xc = constrain(mouseX, 15, 385);
// player position
let player = { x: constrain(mouseX, 15, 385), y: height - 50 };
circle(player.x, player.y, 25);
drawBullet();
}
// function to draw bullets
function drawBullet() {
for (let bullet of bullets) {
bullet.y -= 10; // bullet moves up
circle(bullet.x, bullet.y, 10);
}
}
function mousePressed() {
let bullet = {
x: constrain(mouseX, 15, 385),
y: 350,
};
bullets.push(bullet);
}