Learn to Code Asteroids – Lesson 8 – Smoke Effects Code Challenge
1st February 2020Asteroids for Binocular Vision Suppression Training
15th February 2020Learn to Code Asteroids – Lesson 9 – Speeding Up The Code
Speeding up the Code And Starting a New Level
As we’ve been working through the code for this game we’ve (well me really) have missed a couple of bits. So in this lesson we will fix these so we’ve got a completely working game ready for the final chapters.
The first but we need to fix is in the polygon on polygon collision detection. If you remember back to that lesson we talked about optimising our code to reduce the amount of strain it puts on our processor. While I was testing out the next section of the course I was finding that the game was slowing down when there were lots of asteroids, explosions and the alien ship on screen at the same time. When I checked polygon on polygon collision detection code I find that we forgot to add the course collision detection.
The polygon on polygon collision detection calls are point in polygon collision detection code multiple times as it loops through each point in each shape. This uses a lot of processing power so we only want to use this code when we absolutely have to. We were going to use an initial collision detection scheme based on the bounding ball algorithm. This would provide us with a course level detection scheme that would filter out any definite misses so that we only have to call our expensive code when we know there’s a chance of a collision. We’ve already written the code for this when we detect for our bullets hitting asteroids so we can just use a modified version of that.
Starting a New Level
Have a go at shooting all the asteroids and see what happens.
Well, at the moment absolutely nothing happens. Your ship stays on screen and is fully mobile it’s just as no asteroids left shoot. We need to add some code to take the player to the next level. This code will need to detect when all the asteroids have been shot, pause for a short time to let the player re-centre his ship, and then regenerate a new set of asteroids.
Since all of our asteroids are held in an array (a Lua table) we can simply test the length of our array to find out how many asteroids are being held in it. As our asteroids array is held in a variable called ‘asteroids’ we can simply use this piece of code to read how many asteroids are in it.
#asteroids
So in our main playing game loop recently need to keep testing this value to see when it hits zero. At this point our game needs to go into a new state where it is waiting for the level to regenerate. This will simply be a short time where we keep the player ship active and finish off any bullets and explosions. After this we can simply call the generateAsteroids function to create a new set of asteroids and then return to the main play game state.
Almost Finished
Once you’ve completed this lesson the only thing left code are the alien ships. These appear on each level after a short time if you haven’t destroyed all the asteroids. Again, I’ll be asking you to do as much of the coding as possible so you get more more practice at developing your own algorithms and translating them into working code. So make sure you got your thinking head on for Lesson 10.
Don’t forget that all the code for this lesson can be downloaded using the link below.