Phillip Trelford's Array

POKE 36879,255

Pacman Tiles

Back in January I built a sample Pacman maze script in F# to use at a Pacman Kata evening with the F#unctional Londoners group. Coincidentally there’s another Coding Kata this Thursday 26th July at Skills Matter. Anyway a couple of weeks ago I started playing with the sample again on the train to and from work, filling in some of the gameplay.

You can play the latest version with your cursor keys and 9 lives below:

(Right click to install game to desktop)

 
Windows 8

As the sample runs in Silverlight I thought I’d also try it out on it’s cousin WinRT. WinRT lets you build Metro apps on Windows 8. The transition code wise has been pretty straight forward and I now have a tile for the game appearing on my Windows 8 start page:

One click: From Metro tile to video game

 

WinRT is yet another XAML based framework, and is very similar to Silverlight and WPF. One of the few differences I have encountered has been the namespaces the classes are in. For example in Silverlight the Canvas class is in the System.Windows.Controls namespace and in WinRT it is in Windows.UI.Xaml.Controls namespace. It is possible to target both WinRT and Silverlight from the same source code using conditional directives:

#if NETFX_CORE
using Windows.UI.Xaml.Controls;
#else
using System.Windows.Controls;
#endif

 

Multi-targeting

Multi-targeting Silverlight and WinRT is the route I decided to go down which allowed me to develop the game on my laptop running Windows 7 with Visual Studio 2010. Then I periodically tested it out on a desktop box running the Windows 8 Preview Release using Visual Studio 2012. Visual Studio 2012 does run on Windows 7, however WinRT does not.

The WinRT version of the app is implemented in F# and C#. The game part is written in F# as a portable library and the plumbing in C#. There’s a great walkthrough on Creating a Portable F# Library over on MSDN which describes this direction in some detail.

Code Fragment

When the ghosts are eaten they need to return to the enclosure. I used a flood fill algorithm to mark the shortest route from anywhere in the maze back to the enclosure. The flood fill is started inside the enclosure, the fill number is incremented with each iteration of the fill, so that the shortest route is to follow the lowest number adjacent to the ghost’s current square:

module Algorithm =
    let flood canFill fill (x,y) =
        let rec f n = function
            | [] -> ()
            | ps ->
                let ps = ps |> List.filter canFill
                ps |> List.iter (fill n)
                ps |> List.collect (fun (x,y) -> 
                    [(x-1,y);(x+1,y);(x,y-1);(x,y+1)]
                )
                |> f (n+1)
        f 0 [(x,y)]
 
Full Source

The full source code to the game is publicly available on BitBucket:

A single file playable script version is also available on F# Snippets:

Silverlight 5: Floating Windows (WIP)

The first stable release of the Silverlight 5 Multi-Window Controls project is now available on CodePlex which brings external window support to the ChildWindow and ContextMenu.

So what’s next if anything?

I came across Tim Heur’s Silverlight FloatableWindow project on CodePlex, that adapts the ChildWindow control for non-modal use, and has attracted over 10,000 downloads!

With Silverlight 5 it should be possible to have floating windows that in desktop mode (also known as out-of-browser or OOB mode) can be dragged in and out of the main window.

I started off with Jeff Blackenburg’s Silverlight drag and drop sample, from day 5 of the 31 days of Silverlight series, which lets you move coloured shapes around the main window. Tracing the mouse move coordinates when a drag operation escaped the main window showed that even though the captured object was left behind the x,y coordinates updated. Therefore all that was needed to drag out was to create a new external window when the drag operation begins and move the window as the mouse moves. Then if the drag operation ends inside the main window the external window can be discarded otherwise it should take ownership of the dragged control.

That worked so I updated the prototype to handle simple windows instead of shapes. To see it in it’s full glory you will need to right click on the Silverlight application below to install it on your desktop where you can drag the floating windows in and out of the main window:


This is a work in progress, so constructive feedback is particularly welcome.

The source is available on BitBucket: https://bitbucket.org/ptrelford/floaters

Once I’m happy with usability I plan to integrate it into the Silverlight Multi-Window Controls project on CodePlex.