Thursday, 4 June 2015

Game #5 : Asteroids

Asteroids is now up, in both code and runnable form here.

Note that it is now on github in .caproj format, not as a .capx. I had a problem with a C2 bug I cannot reproduce occasionally losing image files.


As you can see this has two layouts, there is a high score table implemented with localstorage and a main screen.

The localstorage implementation is fairly straightforward ; it checks for the presence of the key (in the constant ASTEROID_KEY) and if present, loads it into the array of high scores, if not it loads it with defaults (incidentally the name is always AAA). It then adds the score returned (initially this is -1), sorts the array and reverses it (highest first) and displays it, writing it back to localstorage at the same time.

In the main events, a score of -1 is indicative of a new game, and events 1 + 2 do that reset. Events 3 to 6 create asteroids and set values like speedScalar (how fast it's going), update the score and so on (it reloads the layout to start a new level).

Asteroids are wrapped bullets, and there is only one object which is scaled to make the three types. This is done using currentSize and requiredSize - if you look at 5 it sets current to -1 and required to 1. Going down for a minute to event 10, if it finds these values to be different, it resizes, sets speed, rotate speed  and animation of the asteroid. So the asteroid can be automatically resized just by setting "requiredSize" (currentSize is initially set to -1 in case of a previous value).

Events 7-9 are over complicated because I didn't realise you could PickNearest - this works out all the distances the asteroids are from the player if the player is "hiding" - in Asteroids there is a delay when the player appears so it doesn't appear on top of an asteroid, and enables the player if they are all far enough away.

Events 11 through 15 handle the Asteroid moving - rotation, thrust and in 15 the natural friction slowing it down. Event 16 handles spawning of bullets, allowing a maximum of four, and enforcing a delay time between the bullets (the lastFireTime variable). This is all done with the help of CustomMovement.

Events 17 to 23 are slightly tricky. Initially it detects a collision between a bullet and an asteroid, uses hitAsteroid to make it unique (could have used Pick instance here). However it is slightly tricky because when you hit the non-smallest asteroids they split into two. What I do is to shrink the asteroid hit, and send it off in a new direction, and create another one (in 21) and in 24, when it finds this newly created asteroid (currentSize is -1) it fills in the details saved in the variables about how big it is and what direction it is going in.

Event 22 just destroys the smallest (size = 3) asteroid , and 23 forces a restart of the layout with the next level if all the asteroids are dead.

Event 25 onwards drives the UFO - there are two times for this timer, how long it takes the first time, and how long it takes subsequent times. If it has timed out, and there isn't one already, it creats a UFO, with a 50/50 chance of a large or small one (small ones are quicker and fire accurately). The UFO firing is done in 28, and the bullet is initially aimed at the player, 29 checks the instance variable in the UFO to see if it is a big one, and if it is randomises that angle.

Events 30 through 33 check for collisions with the player (hitting a UFO, UFO Bullet or Asteroid) and then if this has happened it restarts, and handles the restart, displaying the level number (35)or Game Over 36, when that Game Over fades out in 37 the game goes back to the high score table.

Finally 38 and 39 update the score and lives display in a function.

This isn't bad but it is a bit messy. I've winged it to date, from now on (Game #6 is Jetpac, a Sinclair Spectrum game) a little more consideration will go into the design, I shall use event groups and so on.

No comments:

Post a Comment