This week we carried on our “Shootah” space shooting game by adding some bullets with an appropriate sound effect. This brings the game to the stage shown below:
Not very snazzy but it’s starting to show some promise!
As with last week, this will only run if served up from a web server, we had a few ninjas in that missed last week and had to install node.js and local-web-server application.
Bullets!
Our Bullets were a pretty simple object that just moves up constantly – this has the following code:
class Bullet { constructor(x, y){ this.x = x; this.y = y; this.speed = 10; } move(){ this.y = this.y - this.speed; } show(){ push(); fill("yellow"); ellipse(this.x, this.y, 10, 10); pop(); } }
We created an array of bullets and added a new one to the array each time the up arrow is pressed. In addition, we played a “firing” sound which really adds to the feeling:
function preload(){ playerImage = loadImage("images/spaceship.png"); bulletSound = loadSound("sound/laser.mp3"); } function draw() { // .... skipping a few lines for(let i=0; i< bullets.length; i++){ bullets[i].move(); bullets[i].show(); } // .... skipping a few lines } function keyPressed(){ if(keyCode==UP_ARROW){ bullets.push(new Bullet(player.x, player.y)); bulletSound.play(); } }
The sound was done very similarly to the image – we created a folder (sound) and added a sound file we found on the internet to the folder. In the preload function we loaded the sound from the file into a variable (“bulletSound”). In the keyPressed function, we check if the pressed key is UP ARROW and if so, we play the sound and add a new bullet to the list.. voila!
Problems to work on!
A great start but a lot left to be sorted with this game! Here are some of the items:
- The bullets array grows forever – this really starts to slow down the game as we keep playing it. We need to add some code to remove a bullet from the array when it is gone off the screen.
- The player has no edge detection – we need some way to keep it onscreen.
- We need some enemies!!
- We need to get the bullets to check when they are hitting the enemies!
- We need to keep and display a score
- We need a way to die.. perhaps the enemies can drop bombs?
- Other ideas:
- Maybe a background tune in a loop?
- Maybe a scrolling starfield in the background?
- Maybe multiple levels?
- Maybe different types of enemies?
- Maybe a shield?
- Perhaps some different weapons or weapons you can pick up?
We have gotten this far so we might as well push forward and make this a fully working game so we’ll get some of the above sorted next week! The code is as usual up on our github. https://github.com/coderdojoathenry/creators2018