This is a slight mod of the source code uploaded by Sam Thys - https://os.mbed.com/users/sammekevremde/code/Snake/ Please check out the original project, this fork is only a hack of some working code to fix a few playability issues. See https://youtu.be/mUqJvReB1lg for a listing

Dependencies:   C12832_lcd LM75B MMA7660 mbed

Fork of Snake by Sam Thys

Files at this revision

API Documentation at this revision

Comitter:
zaphodikus
Date:
Sun Jul 08 15:09:34 2018 +0000
Parent:
0:b4eddf41d75b
Commit message:
renamed most variables to englich names; uses accelerometer and thermometer to generate a random seed; draws the fruit as a larger box; cuts out some un-necessary draw code;

Changed in this revision

LM75B.lib Show annotated file Show diff for this revision Revisions of this file
MMA7660.lib 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Sun Jul 08 15:09:34 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/neilt6/code/LM75B/#7ac462ba84ac
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA7660.lib	Sun Jul 08 15:09:34 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Sissors/code/MMA7660/#36a163511e34
--- a/main.cpp	Mon May 13 10:47:49 2013 +0000
+++ b/main.cpp	Sun Jul 08 15:09:34 2018 +0000
@@ -1,118 +1,132 @@
+// original sources Sam Thys - https://os.mbed.com/users/sammekevremde/code/Snake/
+// modified by: zaphodikus Jul 2018
+// uses thermometer and accelerometer on the LPC7760 to generate random
+// fruit positions
 #include "mbed.h"
 #include "C12832_lcd.h"
+#include "LM75B.h"
+#include "MMA7660.h"
 
+MMA7660 MMA(p28,p27); // accelerometer
 C12832_LCD lcd;
-Ticker move;
+Ticker gameticker;
+LM75B tmp(p28,p27); // I2C Temperature Sensor
+
 //-- Variable --
-enum richting { omhoog, omlaag, links, rechts, null};
-enum gamemode { start,stop,run,pauze};
-richting huidigeRichting;
-richting vorigeRichting;
+enum direction { up, down, left, right, null};
+enum gamemode { start,stop,run,pause};
+direction currentdirection;
+direction lastdirection;
 gamemode game;
 // -- Classes --
-struct block
+struct point
 {
      int x;
      int y;
     };
 //---------------------
-struct block snakeHead;
-struct block fruit;
-int aantalFruits=0;
-//block snakeBody[5];
+struct point snakeHead;
+struct point fruit;
+int countFruit=0;
+float board_temp;
 
-block* snakeBody=new block[aantalFruits+5];//=new block[aantalFruits+5];
+point* snakeBody=new point[countFruit+5];
 // -- I/O setting --
-InterruptIn up(p12);
-InterruptIn down(p15);
-InterruptIn left (p13);
-InterruptIn right(p16);
-InterruptIn center(p14);
+InterruptIn upHandler(p12);
+InterruptIn downHandler(p15);
+InterruptIn leftHandler(p13);
+InterruptIn rightHandler(p16);
+InterruptIn centerHandler(p14);
+
+// -- Functions --
 
-// -- Functies --
-
-void setFruit()
+void placeFruit()
 {
-fruit.x = rand() % 126 +1;  
-fruit.y = rand() % 30 +1; 
+    board_temp = tmp;
+    
+    int data[3];
+    MMA.readData((int*)&data);
+    srand(data[0]*192 + data[1]*192 + data[2]*192 + board_temp*2048);
+    fruit.x = rand() % 126 +1;  
+    fruit.y = rand() % 30 +1; 
 }
 
-void richting_omhoog()
+void direction_up()
 {
-    if ( huidigeRichting!=omlaag)
-        huidigeRichting=omhoog;
+    if ( currentdirection!=down)
+        currentdirection=up;
 }
-void richting_omlaag()
+void direction_down()
 {
-    if ( huidigeRichting!=omhoog)
-        huidigeRichting=omlaag;
+    if ( currentdirection!=up)
+        currentdirection=down;
 }
