xxxxxxxxxx
122
var vehicle;
var aestroids = [];
var isPressed = false;
var isMoving=false;
var score;
var totalBullets = 0;
var countScore = 0;
var rotationDir = 0;
var fireSound;
var gameOver;
var gameOverTitle;
var explode;
function preload() {
soundFormats('mp3','wav');
fireSound = loadSound('fire.mp3');
explode= loadSound('explode.mp3');
gameOver = loadSound('gameover.wav');
}
function setup() {
alert('use arrow keys to move and space to shoot');
createCanvas(window.outerWidth, window.outerHeight-300);
// createCanvas(800,600);
vehicle = new Vehicle();
gameOverTitle = createP(' ');
score = createP('Score :' + String(countScore));
aestroids.push(new Aestroid());
}
function draw() {
background(0);
textSize(15);
text(`Score : ${countScore}`,20,30);
text(`Accuracy : ${parseInt((countScore/totalBullets)*100)}`,10,50);
vehicle.show();
if(random(1)<0.005){
aestroids.push(new Aestroid());
}
// if(frameCount%15==0){
// vehicle.shoot();
// }
for(var j=aestroids.length-1;j>=0;j--){
aestroids[j].show();
aestroids[j].update();
if(dist(aestroids[j].pos.x,aestroids[j].pos.y,vehicle.pos.x,vehicle.pos.y)<aestroids[j].r){
gameOver.play();
textSize(32);
textAlign(CENTER);
text('GAME OVER! PRESS R TO RESTART',width/2,height/2);
noLoop();
}
for(var i=vehicle.bullets.length-1;i>=0;i--){
if(vehicle.bullets[i].checkFire(aestroids[j])){
explode.setVolume(0.1);
explode.play();
countScore++;
score.html('Score :'+ countScore)
vehicle.bullets.splice(i,1);
if(aestroids[j].r>35){
aestroids.push(new Aestroid(aestroids[j].pos,aestroids[j].r))
aestroids.push(new Aestroid(aestroids[j].pos,aestroids[j].r))
}
aestroids.splice(j,1);
break
}
}
}
if(keyIsDown(38)){
vehicle.move(-1);
}
if(keyIsDown(37)){
vehicle.rotate(-1);
}
if(keyIsDown(39)){
vehicle.rotate(1);
}
if(isPressed){
vehicle.shoot();
}
if(isMoving){
vehicle.move(-1);
}
if(rotationDir!=0){
vehicle.rotate(rotationDir)
}
}
function keyReleased(){
if(keyCode===38 || keyCode===40){
vehicle.acceleration.setMag(0);
vehicle.velocity.mult(0.1);
}
}
function keyPressed(){
if(keyCode===13){
console.log(vehicle.bullets.length)
}
if(keyCode===32){
fireSound.play();
vehicle.shoot();
}
if(keyCode===82){
aestroids=[];
countScore = 0;
accuracy=1;
loop();
}
}