Kaboom game clone for the Atari 2600 canvas example
Time for yet another one of my canvas example posts, this time I thought I would make a canvas example that is a clone of the classic video game called kaboom that was ported to systems like the Atari 2600. This is a game that involves a character at the top of the screen called the mad bomber that moves back and forth across the the screen dropping bombs. The object of the game then is to catch these bombs rather than avoid them with a player controlled bucket that moves from one side to another. If one of the bombs is missed then all the bombs on the screen blow up, and you loose a bucket. You keep playing until you loose all you buckets and like many of these classic games the object is all about just getting a hight score and that is about it.
The nice thing about cloning some of these other games is that doing so if often fairly simple in a modern javaScript environment. Back in the 1970s it was way hard to make games like these, but now I can often slap a working prototype of many of these games together in a single day often. In addition even with very simple games such as this there is much room for originality, not just when it comes to skinning the game, but the logic also. For example there is working out how many levels there should be until the player hits the max level, how many points a bomb is worth, and many other little details such as the rate at which bombs are dropped.
So it can be fun, and interesting to clone a game like this, and play around with all the little details.
1 - The utils library for this kaboom clone canvas example
I often start off many of my canvas example posts with a custom utility module that is used for this canvas example alone. Some times I might have reusable methods that are for this project alone as it grows, but often it is just a collection of usual suspects.
When it comes to this kaboom clone I know that I am going to want bounding box collision detection, the distance formula, and my usual method that helps me get a canvas relative pointer position. So the module is just composed of those methods for now.
|
|
Any kind of method that I might use across two or more modules should be parked here.
2 - The kaboom module
Now that I have the utils module that I want it is time to get into the kaboom module. This module will be used to store the state of all kinds of values that have to do with the game logic. It is packed with all kinds of private helper methods and objects, but only provides a few public methods and properties that are used by additional modules outside of it when it comes to my draw module, and the main.js file that I will be getting to later in this post.
2.1 - The beginning of the kaboom.js module and some objects for the BOMBER and PLAYER
I start out the module with the start of an IIFE that will then be closed at the bottom of the module. If you are not familiar with this kind of module pattern yet now might be the time to look into it deeper.
At the very top I have some Objects that I am using for certain fixed properties for the bomber and player objects as well as some button objects.
|
|
2.2 - The LEVELS Object
Here I have some javaScript code that will generate an Object of Objects for each level in the game. This is one area where I find myself tweaking values all the time. Each level Object contains properties such as the Pixels per second speed of the bomber and the bombs that are dropped by the bomber.
|
|
2.3 - Clamp an Object
This is a little helper method that I have made that helps to clamp and object such as the bomber object to the limits of the range of movement. In this kind of game the bomber object and the player object are fixed to a single axis so i only need to clamp them to that axis.
|
|
2.4 - Movement methods
When it comes to display objects or sprites if you prefer for kaboom there us the bomber, the player, and then the bombs. Each of these display objects have there own movement helpers because of the differences in how they move, and how they are controlled.
|
|
When it comes to my move player helper method I worked out an AI control feature. The AI is a very crude yet effective way of going about simulating machine assisted input for this game, and for the nature of the game doing so is not so hard to work out.
2.5 - Some methods to use with bomb objects
There will be a need for some methods that are used to spawn bomb objects, as well as find out if a bomb has hot the player or reached the other side of the canvas matrix.
|
|
2.6 - Level check and game over check helpers
Some methods for checking if the level is over, and if the player has lost.
|
|
2.7 - Create state, and set level
Here I have the methods that are used to create a new state object, as well as set the state object to a given level.
|
|
2.8 - The public API and the end of the module
The Public API is just an Object literal that will be used to return a collection of methods and references to certain other things of interest in this module that I think should be public.
There is of course the create state method that I will wan to make public so that I can create a state of the kaboom module from a main.js file. That state object can then be passed to other public methods that act on such a state object. Mainly the pointer start method, and the update method.
|
|
3 - The draw module for the kaboom clone canvas example
The kaboom.js file is just for the state of the game, but not drawing the current state of the game. I have been working with javaScript and canvas long enough to know that it is important to separate the state of a game away from logic that is used to render the state of it by one way or another. So that is where the draw.js module for this canvas example comes into play.
|
|
4 - The main.js file and index.html
I have a utils.js, kaboom.js, and draw.js file out of the way so now it is time to pull all this together with a main.js file and some html. In this main.js file I will be creating and injecting the canvas element into the html pages that I will be using. Here in the main.js file I will also be attaching event handlers for the game that will be used to control the player buckets.
|
|
|
|
5 - Conclusion and future ideas
So this canvas example was a lot of fun to work on, and so far I have to say that this canvas example might be one of the best candidates so far when it comes to working on it more to turn it into something that might be considered and actual project of some kind. There are of course many other such canvas examples that are also competing for my attention so this one might still end up being left stuck where it is.