-void richting_links()
+void direction_left()
 {
-    if ( huidigeRichting!=rechts)
-        huidigeRichting=links;
+    if ( currentdirection!=right)
+        currentdirection=left;
 }
-void richting_rechts()
+void direction_right()
 {
-    if ( huidigeRichting!=links)
-        huidigeRichting=rechts;
+    if ( currentdirection!=left)
+        currentdirection=right;
 }
-void center_knop()
+    
+void center_button()
 {
-        if (game==pauze)
+    if (game==pause)
+    {
+        currentdirection=lastdirection;
+        game=run;
+    }
+    else if (game==run)
+    {
+        lastdirection=currentdirection;
+        currentdirection=null;
+        game=pause;
+    }
+    else if (game==stop)
+    {
+        for (int i=0;i<=4;i++)
         {
-            huidigeRichting=vorigeRichting;
-            game=run;
-        }
-        else if (game==run)
-        {
-        vorigeRichting=huidigeRichting;
-        huidigeRichting=null;
-        game=pauze;
+            snakeBody[i].x=(snakeHead.x)-(i+1);
+            snakeBody[i].y=15;
         }
-        else if (game==stop)
-        {
-        for (int i=0;i<=4;i++)
-{
-    snakeBody[i].x=(snakeHead.x)-(i+1);
-    snakeBody[i].y=15;
+        currentdirection=null;
+        game=run;
+    }
 }
-        huidigeRichting=null;
-        game=run;
-        }
-}
-void moveSnake() 
+
+void tickHandler() 
 {
 
     if (game==run)
     {
-       if (huidigeRichting!=null)
+       if (currentdirection!=null)
         {
-         for (int j=(aantalFruits+4);j>=1;j--)
-         {
-            snakeBody[j]=snakeBody[j-1];
-         }
+            for (int j=(countFruit+4);j>=1;j--)
+            {
+                snakeBody[j]=snakeBody[j-1];
+            }
             snakeBody[0]=snakeHead;
         }
         
-        switch (huidigeRichting)
+        switch (currentdirection)
         {
-            case omhoog:
+            case up:
                 snakeHead.y+=1;
                 break;
-            case omlaag:
+            case down:
                 snakeHead.y-=1;
                 break;
-            case links:
+            case left:
                 snakeHead.x-=1;
                 break;
-            case rechts:
+            case right:
                 snakeHead.x+=1;
                 break;
             
-         }
-         
-        
+         }        
          
          if ((snakeHead.y == 0 ||snakeHead.y == 31 ||snakeHead.x == 127 ||snakeHead.x == 0) )
          {
@@ -120,115 +134,94 @@
             lcd.locate(25,5);
             lcd.printf("Game Over!!");
             lcd.locate(25,15);
-            lcd.printf("U score is %d",aantalFruits);
-            huidigeRichting=null;
+            lcd.printf("Score > %d",countFruit);
+            currentdirection=null;
             snakeHead.x=15;
             snakeHead.y=15;
             game=stop;
-            aantalFruits=0;
+            countFruit=0;
          }
          else if ((snakeHead.y == fruit.y )&& (snakeHead.x == fruit.x)  )
          {
-         aantalFruits+=1;
-      /*   
-         for (int j=0;j<=(aantalFruits+4);j++)
-         {
-            snakeBody[j+1]=snakeBody[j+1];
-         }
-         snakeBody[0]=snakeHead;
-         switch(huidigeRichting)
-         {
-            case omhoog:
-                snakeHead.y+=1;
-                break;
-            case omlaag:
-            snakeHead.y-=1;
-                break;
-            case links:
-            snakeHead.x+=1;
-                break;
-            case rechts:
-            snakeHead.x-=1;
-                break;
-         }
-         */
-         setFruit();
+            countFruit +=1;
+            placeFruit();
          }
          else
          {
              lcd.cls();
-         lcd.pixel(snakeHead.x,snakeHead.y,1);
-         lcd.copy_to_lcd();
-         for(int k=0;k<=(aantalFruits+4);k++)
-         {
-         lcd.pixel(snakeBody[k].x,snakeBody[k].y,1);
-                  
-         }
-         lcd.copy_to_lcd();
-         lcd.pixel(fruit.x,fruit.y,1);
-         lcd.copy_to_lcd();
-         lcd.rect(0,0, 127, 31, 1);
-         lcd.copy_to_lcd();
+            lcd.pixel(snakeHead.x,snakeHead.y,1);
+            lcd.copy_to_lcd();
+            for(int k=0;k<=(countFruit +4);k++)
+            {
+                lcd.pixel(snakeBody[k].x,snakeBody[k].y,1);
+            }
+            lcd.pixel(fruit.x-1,fruit.y-1, 1);
+            lcd.pixel(fruit.x-1,fruit.y, 1);
+            lcd.pixel(fruit.x-1,fruit.y+1, 1);
+            lcd.pixel(fruit.x,fruit.y-1, 1);
+            lcd.pixel(fruit.x,fruit.y, 1);
+            lcd.pixel(fruit.x,fruit.y+1, 1);
+            lcd.pixel(fruit.x+1,fruit.y-1, 1);
+            lcd.pixel(fruit.x+1,fruit.y, 1);
+            lcd.pixel(fruit.x+1,fruit.y+1, 1);
+            lcd.rect(0,0, 127, 31, 1);
+            lcd.copy_to_lcd();
          }
          
-          for (int a=0;a<=(aantalFruits+4);a++)
-         {
+        for (int a=0;a<=(countFruit +4);a++)
+        {
             if ( snakeBody[a].x==snakeHead.x && snakeBody[a].y==snakeHead.y)
                {
                     lcd.cls();
                     lcd.locate(25,5);
                     lcd.printf("Game Over!!");
                     lcd.locate(25,15);
-                    lcd.printf("U score is %d",aantalFruits);
-                    huidigeRichting=null;
+                    lcd.printf("U score is %d",countFruit
+            );
+                    currentdirection=null;
                     snakeHead.x=15;
                     snakeHead.y=15;
                     game=stop;
-                    aantalFruits=0;
+                    countFruit
+                =0;
                     
                 }
-         }
-    }
-    else if (game==pauze)
-    {
-            lcd.cls();
-            lcd.locate(30,15);
-            lcd.printf("Pauze");
+        }
     }
