Phillip Trelford's Array

POKE 36879,255

Building a game in a day

Last night I gave a talk at the F#unctional Londoners meetup about my experiences working in a team building a game in a day at the recent London GameCraft game jam event. We went with a continuous runner and used XNA to build the game on Windows with a hack to get it working on Visual Studio 2012, then Neil Danson was able to port it to iOS and Android using MonoGame. I brought along an Apple iPad and Google Nexus 7 both happily running the game.

iOS and Android

A recent article in the Guardian suggests that iOS and Android combined now generate four times the revenue of dedicated gaming handhelds. Both Unity and MonoGame let you target those platforms. I played a little with Unity at the Rezzed game jam early in the year, and MonoGame at GameCraft. As a coder by trade I felt more comfortable with MonoGame, where Unity can get you a long way fast but it felt more designer orientated (not necessarily a bad thing).

@ptrelford @qmcoetzee #theprismer on iPad from #gamecraft thanks #monogame!

Check out Neil’s article on F# and Monogame Part 4 – Content Pipeline


At a recent QuakeCon conference veteran game developer John Carmack spent a chunk of his annual monologue extolling the virtues of functional programming. F# is a rich functional-first programming language with excellent imperative and OO features when you need them. The experience is similar to the Lua programming language, which is hugely popular in gaming, with it’s light syntax and all important coroutine support. Given that it can run cross platform I think it’s an interesting choice for Indie games development. The XBLA title Path of Go is a good example of what is possible. There’s also a book on F# game development Friendly F# (Fun with game programming).

Path to Go

Code Samples

Berzerk shows how to build a simple game AI using seq expressions:

let zombie target state = seq {
    yield! random_pause state 10
    while true do
        yield! wait target state 50.0
        yield! home target state 10

Flint Eastwood is a small game I built in 6 hours at the first Dublin GameCraft event:

Flint Eastwood

Balls is a sound game, similar to Sound Drop I built last week:

Tsunami Balls

The Prismer is our entry to the London GameCraft game jam:


New York, New York

If you’re interested in learning more about F# check out the Progressive F# Tutorials on 18th/19th in New York followed by a GameCraft game jam on Friday the 20th.



While working on another game idea I bumped into a collision detection sample using the Processing language where you can draw walls for a ball to bounce off. It reminded me of SoundDrop on iOS. Here’s my own mashup using the collision detection code, Tomas Petricek’s reactive rectangles sample for the lines and a generated tremolo sound effect.

Along the way I found a “Chrome Experiment” version in JavaScript called BallsDropping and a very cute multi-level puzzle version created with Unity called drop.

I’ve put the code up on Bitbucket:

You can also edit and run the code as a script online in the Tsunami Cloud IDE:

Tsunami Balls 

Simply click the picture above to launch the Tsunami IDE with the script. Once loaded press CTRL+A to select the code, then hit the big green run button which will launch a window titled “Drawing”. You can then drag the drawing window out and dock it to the right or left of the code.

If you’re interested in learning more about games programming why not pop down to the F#unctional Londoners meetup this Thursday for a talk on building a game in a day.

Generative Art

Last night I ran a free hands on Generative Art session to a full class room at Skills Matter for the F#unctional Londoners meetup group. We host a hands on programming sessions every month, next month we’ll return to the Machine Learning theme with Matt Moloney from the Tsunami IDE team.


I recently picked up Matt Pearson’s Generative Art book published by Manning, his examples use the Processing programming language which is loosely based on Java. For the hands on creative part we used F# and the SmallSharp library which has a similar feel but is limited to 2D.


SmallSharp is a small .Net library for drawing graphics, similar to Small Basic but aimed more at the "Sharp" languages C# and F#

Small Basic the good parts:

  • minimal IDE: you get intellisense, buttons for opening and saving files and a big run button (F5)
  • simple library: type GraphicsWindow and dot to start drawing shapes, no need worry about Single Threaded Apartments, data binding or XAML

Small Basic’s library is just about usable from C# and F# but relies on strings and implicit conversions to a variant type, where as SmallSharp’s API takes explicit typed arguments.

In F# with SmallSharp we can write:

GraphicsWindow.BrushColor <- red
for i in 0..5..200 do


Which draws concentric lines:



I found a nice piece on Deviant Art entitled Bubbles:


The task was to generate a similar work, starting with the following code:

Win.Background <- black
let rand = System.Random()
let colors = [red; green; blue; yellow]
for i = 1 to 200 do
    Win.Opacity <- rand.NextDouble() ** 3.0
    Win.FillColor <- colors.[rand.Next(colors.Length)]
    let x = rand.NextDouble() * Win.Width
    let y = rand.NextDouble() * Win.Height
    let r = 10.0 + rand.NextDouble() * 30.0

Here’s a monochrome from David Kowalski:


and an interesting Spiral effect from Rob Lyndon:

Spiral Galaxy


Following Atwood's Law:

any application that can be written in JavaScript, will eventually be written in JavaScript.

I created the same effect using the HTML5 Canvas, with the F# code being compiled to JavaScript by the FunScript library, which also gives typed access to JavaScript libraries.

module Program

open FunScript
open FunScript.TypeScript

type ts = Api<"../Typings/lib.d.ts">

let circle (ctx:ts.CanvasRenderingContext2D) (x,y,d,c) =
   let pi = ts.Math.PI
   ctx.arc(x, y, d, 0.0, pi * 2.0)
   ctx.fillStyle <- c

let inline str x = x.ToString()
let rgba (r,g,b) a = "rgba("+str r+","+str g+","+str b+","+str a+")";
let next n = ts.Math.random() * n
let from n = ts.Math.floor(next (float n)) |> int

let main() =
   let canvas = unbox<ts.HTMLCanvasElement>(ts.document.getElementById("canvas"))
   canvas.width <- 1000.
   canvas.height <- 500.
   let ctx = canvas.getContext("2d")
   // Set background
   ctx.fillStyle <- "rgb(0,0,0)"
   ctx.fillRect (0., 0., canvas.width, canvas.height);
   /// Circle colors
   let colors = [
   // Draw circles
   for i = 1 to 200 do
      let x = next canvas.width
      let y = next canvas.height
      let r = 10. + next 40.
      let a = next 1.
      let c = rgba (colors.[from colors.Length]) a
      circle ctx (x, y, r, c)


Turing Drawings

We finished up on drawing roulette with Turing drawings, made by random Turing machines:


I created an F# version a few weeks back which you can run in the Cloud Tsunami IDE.

Have fun!