xxxxxxxxxx
323
let bullets = [];
let asteroid = [];
let shooter;
let asteroids;
let asteroidsnum = 0;
let number = 0;
let difficulty = 0;
let currentScore = 0;
let prevScore = 0;
let gamestatus = "start";
let rate = 0;
let widthh = 400;
let fired = 0;
function preload()
{
img = loadImage('background.jpg');
start = loadImage('startgame.jpg');
ship = loadImage('ship.png');
bullet = loadImage('bullet.png');
asteroid1 = loadImage('asteroid1.jpg');
asteroid2 = loadImage('asteroid2.jpg');
asteroid3 = loadImage('asteroid3.jpg');
}
function setup()
{
createCanvas(400, 550);
shooter = new shot();
asteroids = [asteroid1,asteroid2,asteroid3] ;
}
function score()
{
textSize(32);
fill(250);
text("Score: "+currentScore,20,50);
}
function upgrade()
{
if(currentScore === 10 + prevScore)
{
difficulty += 0.5;
prevScore = currentScore;
}
return random(1,5)+difficulty;
}
function keyPressed()
{
if(keyCode === ENTER)
{
gamestatus = "running";
}
if (keyCode === UP_ARROW)
{
makebullet(shooter.x,shooter.y);
}
if(keyCode === LEFT_ARROW)
{
rate = -5;
}
if(keyCode === RIGHT_ARROW)
{
rate = 5;
}
if(keyCode === BACKSPACE)
{
asteroid.splice(0,asteroid.length);
asteroidsnum = 0;
}
}
function removeAsteroid()
{
asteroid.splice(0,asteroid.length);
asteroidsnum = 0;
}
function keyReleased()
{
if (keyCode === RIGHT_ARROW || keyCode === LEFT_ARROW)
{
rate = 0;
}
if(keyCode === ENTER)
{
gamestatus = "running";
}
if(keyCode === BACKSPACE)
{
asteroid.splice(0,asteroid.length);
asteroidsnum = 0;
}
}
function displayBullets()
{
for(let i = 0; i < bullets.length; i++)
{
bullets[i].display();
if(bullets[i].y < 0)
{
bullets.splice(i,1);
number--;
}
}
}
function generateAsteroid()
{
let rand = int(random(0, 100));
let rand2 = int(random(0, 100));
if(rand % 7 == 0)
{
if(rand2 % 3 == 0)
{
if(rand2 % 2 == 0 && rand % 2 == 0)
{
asteroid[asteroidsnum] = new type();
asteroid[asteroidsnum].display();
asteroidsnum++;
}
}
}
}
function displayAsteroid()
{
for(let i = 0; i < asteroid.length; i++)
{
asteroid[i].display();
let temp = false;
for(let j = 0; j < bullets.length; j++)
{
if(bullets[j].collision(asteroid[i]))
{
temp = true;
bullets.splice(i,1);
number--;
}
}
if(shooter.collision(asteroid[i]))
{
asteroid.splice(i,1);
asteroidsnum--;
gamestatus = "end";
}
else if(asteroid[i].y > height || temp)
{
asteroid.splice(i,1);
asteroidsnum--;
}
}
}
function makebullet(x,y)
{
bullets[number] = new bulletClass(x,y);
bullets[number].display();
number++;
}
class type
{
constructor()
{
this.x = random(0,widthh-50);
this.y = -20;
this.asteroidtype = int(random(0, 3));
this.rateFall = upgrade();
}
display()
{
image(asteroids[this.asteroidtype],this.x,this.y);
this.move();
}
move()
{
this.y += this.rateFall;
}
width()
{
if(this.asteroidtype == 0)
{
return 71;
}
else if(this.asteroidtype == 1)
{
return 48;
}
else if(this.asteroidtype == 2)
{
return 91;
}
}
}
class bulletClass
{
constructor(x,y)
{
this.x = x;
this.y = y;
this.collision1 = false;
}
display()
{
image(bullet,this.x,this.y);
this.move();
}
move()
{
this.y -= 7;
}
collision(other)
{
if ( (this.x <= (other.x + other.width())) && (this.x >= other.x))
{
if ((this.y <= (other.y + other.width())) && (this.y >= other.y))
{
currentScore++;
return true;
}
}
}
}
class shot
{
constructor()
{
this.x = 200;
this.y = 450;
this.display();
}
display()
{
imageMode(CENTER);
image(ship,this.x,this.y);
this.move();
this.checkboundries();
imageMode(CORNER);
}
move()
{
this.x += rate;
}
checkboundries()
{
if(this.x > widthh)
{
this.x = 0;
}
else if(this.x < 0)
{
this.x = widthh;
}
}
collision(other)
{
if ( (this.x <= (other.x + other.width())) && (this.x >= other.x))
{
if ((this.y <= (other.y + other.width())) && (this.y >= other.y))
{
return true;
}
}
}
}
function draw()
{
background(0);
if(gamestatus == "running")
{
image(img, 0, 0);
shooter.display();
displayBullets();
generateAsteroid();
displayAsteroid();
score();
}
else if(gamestatus == "end")
{
background(start);
removeAsteroid();
currentScore = 0;
difficulty = 0;
prevScore = 0;
}
else if(gamestatus == "start")
{
background(start);
}
}