Skip to main content

Give Me Just A Little More Time

In this post I want to make the game time limited. By doing this, I make it more of a challenge to beat the high score. I'm going to set the time limit for a level to 30 seconds - I think that's a good amount. Also, I don't want to just dump the player straight into the action unprepared, so and I'm going to add a couple of bits of  "ceremony" before and after the gameplay to make it a nicer experience.


There are a few main things that need to be added to make this happen:
  1. Add "sub" states to the Game state: intro, game and outro
  2. Intro state needs to show a countdown
  3. Game state will run the game as normal, but with a visible "timer bar" to show how long is left
  4. Outro state will show a summary of the game - score, high score, etc.

Let's start with defining these states:

States

First of all I will define the different states. I'll use an enum for this:
The current state will be held in a variable inside the main Game state object:
..and then I'll run different update and rendering code based on the current state. Here's the update() function:

Timer Bar

All three states show the timer bar at the bottom of the playfield. This shows as completely full during the intro state, then it drops down to empty during the game state. The code for this is in the render() function:

Intro State

In the Intro state I draw the game objects, but don't update them. This way the player can see how the level looks but can't control it. I show a countdown over the top of the objects using an Animator object.

The animation is pretty basic (it can easily be polished later) but it shows how I can re-use the Animator object and very little extra code to create a nice intro animation. It's also very easy for me to then use the end of that animation to trigger moving to the next state.

Game State

The Game state is pretty much the same as before - update game objects and render them. There's a timer that kicks us into the next state after 30 seconds of gameplay.

Outro State

Finally we come to the Outro state. This state shows a little ceremony where I display the score that the player achieved during the level and the current high score. If the player beat the current high score this time then I flash "NEW HIGH SCORE" on the screen too. The code is inside the render() function:


After a few seconds, the Outro state exits and we end up back at the title screen.

Wrapping Up

I added a time limit to the level and wrapped up the gameplay between an intro and an outro. I also changed a few other bits that weren't covered here:
  • Refactored the states so that each state is in it's own file
  • Made the player flash white when they are hit. During this flash the player is also unable to score any points. You can see the code for this in player.js
  • Tweaked the Zapper enemy that I added in the last post to make it a little more nasty
You can see all of the changes in the git repository on BitBucket, and you can play the game in it's current state online.

Next Time

I'm still heading towards getting level editing working in the game, but there are a couple of house-keeping activities that I want to get out of the way before that. I'll be working on these over the next couple of posts.

Comments

Popular posts from this blog

Set The Controls For The Heart Of The Sun

When we last met, I had just started work on a level editor. I'd got as far as adding the ability to select an object and move it around, and then I added an undo/redo system using the command pattern. That was a good start, but dragging items around the level is only fun for so long. What I really want to be doing is.. ..editing the properties of the bullet spawners! And I want to be doing this in realtime as the game plays so that I can immediately see what effect my changes have made to the gameplay of the level.

World In Motion

In the last post I touched on how easy it is to score in this game - you can just camp out on the score bullet spawner and watch the points rack up. I'm going to add a little delay to the score bullets so that they start out in a deactivated and uncollectable state, and then change to active and collectable after a short time - say, half a second. To make it clear what state they are in I'm going to add code to let me change the look of Objects and allow them to be animated.

The Long And Winding Road

I started this blog back in November of last year. My aim was to document the development of a Bullet Hell shoot-em-up, written in JavaScript. Here we are, six months later, and lots has happened. With 20 posts already written, I thought that now would be a good time to take stock of what I've already done, and to look to the future and see what's to come.