Category Archives: Raspberry Pi

8×8 Snake v2

A couple of weeks ago the lovely Lisa Mather (party planner extraordinaire) put out a request on Twitter for party games. This was for the Manchester Raspberry Jamboree after party on the 28th February (more on the jam in my next post).

So, having already cobbled the basic Snake game together in December I offered to polish it up a bit and add scoring functionality. In addition I felt it needed a better control interface than the keyboard, so a quick shout out to some local friends and I had a Quickshot II+ from a Spectrum ZX81 – it even has the clicky micro switches that any child of the 80’s instantly recognises – mmmmm, retro.

The core code from my last post hasn’t changed much, the main differences are;

  • swapping keyboard input routine for GPIO pins detection – one for each switch in the joystick (up/down/left/right/fire)
  • implementing code to record scores (1 point for each piece of food) and using two of the 4x seven segment displays from the Delorean Time Circuit for displaying them

Snake Wiring

Wiring was a pig (as you can see), but worth it in the end; even if I did have to do some gorilla soldering on Friday when a couple of leads came loose.  I highly recommend buying at least the 8×8 grid as a PCB kit (e.g. Pimoroni or CPC) to save your sanity.  The desoldering of the seven segments and subsequent wiring you see here took me a goodly portion of my Sunday afternoon 🙁

In the picture you can see 5 (extra chunky) resistors on the breadboard.  These are being used to pull the GPIO pins low (or off).  As the joystick is moved, the internal switches will complete the circuit with the +3.3v line (red wires) which will set the attached pin high (or on).  This is detected by the code and the snake moves the chosen direction (if it’s allowed to of course).

The game had a lot of interest and went down well with young and young at heart. The high score went to both Oliver (who achieved it first) and Michael with a score of 22!

Chris (3rd - 21) Michael  & Oliver (1st - 22)

Chris (3rd – 21) Michael & Oliver (1st – 22)

[updated source to follow]

8×8 Snake

8x8 Snake

One of the other things the Adafruit LED Backpacks are bundled with is an 8×8 LED matrix.  As I no longer needed my Time Circuit project I removed a backpack from the setup and ordered a Red/Green matrix from CPC.  I purposely bought the bi colour matrix as the red, green and yellow (r+g) gives me scope to play around more.

So what first?  Well, I needed to wire it up; and as I am using the Adafruit Python libraries again I need to make sure the red and green pins were wired according to their code (red = a0 – a7, green = a8 – a15).  To make sure everything was in order I ran the example Python code from the library which cycles through each LED in red, green then yellow.

Let’s Code
In my code there are four main variables

snake – a list object containing the x,y coordinates of each piece of the snake at any one time. These are in head to tail order.

food – contains the x,y coordinates of the current ‘food’ block on the display

direction – a string that holds the direction of travel of our snake r(ight), l(eft), u(p) or d(own)

grid – an instance of the Adafruit 8×8 grid library

The main function then has the following actions:

    • Check for key press; see if we need to change direction
    • Identify the next square the snake will move to
    • Check to see if it’s hit the edge or itself
    • Check to see if we’ve ‘eaten’ the food
    • Update the snake object to move it on one square
    • Draw the snake

To manage the snake the list object is used as a queue or First In First Out, aka FIFO, structure (that’s a proper programming term and everything kids!).  So every move is done in two steps,  insert the new head location into the first position (or push) and remove (or pop) the last item in the queue.  

Before the insert happens the new coordinate is checked to see if we need to do anything special.  

    • If the new head is outside the grid (hit the edge) or is the same location as part of the snake body then the code walks the snake list object and turns each coordinate red.
    • If the snake eats the food then the tail coordinate stays on the queue, extending the body by 1

That’s about it really.  The only other thing to note is that without using PyGame and an interactive screen capturing the keys is not quite as straight forward as you would hope.  There are two functions keyPress and control that I borrowed from the interwebs (there’s stacks of examples) to capture the key presses on a separate thread so that the main display loop (move snake, redraw etc) doesn’t hang waiting for a key press.

Source Code

What next?

Almost a couple of weeks on from the launch of this site c/o Raspberry Pi and it’s all quietened down now. So where next? I have a few projects I’d like to play with when I can find the time/impetus to get on to them.

  • D.I.Y. LED matrix – Buy a bucket load of LEDs, solder them to a breadboard and make messages/patterns.  Equally I could make a simple game of snake, pong or similar.  Yes yes I know there are ready made boards…. but where’s the fun in that
  • LED Cube – I find these fascinating and would love to build one
  • LEGO Bot – Who doesn’t love LEGO?  I have LEGO, motors and a Pi…. just need to combine them
  • LEGO Bot + Web – Web technology has never been my forte, but putting together a web page that could control the bot would be great.  Possibly harness the Google Coder project for it

I doubt I’ll get round to much more before the Egham Raspberry Jam comes around on 3rd November.  I’ve said I will bring along my Time Circuit and Flux Capacitor project so I can’t go too mad just yet.

What a difference a day makes

Knowing that Liz was going to put this on the raspberrypi.org blog I thought I’d add Google Analytics to see how well received my write up was.  Well, as you can see from the following graph; a couple of people liked it

Approx 4pm 18th Sept to 6pm 19th Sept

Approx 4pm 18th Sept to 6pm 19th Sept – click for more detail

So, Hello to the 1700+ people that have visited this site in the last 24 hours!  That stat just blows my socks off.  At it’s peak there were 132 hits inside and hour, on the real time stats I saw 31 hits in a minute go past.  Just wow!

Not only that but you lovely people come from all over the globe

Top ten countries visiting this site

Top ten countries visiting this site

If it's blue it's a hit (don't sink my battleship....)

If it’s blue it’s a hit (don’t sink my battleship….)

In all, 76 countries have seen the Raspberry Pi blog post/tweet and then visited this site; mind blown. This just goes to show the reach and reputation that the Foundation has built up over the past couple of years, hats off to them.

I’m humbled.