Issue #30

Issue #30

In light of the recent Elm Game Jam I got slightly interested in this topic and decided to cover it for this special. I don't have any experience in game development, so sifting through the articles and tools was quite interesting.

To start off, there are two somewhat outdated but still comprehensive lists.

At the heart of each game there is a game loop. In fact, the same pattern is used in many other places too. If you've ever programmed Arduino, you know there's the setup function that is called once, and there's the loop function that is called repeatedly. There are also loops when you program RC cars, UAVs, and other tech: read data from sensors, apply error margin, calculate values to use with motors, etc. All of this is done during every loop iteration. This is a universal concept that is not tied to Elm. Koen Witters has written a very detailed article on how the game loop works. Rodrigo Monteiro has also shared his thoughts on this topic. Joël Quenneville explains how to use that concept and apply it to Elm in the following video:

Even if you're not a gamer or a game developer yourself, you might have heard of Unity or the Unreal Engine - game engines that simplify the development process. These contain tools for importing models, controlling light, physics engine that let you create real-looking movements, and much more. There is also a purely functional free and open-source game engine called Nu:

The Nu Game Engine is a game development platform that aims to  drastically reduce the cost of game development by providing a superior  programming model for games via Iterative Functional Reactive  Programming (IFRP). After finally reaching v1.3.0.4, the implementation  has matured enough to prove both the efficacy and viability of  functional programming in games in terms of performance,  programmability, and reliability.

While it's not using Elm for writing code, it is quite similar and it's using Elm-style architecture. If you want a quick intro, take a look at a nice tutorial by vsyncronicity on how to create a simple Mario-like game:

If you're looking to make games in browser and want to use Elm, you need to have a physics engine. Andrey, author of one of the lists above, is also the person behind multiple games, and most famous for creating  the w0rm/elm-physics. Sometimes these engines have bugs:

Have a look at the presentation Andrey gave on his work, including the tutorials he has written:

There is another physics engine for Elm jastice/boxes-and-bubbles if you want to build simpler 2D games. Take a look at what it can do.

In addition to having a physics engine, you likely need a way to render each frame on canvas. There are various ways to achieve that. For simple games you can use css animations and just move divs around. For something more complicated you might need to use WebGL which is a JavaScript API for rendering interactive 2D and 3D graphics inside the browser. Elm has a wrapper around it to make things simple. Have a look at some examples.

Using WebGL directly might seem quite complicated at first, and there are simple ways to start with development. Have a look at Zinggi/elm-2d-game by Florian Zinggeler which is an attempt to help you create games without the need to know or learn WebGL.

Looking for resources on the WebGL and physics, Andrey looks like the best expert on topic within the Elm community. There is also a Slack channel #webgl on the official Elm Slack.

Luca, another active member of the Elm community, has started a 12-part series (which is not yet complete) on building games in Elm. It is targeted at beginners, and has explanations Luca explains everything from the game loop to keyboard input, players, and rules.

Games can be very different, not just 2D or 3D shooters. There are also board games, text-based games, pixel games. And all of these can be built in Elm. Lucas Payr, another active community member, has created a pixel engine to create turn-based pixel games:

Elm narrative engine is a library to help you create interactive story games.

Overall there are quite a lot of resources, tools, and examples to build games in Elm, from extremely simple to something quite complicated. If you want to start creating one, I suggest you participate in the next Elm Game Jam.

To finish this issue, here's a quote from the game I was obsessed with when I was young:

It’s time to kick ass and chew bubble gum…and I’m all outta gum.

From Duke Nukem

Show Comments