This week we continued our Shootah project. We did two main things;
- Changed the code to control the total number of bullets to those actually on screen
- Added an enemy that loops backwards and forwards across the screen
Controlling the Number of Bullets
In Part 2, we made a new bullet every time the user pressed the Up key and put it in the bullets list.
This meant that after a while we could have a lot of bullets, most off the top of the screen, which was even slowing some machines down.
To limit the number of bullets we did four things:
- Added a new property to the Bullet class called active and set it to be true
- In the move() function for the bullet class, we added a check for the bullet going off the top of the screen (this.y < 0) and, if true, set the active property to false
- In sketch.js, we moved the lines of code in the draw() function responsible for moving and drawing the bullets into a new function called manageBullets() and called it from draw().
- In manageBullets() we made a new list called active and put every bullet that was still active into it. We then made this the new bullets list.
We write a little code that printed out the total number of bullets to verify this was working.
Adding an Enemy
We added a new file called enemy.js and included it in the index.html file.
This file looked a lot like player.js. The main different was the move() function. Our enemy moves constantly left-to-right. When it gets too far off the right-hand side of the screen (checked in move()) we set its x position to be off the left-hand side of the screen instead. This makes it loop around.
We still have loads to do and we made a list on the day:
No more infinite bullets!
- Check edges so spaceship doesn’t disappear
- Collision detection
- Enemies shoot back (bombs)
- Background music
- Moving background
We’ve done two and we’ll do some of the others for sure.
The files for this week can be found on our GitHub repository.