xxxxxxxxxx
313
let mainship;
let bullets = [];
let numBullets = 0;
let arrRocks;
let rocks = []
let rate = 0;
let lasersound;
let difficulty = 0;
// let xx;
let rocksctr = 0;
let gamestatus = "start";
let currentScore = 0;
let prevScore = 0;
function preload() {
img = loadImage('Hs4QN2.gif');
startScreen = loadImage('startscreen.gif');
ship = loadImage('Untitled-1.png');
bullet = loadImage('bullet.png');
soundFormats('mp3', 'ogg');
lasersound = loadSound('lasersound.mp3');
bomb = loadSound('explo.mp3');
rock1 = loadImage('rock1.png');
rock2 = loadImage('rock2.png');
rock3 = loadImage('rock3.png');
gameoverpng = loadImage('gameover.png');
mainFont = loadFont('metal lord.otf');
}
function setup() {
createCanvas(400, 550);
mainship = new spaceship();
arrRocks = [rock1,rock2,rock3] ;
textFont(mainFont);
// xx = new boulders();
}
function startPage(){
textSize(50);
fill(250);
text("TOP G",130,250);
textSize(20);
text("press enter to start",100,310);
}
function score(){
textSize(32);
fill(250);
text("Score: "+currentScore,20,50);
}
function increaseD(){
if(currentScore === 10 + prevScore){
difficulty += 0.5;
prevScore = currentScore;
// console.log(difficulty);
}
return random(1,5)+difficulty;
}
function keyPressed() {
if(keyCode === ENTER){
gamestatus = "running";
}
if (keyCode === UP_ARROW){
makebullet(mainship.x,mainship.y);
}
if(keyCode === LEFT_ARROW){
rate = -5;
}
if(keyCode === RIGHT_ARROW){
rate = 5;
}
if(keyCode === BACKSPACE){
rocks.splice(0,rocks.length);
rocksctr = 0;
}
}
function removeRocks(){
rocks.splice(0,rocks.length);
rocksctr = 0;
}
function keyReleased()
{
if (keyCode === RIGHT_ARROW || keyCode === LEFT_ARROW)
{
rate = 0;
}
if(keyCode === ENTER){
gamestatus = "running";
}
if(keyCode === BACKSPACE){
rocks.splice(0,rocks.length);
rocksctr = 0;
}
}
function displaybullets(){
for(let i = 0; i < bullets.length; i++){
bullets[i].display();
if(bullets[i].y < 0){
bullets.splice(i,1);
numBullets--;
}
}
// console.log(numBullets);
}
function generaterocks(){
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){
rocks[rocksctr] = new boulders();
rocks[rocksctr].display();
// console.log(rocksctr);
rocksctr++;
}
}
}
}
function displayrocks(){
for(let i = 0; i < rocks.length; i++){
rocks[i].display();
// console.log(">",rocks.length);
let temp = false;
for(let j = 0; j < bullets.length; j++){
if(bullets[j].didcollide(rocks[i])){
temp = true;
bullets.splice(i,1);
numBullets--;
}
}
if(mainship.didcollide(rocks[i])){
rocks.splice(i,1);
rocksctr--;
gamestatus = "end";
bomb.play();
}else if(rocks[i].y > height || temp){
rocks.splice(i,1);
rocksctr--;
}
}
}
function makebullet(x,y){
// console.log(x);
bullets[numBullets] = new bulletClass(x,y);
bullets[numBullets].display();
numBullets++;
}
function draw() {
background(0);
if(gamestatus == "running"){
image(img, 0, 0);
mainship.display();
displaybullets();
generaterocks();
displayrocks();
score();
}else if(gamestatus == "end"){
image(gameoverpng, 0, 0);
removeRocks();
currentScore = 0;
difficulty = 0;
prevScore = 0;
}else if(gamestatus == "start"){
background(startScreen);
startPage();
}
// xx.display();
}
class boulders{
constructor(){
this.x = random(0,width-50);
this.y = -20;
this.rocktype = int(random(0, 3));
this.rateFall = increaseD();
}
display(){
image(arrRocks[this.rocktype],this.x,this.y);
this.move();
}
move(){
this.y += this.rateFall;
}
width(){
if(this.rocktype == 0){
return 71;
}else if(this.rocktype == 1){
return 48;
}else if(this.rocktype == 2){
return 91;
}
}
increaseD(){
}
}
class bulletClass{
constructor(x,y){
this.x = x;
this.y = y;
lasersound.play();
this.collision = false;
}
display(){
image(bullet,this.x,this.y);
this.move();
}
move(){
this.y -= 7;
}
didcollide(other){
if ( (this.x <= (other.x + other.width())) && (this.x >= other.x)) {
if ((this.y <= (other.y + other.width())) && (this.y >= other.y)){
// print("Collision");
currentScore++;
return true;
}
}
}
}
class spaceship{
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 > width){
this.x = 0;
}else if(this.x < 0){
this.x = width;
}
}
didcollide(other){
if ( (this.x <= (other.x + other.width())) && (this.x >= other.x)) {
if ((this.y <= (other.y + other.width())) && (this.y >= other.y)){
// print("Collision");
return true;
}
}
}
}