A Canvas game example called flappy collector
I want to make more canvas examples, some of which might turn into interesting game prototype projects maybe. Today I though I would make something like flappy bird, only maybe not a total clone that has nothing going on that sets it apart from that. When cloning something it is important to make at least some kind of effort to make it distinct after all. I shall call it flappy collector, until I come up with some better working title for such a canvas example as I am not so great with names.
The same basic idea is there though when it comes to having a display object at the far left side of the canvas and you click or tap the screen to have it jump up a little as it falls down constantly. So it is yet another kind of game like flappy bird, space invaders, or kaboom where you have the player sprite object fixed to one axis or another. Nothing wrong with that as there are plenty of grate games that have that kind of general game mechanic at play.
The nice thing about these kinds of projects is that it is very easy to get to that point where I have a clone of the general idea at least. However there is then the hard part of course and I would say that is where I go from there with it to make it more original so it is not just a cheep rip off. So with that said this post will be on my current standing flappy bird clone that is part of my canvas example collection.
1 - This is a canvas example post that is a game like flappy bird and what to know before hand
This is a post on a canvas example of a game that is like flappy bird only I tried to find a slightly novel spin on the game. It is not a getting started post on canvas, or javaScript in general, so I assume that you have some background on those topics before hand.
2 - The game module for this canvas example
To start out with I made a game module that contains public and private methods that can be used to create a game state, and work with that state also. This module follows the pattern of using an Immediately Invoked Function Expression to wrap up everything that is used internally and then return a public API that is used outside of the module.
2.1 - The beginning of the game module and a bounding box collision detection helper
To start off my game.js file I just wrote an IIFE to which I will return a public API with the return keyword that will then be stored in a global variable called game. I then started writing some helper methods that will be used internally for this game module, and one thing that I know that I will need for this kind of game is a collision detection method.
|
|
The subject of collision detection can quickly turn into a rabbit hole of sorts, but for this canvas example I am just going to use a method that I find myself copying and pasting from project to project and move on following the mantra of less code and more game. If you would still like to read into collision detection a bit more anyway I do have a post on the subject of canvas hit regions and collision detection.
2.2 - The spawn berry helper
So in this game the object is to just collect berries or not. It is a silly idea sure, but never the less that is the idea that I want a prototype for and that is it. So with that said I am going to need a helper method that will spawn a berry that will or will not be collected by a bird that is controlled by the player.
|
|
2.3 - The update berries method
Now that I have a method that will spawn berry objects I will also want a method that will update them. this method will loop over all the berries in the main state object and reduce the x axis value of each of theme by there current pixels per second value multiplied by the passed seconds value sense the last update. In addition to that this method also checks for collision detection, and splices out old berries that the bird hits, or goes out of bounds.
|
|
2.4 - Update bird pixels per second
The rate at which the bird moves is set by a pixels per second value same as the berries, or any display object that will be moving around for that matter. Over time I have found that this is the best way to go about moving display objects in any kind of real time game.
So with that said I also have a helper for updating the bird also, the expression for doing so is a little different of course though. The bird is the object that the player controls after all, and the flap property is a value that is true then the player taps of clicks the screen.
|
|
2.5 - Starting the public API and the new bird method
Now that I have all my helper methods it is time to start working out my public API that will be returned to the game global variable that can then be used elsewhere in the canvas example. For this I just created an object literal to which I will append public methods.
|
|
2.6 - The main update method
Now I have a main update method that is to be called in the main app loop or state machine.
|
|
2.7 - The flap method
A simple flap method that will be called by an even handler outside of the game module.
|
|
3 - Draw method module
So I have a game module worked out, and now I need something that can be used to render the state object that the game module is used to create and update.
|
|
4 - Lets test it out
Now for the main index html file and main javaScript file that will tie everything together.
|
|
|
|
5 - conclusion
So this canvas example when up and running is just a green box that will fall to the bottom of the canvas and then just stay there at the bottom. That is unless if a player clicks or touches the canvas and as such causes the green box to flap upwards sort of speak. As crude and silly as the canvas example may be, it is all ready starting to feel like a bit of a game, but more is clearly needed in oder to make this something that most people would actually want to play.
As of this writing I have made one additional example based off of this source code where I am automating the process of playing. This is not the kind of game that I would care to play manually, but it might be cool to have a game like this the sort of plays itself, but at any time I can just come in and play in manually for a moment.