A Canvas example of an idle game called flappy collector idle
This canvas example will be a more advanced version of the canvas example that I worked out that is a kind of flappy bird clone of sorts. In that post I made a canvas game example that is the basic idea of flappy bird where I just want to have a display object constantly drop down that is countered by the action of a player clicking or tapping the canvas. The canvas example is not a true clone of flappy bird of course, but the basic idea is there and that is all I wanted as a starting point at least.
In this canvas example I am just expanding from that canvas example by making it so the game plays by itself. The player can still play manually if they want to, but after a period of inactivity the game will just play automatically. The direction I was going with this one was to make this canvas example into an idle game of sorts where the player can play manually, but there is also some kind of automatic action also that will kick in when the player just lets the game run.
1 - What to know before continuing
2 - The game module
In this section I will be going over all the code that is contained in a main game.js file. This is the file that is used to create and work with a game state that is then rendered to the canvas with an additional separate module. Many of the methods here are not all that much different from what i worked out in my other post that is a simpler version of this. Other methods are hacked over a little, and some are new.
2.1 - The start of the module and bounding box
I start off the module with an IIFE that will contain a bunch of private methods used only in the module, and then return a public api to a global variable that is used outside of the module. The first private method that I have here is a bounding box collision detection method that is not any different from the one that I am using in the other canvas example.
2.2 - The spawn berry method
The method that I use to spawn new berries is a little different from what I worked out before. Here I am not taking into account the current berry level value of the state object, and using that to set the point worth of the berry as well as the speed in pixels per second.
2.3 - The update berries method
Just a very simple change has been made with the update berries method where I am adding points based on the worth of the berry that was collected rather that a static number literal.
2.4 - Set next level and level check methods
In this canvas example I added two new methods that have to do with setting the current berry level.
2.5 - Set the berry delay time
Another new method is one that can be used to set the berry delay time based on the current berry level.
2.6 - Update bird pps
the update bird pps method is not all the different from before. The one is just a place holder of sorts for what might eventually be a variable that is used to adjust things when it comes to auto play. That is if I continue working on this canvas example rather than others.
2.7 - The get should flap method
A new feature that is added in this canvas example is an auto play mode. This method is used to just find out if the bird should flap or not when working out a very simple AI for doing just that. That is why a game example like this is great for getting started with AI there is just one action that needs to be preformed, and I just need to work out some basic logic that makes the choice of preforming that action.
2.8 - The start of the public API and the new bird method.
Now it is time to to get to the public API. Here I have the same set of methods as before, but there are now a buncj of changes to make use of the new features.
The new bird method does the same as before, just returns a new bird state object that can then be used with all the other methods that act on such a state object.
2.8 - The update method
The main update method of the game module now checks to see if the berry level should be updated. In addition to this there are also some additional changes that have to do with auto play.
2.8 - The flap method
The flap method is now more than just a method that just sets the flap property to 1, making the method far less pointless.
3 - The draw module
Now that we have the game module out of the way it is time to have a draw module that is used to draw the current state of a bird object to a canvas element.
4 - The html and main.js file
Now to tie everything together with html and a main.js file. In the html I just have a single container div, and link to the game.js file, draw.js file, and main.js files.
In the main.js file I create the canvas element, and create an instance of the game module. For this kind of game I just need to attach a single event handler to the canvas that will call the flap method of the game instance. In the main.js file I also have the main app loop of the program.
The result when this is up and running is what I would expect a box moves up to the top of the canvas when it click or touch the canvas, and falls back down when I leave it alone. If I get the player controlled box to overlap with one of the other boxes I get a point. I am not sure what more I should add to this when it comes to moving forward, but this is all I had in mind for this example at least.
5 - Conclusion
I am generally happy with how this project has come together thus far, and I have to say that this project is one of several canvas examples that I might put more time into. With that said of course there is much more that needs to happen when it comes to making this into an actual project rather that just a simple canvas example for the sake of this post.
Still the general idea that I had is up and working I just need to get around to adding some more features, skin it, and see about promoting it some how. I do think that if I am going to have some kind of game project on each page of my website which is something I am thing of doing it should be some kind of project like this. That is you can play manually if you want to, but you can also just let the computer play for you actually. I have come to find that those kinds of games are the kinds of games I still like paying because I can let the computer play for me and then I can focus on something else, such as making such a game.