Snake game for a 5x5 LED matrix

Dependencies:   MCP23S17 mbed

Files at this revision

API Documentation at this revision

Comitter:
dhamilton31
Date:
Wed Oct 09 16:23:20 2013 +0000
Child:
1:5fcb94bb03db
Commit message:
Snake movement completed, along with options to turn LEDs on and off with ledCube class.

Changed in this revision

bodyPiece.h Show annotated file Show diff for this revision Revisions of this file
draw.cpp Show annotated file Show diff for this revision Revisions of this file
draw.h Show annotated file Show diff for this revision Revisions of this file
ledCube.cpp Show annotated file Show diff for this revision Revisions of this file
ledCube.h Show annotated file Show diff for this revision Revisions of this file
ledValues.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
snake.cpp Show annotated file Show diff for this revision Revisions of this file
snake.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bodyPiece.h	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,10 @@
+#ifndef __BODYPIECE_H__   // if x.h hasn't been included yet...
+#define __BODYPIECE_H__   //   #define this so the compiler knows it has been included
+
+
+class bodyPiece{
+    public:
+        char currRow, currCol;
+};
+
+#endif // __BODYPIECE_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/draw.h	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,1 @@
+#include "mbed.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ledCube.cpp	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,51 @@
+#include "ledCube.h"
+#include "ledValues.h"
+
+// Set up the ledArray output pins
+ledCube::ledCube(){
+    ledArray[0][0] = new DigitalOut(P000);
+    ledArray[0][1] = new DigitalOut(P001);
+    ledArray[0][2] = new DigitalOut(P002);
+    ledArray[0][3] = new DigitalOut(P003);
+    ledArray[0][4] = new DigitalOut(P004);
+    ledArray[1][0] = new DigitalOut(P010);
+    ledArray[1][1] = new DigitalOut(P011);
+    ledArray[1][2] = new DigitalOut(P012);
+    ledArray[1][3] = new DigitalOut(P013);
+    ledArray[1][4] = new DigitalOut(P014);
+    ledArray[2][0] = new DigitalOut(P020);
+    ledArray[2][1] = new DigitalOut(P021);
+    ledArray[2][2] = new DigitalOut(P022);
+    ledArray[2][3] = new DigitalOut(P023);
+    ledArray[2][4] = new DigitalOut(P024);
+    ledArray[3][0] = new DigitalOut(P030);
+    ledArray[3][1] = new DigitalOut(P031);
+    ledArray[3][2] = new DigitalOut(P032);
+    ledArray[3][3] = new DigitalOut(P033);
+    ledArray[3][4] = new DigitalOut(P034);
+    ledArray[4][0] = new DigitalOut(P040);
+    ledArray[4][1] = new DigitalOut(P041);
+    ledArray[4][2] = new DigitalOut(P042);
+    ledArray[4][3] = new DigitalOut(P043);
+    ledArray[4][4] = new DigitalOut(P044);
+    
+    lastLedLit = NULL;
+}
+
+// Light the LED at the specified row, column, layer coordinate
+void ledCube::lightLed(char row, char column, char layer){
+    //if(lastLedLit != NULL){
+    //    *lastLedLit = 0;
+    //}
+    *ledArray[row][column] = 1;
+    //lastLedLit = ledArray[row][column];
+}
+
+void ledCube::turnOffLed(char row, char column, char layer){
+    *ledArray[row][column] = 0;
+}
+
+void ledCube::drawHorLine(char startRow, char startCol, char endRow, char endCol){
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ledCube.h	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,14 @@
+#include "mbed.h"
+
+class ledCube{
+public:
+    DigitalOut *ledArray[5][5];
+    int numRows, numCols;
+    ledCube();
+    void lightLed(char row, char column, char layer);
+    void drawHorLine(char startRow, char startCol, char endRow, char endCol);
+    void turnOffLed(char row, char column, char layer);
+    
+private:
+    DigitalOut *lastLedLit;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ledValues.h	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,25 @@
+#define P000 PinName(p5)
+#define P001 PinName(p6)
+#define P002 PinName(p7)
+#define P003 PinName(p8)
+#define P004 PinName(p9)
+#define P010 PinName(p10)
+#define P011 PinName(p11)
+#define P012 PinName(p12)
+#define P013 PinName(p13)
+#define P014 PinName(p14)
+#define P020 PinName(p15)
+#define P021 PinName(p16)
+#define P022 PinName(p17)
+#define P023 PinName(p18)
+#define P024 PinName(p19)
+#define P030 PinName(p20)
+#define P031 PinName(p21)
+#define P032 PinName(p22)
+#define P033 PinName(p23)
+#define P034 PinName(p24)
+#define P040 PinName(p25)
+#define P041 PinName(p26)
+#define P042 PinName(p27)
+#define P043 PinName(p28)
+#define P044 PinName(p29)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,13 @@
+#include "mbed.h"
+#include "ledCube.h"
+#include "snake.h"
+
+int main()
+{
+    ledCube cube;
+    snake mySnake(0, 0);
+    
+    while(1) {
+        
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snake.cpp	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,51 @@
+#include "snake.h"
+#include "bodyPiece.h"
+#include <list>
+
+snake::snake(char startRow, char startCol){
+    bodyPiece head = bodyPiece();
+    head.currRow = startRow;
+    head.currCol = startCol;
+    snakeBody.push_back(head);
+}
+
+void snake::move(char newHeadRow, char newHeadCol){
+    
+    // Variables to hold the bodyPiece's X and Y positions on the grid
+    char prevRow, prevCol, tempRow, tempCol;
+
+    // Store the snake's head into the iterator
+    std::list<bodyPiece>::iterator it=snakeBody.begin();
+    // Save the head's current Row and Col position
+    prevRow = (*it).currRow;
+    prevCol = (*it).currCol; 
+    // Update the head to the new Row and Col
+    (*it).currRow = newHeadRow;
+    (*it).currCol = newHeadCol;
+    // Check to see if we are at the tail with this while loop
+    while(it != snakeBody.end()){
+        // Move to the next bodyPiece
+        it++;
+        // Store the previous piece's location in the current piece
+        tempRow = (*it).currRow;
+        (*it).currRow = prevRow;
+        tempCol = (*it).currCol;
+        (*it).currCol = prevCol;
+        prevRow = tempRow;
+        prevCol = tempCol;
+    }
+}
+
+void snake::addPiece(){
+    snakeBody.push_back(bodyPiece());
+}
+
+food::food(char row, char col){
+    currRow = row;
+    currCol = col;
+}
+
+void food::moveFood(char row, char col){
+    currRow = row;
+    currCol = col;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snake.h	Wed Oct 09 16:23:20 2013 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+#include "bodyPiece.h"
+#include <list>
+
+
+typedef enum {
+    Up,Down,Left,Right
+} Direction;
+
+class snake
+{
+public:
+    snake(char startRow, char startCol);
+    void move(char newHeadRow, char newHeadCol);
+    int movementSpeed;
+    Direction movementDirection;
+    void addPiece();
+
+private:
+    std::list<bodyPiece> snakeBody;
+    int bodySize;
+};
+
+class food
+{
+public:
+    char currRow, currCol;
+    food(char row, char col);
+    void moveFood(char row, char col);
+};