Playing with HTML5

In the last weeks I have been playing with HTML5 in a very literal way, merging two of my interests: web development and video games. The result is a small arcade game which makes use of several features from the new web standard.

You can take a look at the code on GitHub at any time and even play it online here. Let me comment on some of the implementation details below:

The game loop

The game loop is implemented using the function setInterval() from the standard JS libraries. In particular, a callback is invoked 30 times per second: it checks the state of the game entities, updates them and then redraws the scene. This was the best option given that I’m not using any development framework.

Canvas

The game area is contained in a canvas. The canvas has been around long before HTML5 took shape, but it has received quite a lot of love recently in the form of acceleration. The game code is not specially designed for performance (after all, it was mostly developed in a week), and the scene is being completely redrawn 30 times per second. Nonetheless, I could try it in a first-generation Atom laptop with no noticeable lag.

Bitmaps

The canvas element has primitive operations to draw vector graphics (lines, bezier curves, text…) but 99% of the scene has been implemented using bitmaps, piled in several layers. This way of working is really similar to using a low-level graphic abstraction like SDL in the end.

What’s more, the bitmaps are sent to the browser packed in sheets that contain several sprites. It is a common technique in web development to save bandwidth and to overcome the limit in the number of simultaneous requests that browsers can send, but its use in video games dates from longer ago, when programmers had to deal with memory limitations.

Input

The game is controlled with the keyboard, and I use callbacks for standard key events (keyupkeydown) to check its state. These events act asynchronously and independently from the game loop, so the state of the keys is stored in an object which is checked in the update entities phase of the loop.

It also has to be taken into account that the key event callbacks are set on the canvas object, as a consequence events are lost if the focus moves out of the canvas. I have found some weird interactions when the focus changes while keys are pressed, so this part of the code definitely needs some work yet to become more robust.

Sound

I’ve used the web audio API to be able to manipulate sound with a high level of detail. Though I haven’t used most of its capabilities by far, it was worth connecting all the wires to make it work.

This API is designed as an audio flow going through several nodes, similarly to other popular multimedia frameworks. Each node has the ability to process the input in some way before sending it to the next node. I used only one intermediate node, the GainNode, to be able to mute the audio output at will, but it could have been used to implement an internal volume control.

Wishlist

A project is never complete with a list of things one would like to add, complete or polish. I’ve already mentioned some details worth revisiting, but there are also some features that I would have liked to use; examples are local storage to implement high score boards or network code to implement some sort of interaction between players. But in the end of the day, this has been a worthwhile exercise; I’m satisfied with the result and  I might still improve it in the future!

The latest additions to my collection!

I really love accumulating old (and not so old) gaming devices and games. Recently I got two distinguished members of the club of handheld game consoles 😉 . The first one is the well-known Game Boy Color:

My Game Boy Color

A Game Boy in its original package, with some games. Great!

The second one is not so popular, but very interesting: it’s a GP32 BLU (in the photo, with my tablet to compare its sizes 😉 ):

My GP32

This console was released in 2001, and it was very powerful in that moment: it featured a 133MHz ARM CPU, 10MB of RAM and a screen resolution of 320×240, quite impressive for a handheld device. But the most special feature is the possibility of developing your own programs with the SDK provided by the company and save them to SmartMedia Cards which were used as game cartridges; there were also a few commercial games, but not with an awesome quality. Anyway, their idea for game distribution was also interesting: some games were sold physically in their own cards, but some other were sold digitally via internet, to be downloaded and stored in your own card (obviously by a cheaper price 😛 ). As you can see, they implemented digital distribution long time before the “boom” we live in.

The GP32 wasn’t a huge success, but it was enough popular among hackers for the company to launch another machine, the GP2X, fully based on linux.

By the way, does any of you have a SmartMedia Card? Now I need one of those old relics 😉 .

Passage: a little surprise in intrepid repositories

Last week I was installing the latest Ubuntu (8.10 so far, time flies…) in a computer, for a relative of mine. Once I had finished, I took a look into the gnome-app-install application to look for new programs, and I was happy to found Passage in the games section. I knew about this game about a year ago; sources are available in the web page, as well as binaries for Windows and MacOS X, but I hadn’t found a user-friendly way (I mean, packages :P) to install it in GNU/Linux before.

Passage screenshot

Passage is a game about the flow of life and death. I think that it was born with the aim of demonstrating how a videogame can also be a work of art, in a classic way. Its graphics have the symbolism of a painting (built of huge pixels, of course), its gameplay has hidden meanings, just like poetry, and its crappy 8-bit music will stay in your head for ages. When the game ends and the screen fades off you stay for some seconds looking at that black screen, silent, shocked. I personally felt some kind of anxiety, and sadness.
I don’t want to reveal all the content of the game, so I recommend you to give it a try. A game lasts only five minutes, and installing it takes even less. After that, if you’re interested, you can read the creator’s statement to know everything he wanted to say in this game, and compare it with your ideas. Until I read it I couldn’t figure out why the punctuation system works as it does :S .
But at the end, I have noticed a curious contradiction: Passage is a game, but it isn’t fun. It’s all about feelings: you can play it once or twice, for some minutes, to try it and feel what it has to communicate. But, at the end, you won’t play it anymore. Don’t misunderstand me; it’s art, I have no doubt; but for me, Super Mario Bros. is also art for different reasons; and I still play with it every now and then 😉 .