xxxxxxxxxx
function setup() {
//Background
createCanvas(windowWidth, windowHeight);
background(100);
//Creating ships, lazers, aliens
ship = new Ship(650, 580, 80, 20, 5)
for (let x = 20; x < width; x += 50) {
for (let y = 20; y < height / 4; y += 50) {
aliens.push(new Alien(x, y, 50, 30, 15, 3, color(random(0, 255), random(0, 255), random(0, 255))))
}
}
}
function draw() {
//Showing and moving ship
background(100)
ship.show()
ship.move()
if (keyIsDown(LEFT_ARROW)) {
ship.left()
}
if (keyIsDown(RIGHT_ARROW)) {
ship.right()
}
//Showing, bouncing, and moving aliens
for (let i = 0; i < aliens.length; i++) {
aliens[i].show()
aliens[i].move()
if (aliens[i].x > width || aliens[i].x < 0) {
for (let alien of aliens) {
alien.bounce()
}
}
}
//Showing and moving lazers
for (let i = 0; i < lazers.length; i++) {
lazers[i].show()
lazers[i].move()
}
//Lazers hitting aliens
if (lazers.hits(aliens)) {
lazers.splice(i, 1)
aliens.splice(i, 1)
}
}
function keyPressed() {
//Pressinf lazers to move them up
if (keyIsDown(UP_ARROW)) {
lazers.push(new Lazer(ship.x + 35, ship.y - 50, 10, 3))
}
}