A canvas example game that is a fixed shooter hybrid
This post will be on a fixed shooter hybrid canvas example game, like that of centipede. The game is like a fixed shooter type game like that of space invaders or kaboom, however the player is able to move by two axis of movement rather than just one.Still the player can not just move anywhere the play display object must stay in an area at the bottom of the screen. So then the player is not truly fixed to one axis, but is fixed to an area. I do not aim to make a true clone of centipede, but I would like to just make a clean canvas example of something that is similar to that classic game.
If I get more time to come around and add some more features I am not really sure what more I can add to it that would be taking this kind of game into a new direction. I also have so many other canvas examples that also could use some more work. All i wanted to do with this example is just get the basic idea of this kind of game up and working though, and that is what this is.
1 - The utils.js file
First off when it comes to this canvas example I have a utility library as I often do with many of these canvas examples of mine. For this example I know that I am going to want bounding box collision detection, a clamp method to clamp a display object to an area. I also have a helper method here that helps be get a canvas relative position of a pointer object.
As I continuing working on this example, which I might at some point in the future, this library will grow with additional methods like this. So in other words this is a kind of application specific lodash of sorts where I am just adding methods that I am actually going to use in one or more other modules.
2 - The game.js file to create and update a state object
So in this section I will be going over the game.js file for this canvas example of a fixed hybrid type game. This module can be used to create and return a state object that is used outside of the module that contains properties that have to do with the areas for enemies and the player, as well as the player object and pools of display objects for shots and enemies.
Theer are methods for creating display object pools for both an array of enemies, as well as an array of shots that the player auto fires upward. I followed a standard where there is an active flag for each object, and that is used as a way to spawn and destroy from the game board. I sometimes go with this kind of approach with that rather than pushing and purging display objects from a pool.
For now I went with an auto fire feature for this game that I might keep in place as I continue to developed this one more when and if I get around to it. I could make it so that there are ways to fire by pressing a button or preforming some kind of action when it comes to working out how I go about handing input here. However chances are this is the kind of game where I would want to just keep firing over and over again anyway.
The get inactive helper method was designed in a way so that it will work with any pool object in general. I did start a unit module when starting this example, but I did not get far with it this time. When it get around to it much of the code here would be pulled from the game.js file into this unit module of sorts, but as of this writing I did not get around to that.
I do not want to get into to much detail about the module here as I might get around to chaining a lot of things, and then I would just have to write everything all over again about it when I do so with this one. In any case moving forward this will be the main module for creating an updating a main game state object, it all ready contains properties like how many enemies the player killed and so forth, so in the future it might contain values like level, and current weapon type when and if I get into a more interesting version of this project.
3 - The draw.js module
So then of course I have my draw module that will render to a canvas element. There are methods for just drawing a plain background as well as many methods for drawing certain aspects of the game state object. This way I can control the order in which cretin things are drawn when drawing to the canvas element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var draw = (function () {
return {
background: function (state) {
var canvas = state.canvas,
ctx = state.ctx;
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
},
board: function (state) {
var canvas = state.canvas,
ctx = state.ctx,
b = state.board,
pa = state.playArea;
// board
ctx.fillStyle = 'blue';
ctx.fillRect(b.x, b.y, b.w, b.h);
// play area
ctx.fillStyle = 'green';
ctx.fillRect(pa.x, pa.y, pa.w, pa.h);
},
// draw the player display object
player: function (state) {
var canvas = state.canvas,
ctx = state.ctx,
pl = state.player;
ctx.fillStyle = 'red';
ctx.fillRect(pl.x, pl.y, pl.w, pl.h);
},
// player shots
playerShots: function (state) {
var canvas = state.canvas,
ctx = state.ctx;
ctx.fillStyle = 'white';
state.player.shots.forEach(function (shot) {
if (shot.active) {
ctx.fillRect(shot.x, shot.y, shot.w, shot.h);
}
});
},
// draw enemies
enemies: function (state) {
var canvas = state.canvas,
ctx = state.ctx;
ctx.fillStyle = 'white';
state.enemies.pool.forEach(function (e) {
if (e.active) {
ctx.fillRect(e.x, e.y, e.w, e.h);
}
});
},
// info
info: function (state) {
var canvas = state.canvas,
ctx = state.ctx,
p = state.player,
b = state.board,
sx = b.x + b.w + 16,
sy = b.y + 16;
ctx.fillStyle = 'white';
ctx.font = '10px courier';
ctx.fillText('kills: ' + p.kills, sx, sy);
ctx.fillText('v' + state.ver, sx, sy + 10);
}
};
}
());
4 - Main.js, and index.html
Now for the current state of the main.js file for this canvas example. Here I am creating and injecting the canvas element as well as the state object created with he create method of the game module.
So when this canvas example is up and running I have the basic idea of the shooter type games like I wanted. I can more the player object around and shot objects move from the player object up to the top of the board. When a shot hits an enemy object it is killed, and I have a kill count that show how many enemy objects I killed so far.
So it is not much of anything to exciting from an end users perspective, but I just wanted the basic idea of the game working so far, and that is what this is. The question now is what more to I add from here when it comes to making the game a little more fun, and unique. There is a lot that comes to mind with this, and at some point in the future maybe I will get around to expanding this more.
5 - Conclusion
So this canvas example is showing some potential when it comes to making a project that might actually be a little fun and interesting. That is not saying much though because I have a lot of other canvas examples like this one that i would also like to put a little more time into also.
I all ready have some plans for additional versions of this canvas example, I might expand when it comes to making a general unit class for the project. It would be nice for there to be different types of units when it comes to enemies, for the speed and hit point values for the enemies to go up and so forth. I never get around to graphics with many of these projects and that is something that I would also like to change at some point, if not with this project one of theme. However there is always so many ides for projects and so little time.