Phil Trelford's Array
POKE 36879, 255


January 22, 2012 14:59 by phil

It’s a bit of a long story. A chain of events would unfurl that would lead me inextricably to writing a clone of a 70s video game. It started a few weeks ago while exploring early 90s dance tracks on Spotify, when I happened upon the seminal EP - Clonks Coming by the Sweet Exorcist. Later a lack of content on Spotify would push me towards YouTube and to find a hypnotic collage of Space Invaders, Beach Balls, Pong and the BBC test card girl.


Tapping along to the retro beeps on the train to London, a couple of white rectangles started moving up and down with the keyboard and a small white square began floating diagonally, and then the train reached King’s Cross. Much later a copy of RetroActivity would arrive in the post and I would return to the rectangles and make the white square bounce. Well, not before I had spent a few hours playing with Rebirth. The code is posted as an F# Snippet and fits happily in just under 100 lines. You can play in the browser if you have the Silverlight plug-in installed, just click inside to start the game.

Player 1 keys 'Q' - up, 'A' - down. Player 2 keys 'P' - up, 'L' – down.

Today I added a few beeps and a score, and put the project up on BitBucket. I started out trying the new SoundEffect classs in Silverlight 5 that promises low latency sound but unfortunately it seems a bit temperamental and I had to switch to using the old MediaElement class.

There’s a couple of reusable routines that are specific to gaming, the first is to know what keys are pressed at any instant in time:

type Keys (control:Control) =
    let mutable keysDown = Set.empty  
    do  control.KeyDown.Add (fun e -> keysDown <- keysDown.Add e.Key)
    do  control.KeyUp.Add (fun e -> keysDown <- keysDown.Remove e.Key)
    member keys.IsKeyDown key = keysDown.Contains key

The second is to synchronize the game updates with Silverlight’s rendering:

let run rate update =
    let rate = TimeSpan.FromSeconds(rate)
    let lastUpdate = ref DateTime.Now
    let residual = ref (TimeSpan())
    CompositionTarget.Rendering.Subscribe (fun _ -> 
        let now = DateTime.Now
        residual := !residual + (now - !lastUpdate)
        while !residual > rate do
            update(); residual := !residual - rate
        lastUpdate := now

If you’re interested in playing and making simple games, or even just coding, why not pop down to Skills Matter in London this Thursday for a PacMan Kata.

Winking smile

Categories: F# | Silverlight | .Net
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed


January 23. 2012 18:03


Hi Phil, I like these little posts on the golden age of video games :)

Im just waiting for Xenon next!  

Maybe I'll get round to doing mine one day...


Add comment

(Will show your Gravatar icon)

  Country flag

  • Comment
  • Preview