-
-    
+    else if (game==pause)
+    {
+        lcd.cls();
+        lcd.locate(30,15);
+        lcd.printf("pause");
+    }
 }
-//template <class T>
-
-
-
 
 
 //--------------------------------
-
 int main()
 {
-game=run;
-lcd.rect(0, 0, 127, 31, 1);
-lcd.copy_to_lcd();
-huidigeRichting=null;
-snakeHead.x=15;
-snakeHead.y=15;
-for (int i=0;i<=4;i++)
-{
-    snakeBody[i].x=(snakeHead.x)-(i+1);
-    snakeBody[i].y=15;
-}
-setFruit();
-move.attach(&moveSnake, 0.1);
-up.rise(&richting_omhoog);
-down.rise(&richting_omlaag);
-left.rise(&richting_links);
-right.rise(&richting_rechts);
-center.rise(&center_knop);
+    game=run;
+    lcd.rect(0, 0, 127, 31, 1);
+    lcd.copy_to_lcd();
+    currentdirection=null;
+    snakeHead.x=15;
+    snakeHead.y=15;
+    for (int i=0;i<=4;i++)
+    {
+        snakeBody[i].x=(snakeHead.x)-(i+1);
+        snakeBody[i].y=15;
+    }
+    placeFruit();
+    gameticker.attach(&tickHandler, 0.1);
+    // set interrupt handlers
+    upHandler.rise(&direction_up);
+    downHandler.rise(&direction_down);
+    leftHandler.rise(&direction_left);
+    rightHandler.rise(&direction_right);
+    centerHandler.rise(&center_button);
 
-while (1)
-{
+    while (1)
+    {
 
-}
+    }
 }
\ No newline at end of file