Inspiration:
I came up with the game while thinking about my childhood games. Before starting to work on the project I kept brainstorming possible ideas that I can work on. One thing in particular that crossed my mind was to make the snake game as it was the main game that we all played on our Nokia devices when we were kids. But I noticed that a lot of people are doing it already so I changed my mind about the second option: “spaceship destroyer”. When I was a kid there was this well-known game in my community and among my friends, we called it “chicken.” In that game, there was this spaceship that was flying in the air and it was firing lasers at chickens while avoiding colliding with their eggs. Of course, it was way more advanced than that as it had multiple powerups and multiple levels and it seemed to have a story. However, I wanted to do something similar. And that’s when I decided to make the game I made.
Challenges:
I think I experienced challenges in every step of coding that game. It all started with setting the size of the canvas. I think I overthought it as I was trying to find the perfect canvas size for the game. I ended up choosing a portrait-like canvas.
Then I had a problem controlling the spaceship. The arrows were not doing the job. The spaceship’s movement was very weird. I tried multiple ways to make it smoother, but a lot of them didn’t work at first. I eventually found a good setup for the arrows. And I figured out a way to make it go to the right and appear from the left and vice versa to make the user experience smooth.
I think the biggest challenge was to make the bullets hit the rocks falling down. It was just going through it for hours without making any obvious thing at all. After hours of debugging, I figured out a way to make it the rock disappear when the laser hits it.
Code:
controlling the spaceship:
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; } }
Generating rocks:
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++; } } } }
catching collisions and displaying rocks:
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--; } } }
Main spaceship class:
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; } } } }
The game:
Reflections:
Even though I faced several difficulties when creating the game and my original concept had to be modified, I am quite pleased with the outcome. Through making mistakes, I gained a lot of knowledge. I also discovered that it’s a pretty good approach to advance to break the problem into smaller challenges. I began the code with simple shapes before starting to add my assets, animation, point system, and other things. I examined the animation code independently and just inserted it once it began to move.
Good use of classes (though note that class names are usually made with an uppercase first letter by convention – you should make “Spaceship”). Game works well! The sound effects are nice. Would be cool to have a sound effect or animation when you destroy a rock to give more feedback to the player. Nicely done!