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

TwitterFacebookRedditGoogle+EmailPinterest

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

TwitterFacebookRedditGoogle+EmailPinterest
  • Brian Flauto

    THANK YOU! THANK YOU! THANK YOU! I have been struggling with the Nokia 5110 using a N+2 for the longest time. I’m still learning about SPI and PWM. Using this tutorial + .Net MF 4.3 I finally got it working. Cant say thanks enough!

    • RogueCode

      Awesome, glad it was useful :D