xxxxxxxxxx
102
var ship;
var asteroids = [];
var lasers = [];
var c = 0;
var target = 100;
function setup() {
createCanvas(windowWidth, windowHeight);
ship = new Ship();
for (var i = 0; i < 20; i++) {
asteroids.push(new Asteroid());
}
}
function draw() {
background(0);
for (var i = 0; i < asteroids.length; i++) {
if (ship.hits(asteroids[i])) {
textSize(32);
stroke(255);
strokeWeight(2);
fill(0, 102, 153);
text('HIT HIT HIT', width / 2 - 90, height / 2);
}
asteroids[i].render();
asteroids[i].update();
asteroids[i].edges();
}
for (var k = lasers.length - 1; k >= 0; k--) {
lasers[k].render();
lasers[k].update();
if (lasers[k].offscreen()) {
lasers.splice(k, 1);
} else {
for (var j = asteroids.length - 1; j >= 0; j--) {
if (lasers[k].hits(asteroids[j])) {
if (asteroids[j].r > 10) {
var newAsteroids = asteroids[j].breakup();
asteroids = asteroids.concat(newAsteroids);
}
c++;
asteroids.splice(j, 1);
lasers.splice(k, 1);
break;
}
}
}
}
shipdisplay();
}
function keyReleased() {
ship.setRotation(0);
ship.boosting(false);
}
function keyPressed() {
if (key == ' ') {
lasers.push(new Laser(ship.pos, ship.heading));
} else if (keyCode == RIGHT_ARROW) {
ship.setRotation(0.1);
} else if (keyCode == LEFT_ARROW) {
ship.setRotation(-0.1);
} else if (keyCode == UP_ARROW) {
ship.boosting(true);
}
}
function shipdisplay() {
if (c > target) {
lasers = [];
asteroids = [];
background(0);
textSize(32);
stroke(255);
strokeWeight(2);
fill(0, 102, 153);
text('GAME OVER', width / 2 - 90, height / 2);
text('SCORE : ' + c, width / 2 - 90, height / 2 + 70);
} else {
ship.render();
ship.turn();
ship.update();
ship.edges();
displayingtext();
}
}
function displayingtext() {
textSize(32);
stroke(255);
strokeWeight(2);
fill(0, 102, 153);
text('SCORE : ' + c, 10, 30);
}