Computer games have been written in nearly every computer technology. In 90' C and C++ were the kings. As we moved to the era of internet the number of available technologies exploed. Clearly, HTML and Javascript opened new horizons thanks to new and far simpler distribution. You just type an URL into a browser instead manual copying of floppy disks.
Let's take a closer look at Vue.js and how it can be used to write computer games. In this series, we will focus on building a clone of a very old game from 80': Sokoban.
Levels in Sokoban are fairly simple. The level consists of walls, boxes, spots where boxes should be moved and one character who is moving the stuff in a warehouse. The character can push just one box and he can't pull anything.
The following text will reference 4th commit on branch article-01 from repository https://github.com/georgik/pf2019/
You can access the version by commands:
Let's define one level where w stands for wall, o and x stands for objects in the game:
Let's prepare the basic skeleton of an application so that we can display the map on the screen.
First of all, we're going to define references to Vue.js and Vuex libraries in index.html.
In the second step, we will define a template which will render our map into real HTML code.
The template just iterates ower the levelMap. Vue.js has a construct for rendering lists v-for. The first attribute tileRow will contain one line of the array from levelMap. rowIndex will contain the index of the row which is being rendered. We do not need rowIndex right now, but it will be useful in the future for positioning of tiles.
The second nested v-for is iterating over characters in each array. In this implementation, we're going to display just the character. Later on, we will map it to graphics and that will require some CSS tricks.
Let's finish construction of index.html. We will need a tag where Vue.js will plug the application. It could be achieved by following code in body.
Div tag is clear. But playground is definitely not an HTML tag. Here will Vue.js plug the component that we prepare in Javascript.
Last, but not least is include of our js/game.js file where Javascript logic will be stored.
Now we will move to the second file js/games.js. First of all let's store our levelMap in Vuex.Store state. Just define it for now. We will explore Vuex later on.
Since we have a map we can define our component playground which we have seen in index.html.
Here you can see that the definition of playground component is binding together HTML template with id playground-template from index.html with playground HTML tag which will be supplied by attribute levelMap which will be read from Vuex store.
Seems little bit mindbending, take your time to understand the code.
The last missing piece to make this whole thing work is the definition of Vue application itself.
The result should look like this:
The source code of this article is stored at Github.
In the next article, we will explain how to replace characters from a level map by tiles using CSS.
You can find more HTML5 games at https://georgik.rocks/tag/games