Old school ft. New school: Snake with a Lumia 920 controlling a 5110 LCD using a Netduino

Yesterday I was made a Nokia Developer Champion, and I have been working on this post on and off for a few days so the timing is perfect as a homage to Nokia.

This project definitely gave me a good helping of nostalgia, and I had more fun than any other project I’ve done.

The Objective was to remake the original snake game from 1997 that came with the monochrome Nokias, and display it on a Nokia 5110 LCD connected to a Netduino. Then have a Lumia 920 connected over Bluetooth as the controller.

A while back when looking around for display solutions for various projects I stumbled onto the 5110 LCD. I’m not sure *why* exactly they are so popular, but it turns out that you can find them easily online and they are dirt cheap! I bought a couple from DX recently and they work exactly how I expected, and are perfect for a lot of things. At the price, I highly doubt that the DX ones are real, but at less than $5 who’s going to complain?

Get them here: http://dx.com/p/replacement-1-6-lcd-screen-with-blue-backlight-for-nokia-5110-blue-145860 (they also have white), or from ebay etc.

What you need:

  • Netduino (I recommend a Netduino 2)
  • Bluetooth module
  • A Windows Phone 8 (should be a Nokia for authenticity sakes Smile with tongue out)

Netduino:

A Netduino community member, Omar, wrote an awesome library for using this particular LCD, which you can grab here: http://wiki.netduino.com/Nokia-5110-LCD.ashx

On that wiki the labels for wiring were in a different order and had slightly different names to my LCD, so if you have one from DX follow my wiring below, or use the wiki instructions.

board_final

Once you’ve downloaded the 5110 library, copy Nokia.cs out and paste it into your new Netduino project. You will notice a few errors in the code now because it was written for an older SDK. So we need to make two changes:

  1. Reference the SecretLabs.NETMF.Hardware.PWM assembly in your project, then add this using at the top of Nokia.cs:

    Then scroll down to line 115 and change the line to this:

    Then go down a few lines to 127 and change the line to this:
  2. The next problem is the default parameters on the constructor (line 123), so you can safely just kill the defaults because you will be setting them anyway:

     

OK, with the LCD setup, go ahead and implement the Bluetooth stuff as described here.

Now create a new class called Snake.cs which is where the snake game will go. I had never thought much about how Snake was written, but it was surprisingly easy to get the basic game up and running.

Here is a simple explanation of how the game works: We have an ArrayList (lack of generic lists in NETMF) which holds all points (simple class with X and Y) with the front of the snake at position 0. Each frame we remove the last item in the list (the end of the snake which will disappear because it is moving forward), then get the position of the first point and add/subtract either X or Y depending on the current direction, then insert that new point at position 0. So instead of moving every block to its new position we just delete the last block and then put a new one at the beginning.
Each frame we check that the new position is not touching the wall or itself (die if it is), and also check whether it touches a fruit (create a new fruit if it is, and increase score).

The whole game, which is mostly just properties and stuff, is only ~160 lines.

In Program.cs the main loop simply draws the blocks for the snake, walls, and fruit. I tried this game on both the Netduino and Netduino Plus 2 and the performance on the former wasn’t great. The drawing took a little too long which meant that the game never really sped up. So if you have a N2 then I recommend that you use that, however it does definitely work on the N1. Check the source for the rest of the code, but here is the main drawing loop on the Netduino:

 

Windows Phone:

The Windows Phone side is really simple, and is mainly just stuff to make it “pretty”.snakelumia
There is a transparent Border (could have been rectangle etc) over the 2, 4, 5, 6, and 8 buttons. Each has a LeftMouseButtonDown event which then sends the direction over Bluetooth to the Netduino.

The Netduino will send messages back to the phone when one of the following happens:

  • Player dies
  • Player scores (eats a fruit)
  • The game is ready to start after dying

Each one of those is just a small message which the phone directly displays in a TextBlock on screen in an “LCD” font.

There are 3 MediaElements on the UI (all are invisible). The first will play the beep sound when a button is pressed, the next will play a happy jingle when they score, and the last will play a dying sound when they die.

When the player presses a button to turn, a little arrow is animated on the Lumia screen as a visual cue. This is simply a vector path object, with a storyboard to animate it. The storyboard has two DoubleAnimations – one to make it fade, and the other to slide it up, left etc.

There are four arrows, each with different rotations:

And then each has a StoryBoard:

Finally, pressing a button also uses the Windows Phone vibrate API to make a small vibration.

 

Pretty simple huh? A bunch of code wasn’t mentioned here, so make sure you download the source below:

Download the Netduino + WP8 Source Code (~1.6MB)

 

If you’ve got question you can ask me here or on the Twitter machine: @roguecode

Making a Windows Phone weather app in an hour

TechEd Africa 2013 ended a few days ago in Durban, South Africa.

I did a couple of talks – one of which was an hour-long session where I guided the attendees through the process of creating a good-looking weather app for Windows Phone. There are no shortage of weather apps in the marketplace, but it was a nice example to bring in a lot of different real-world situations like consuming JSON, using MVVM, and lots of binding stuff.

The point was to show how easy it is to create an app quickly. It only took 20 lines of code and 60 lines of XAML (half of which are closing tags so don’t count). The app would take around 15 minutes to make if I weren’t explaining.

BIJvreECYAIWiDb

Continue reading

Jumpstart to Windows Phone and Unity 3D

EDIT: This is now also syndicated on the Nokia Developer Wiki here.

I’m currently sitting on a 12 hour flight from Amsterdam to Johannesburg, South Africa. So writing this is a far more compelling option than watching bad quality movies on a grainy 7” screen.

The beta of Unity 3D came out yesterday (a couple days ago by the time I will be able to post this) for Windows Phone 8, and it’s awesome. My biggest problem with it at this stage is that because of how easy it is, it feels very close to cheating – but hey, time is money and why should you be wasting time fighting with technical problems when you could be using it to make your idea a reality?

For those living under a medium-sized boulder, Unity 3D is middleware to create cross-platform games quickly and easily – and it excels at that. It is made up of the main IDE (which has free and paid versions) and then an exporter for each platform (desktop is free, but all the mobile platforms require a normal or pro purchase).

Continue reading

Netduino + WP8: Real life Maze game

When I was young my parents bought me a wooden maze game. I loved that thing. I don’t think I ever cared much for the actual maze element – but the mechanism to tilt the stage was intriguing, and very simple.

When I remembered it the other day I managed to both find it online, and actually find my original one in an old cupboard from storage.

Continue reading

More Netduino + WP8 + Bluetooth fun- 3D reconstruction

Following my previous post on Bluetooth with the Netduino, I thought I would extend it a bit.

The goal for this project is the following: Have a Netduino sending it’s orientation data (XYZ) over Bluetooth to WP8 which displays a 3D representation of it (at the right angle of course), and for bonus points attach a potentiometer to control the zoom.

Netduino + Sonar + WP8 + Bluetooth – Controlling sound with your mind

OK, maybe not with your mind, but at the very least, your hand.

The objective here was to add a sonar (ultrasonic) sensor to the Netduino, work out how close my hand was, and then send that info over Bluetooth to Windows Phone 8 and display it. Bonus points for doing something useful with the data (yes, controlling the volume of the Imperial March is totally useful!).

Continue reading