Completed Snake Program

Dependencies:   N5110 PinDetect PowerControl mbed

Fork of DocTest by Craig Evans

Files at this revision

API Documentation at this revision

Comitter:
MBirney
Date:
Tue Apr 14 15:34:37 2015 +0000
Parent:
3:478b090b7e1b
Child:
5:1bfc306466db
Commit message:
Snake moves with joystick as expected(next stage to add food)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Apr 09 13:10:14 2015 +0000
+++ b/main.cpp	Tue Apr 14 15:34:37 2015 +0000
@@ -1,50 +1,76 @@
 #include "mbed.h"
 #include "N5110.h"
+#include <vector> 
+#include <cstddef>      // std::size_t
+#include <valarray>  
+#include <iostream>
 
-#include "PinDetect.h"
+//#include "PinDetect.h"
 
 // change this to alter tolerance of joystick direction
-#define DIRECTION_TOLERANCE 0.2
-//    VCC,SCE,RST,D/C,MOSI,SCLK,LED
+#define DIRECTION_TOLERANCE 0.1
+//        VCC,SCE,RST,D/C,MOSI,SCLK,LED
 N5110 lcd(p7,p8,p9,p10,p11,p13,p21);
-DigitalOut led2(LED2);
-InterruptIn pb(p16);
+BusOut myleds(LED1,LED2,LED3,LED4);
+InterruptIn startButton(p16);
+InterruptIn resetButton(p17);
+
 // connections for joystick
 DigitalIn button(p18);
 AnalogIn xPot(p19);
 AnalogIn yPot(p20);
+Serial serial(USBTX,USBRX);
+int snake[84][47];
+bool gamePlaying =0;
 
-bool gamePlaying =0;
+vector<int> snakeX (5);
+vector<int> snakeY (5,27);
+
+
+
+
+//snakeX[4]=21;
+
+//int snakeX[4]={21,22,23,24}; //5 pixels start position 21 22 23 24 25
+//int snakeY[4]={27,27,27,27}; //5 pixels y position     27 27 27 27 27 
+
+
+void startingSnake(){
+    
+    for (int i=0; i<5; i++){
+        lcd.setPixel(snakeX[i],snakeY[i]);
+       // lcd.drawRect(snakeX[i],snakeY[i],1,1,1);
+    }
+    }
+    
+
 
 // timer to regularly read the joystick
 Ticker pollJoystick;
-/*pin.mode( PullDown );
-    pin.attach_asserted( &keyPressed );
-    pin.attach_deasserted( &keyReleased );
-      pin.attach_asserted_held( &keyPressedHeld );
-      pin.attach_deasserted_held( &keyReleasedHeld );
-    pin.setSampleFrequency();*/
+
+Ticker startGame;
 
 
-/*
-void keyPressed( void ) {
-joystick.direction =DOWN;
-led2 = 1;
-}
+void Boundary(){
 
-void keyReleased( void ) {
-joystick.direction =UNKNOWN;
-led2 = 0;
+    
+    for(int x = 0; x< 84; x++) {
+        lcd.setPixel(x,0);
+    }
+    for(int x = 0; x< 84; x++) {
+        lcd.setPixel(x,47);
+    }
+    for(int y = 0; y< 48; y++) {
+        lcd.setPixel(0,y);
+    }
+    for(int y = 0; y< 48; y++) {
+        lcd.setPixel(83,y);
+    }
+   
+    lcd.refresh();
+    
+
 }
-
-void keyPressedHeld( void ) {
-led2 = 1;
-}
-
-void keyReleasedHeld( void) {
-}*/
-
-
 enum Difficulty {
     EASY,
     MEDIUM,
@@ -55,7 +81,6 @@
 
 Difficulty currentDifficulty=EASY;
 
-
 // create enumerated type (0,1,2,3 etc. for direction)
 // could be extended for diagonals etc.
 enum DirectionName {
@@ -67,8 +92,6 @@
     UNKNOWN
 };
 
-
-
 // struct for Joystick
 typedef struct JoyStick Joystick;
 struct JoyStick {
@@ -83,6 +106,7 @@
 Joystick joystick;
 
 
+DirectionName previousDirection =RIGHT;
 
 // read default positions of the joystick to calibrate later readings
 void calibrateJoystick()
@@ -117,6 +141,106 @@
     }
 }
 
+
+void updateSnakeArray(){
+        
+        if (joystick.direction==LEFT && previousDirection==RIGHT){
+            joystick.direction=RIGHT;
+            }
+            
+        if (joystick.direction==RIGHT && previousDirection==LEFT){
+            joystick.direction=LEFT;
+            }
+            
+            if (joystick.direction==UP && previousDirection==DOWN){
+            joystick.direction=DOWN;
+            }
+            
+            if (joystick.direction==DOWN && previousDirection==UP){
+            joystick.direction=UP;
+            }
+            
+             if (joystick.direction==UNKNOWN || joystick.direction==CENTRE){
+                 joystick.direction= previousDirection;
+            }
+        
+    
+       lcd.clearPixel(snakeX[0],snakeY[0]);//delete tail 
+    //  lcd.drawRect(snakeX[0],snakeY[0],1,1,2);
+        
+             for (int i =0; i<snakeX.size(); i++){   // shift elements 
+                snakeX[i]=snakeX[i + 1]; // apart from head 
+                snakeY[i]=snakeY[i+ 1];
+                } 
+                
+                 switch(joystick.direction){
+                      
+                       case UP:snakeX[4]=snakeX[3];
+                    snakeY[4]=snakeY[3]-1;
+                    break;
+                    
+                      case DOWN:
+                    snakeX[4]=snakeX[3];
+                    snakeY[4]=snakeY[3]+1;
+                    break;
+                    
+                      case LEFT:
+                    snakeX[4]=snakeX[3]-1;
+                    snakeY[4]=snakeY[3];
+                    break;
+                    
+                      case RIGHT:
+                    snakeX[4]=snakeX[3]+1;
+                    snakeY[4]=snakeY[3];
+                    break;
+                    
+                    case CENTRE:
+                    snakeX[4]=snakeX[3]+1;
+                    snakeY[4]=snakeY[3];
+                    break;
+                     case UNKNOWN:
+                    snakeX[4]=snakeX[3]+1;
+                    snakeY[4]=snakeY[3];
+                    break;
+                        
+                    
+                    }
+                   
+                 /* snakeX[0]=snakeX[1];
+                snakeX[1]=snakeX[2];
+                snakeX[2]=snakeX[3];
+                snakeX[3]=snakeX[4];
+                snakeY[0]=snakeY[1];
+                snakeY[1]=snakeY[2];
+                snakeY[2]=snakeY[3];
+                snakeY[3]=snakeY[4];
+                
+                
+               */
+                  
+}
+
+void checkForCollision(){
+    
+    if (snakeX.back()==0 || snakeX.back()==83 || snakeY.back()==0 ||snakeY.back()==47){
+        startGame.detach();
+        lcd.clear();
+        
+        
+        
+        }
+    
+    
+    }
+void startButtonPressed(){
+    gamePlaying=1;
+    myleds=15;
+    }
+    
+    void resetButtonPressed(){
+    gamePlaying=0;
+    myleds=0;
+    }
 void displaySplash()
 {
     // these are default settings so not strictly needed
@@ -190,81 +314,132 @@
     lcd.drawCircle(10,43,2,1);
     gameSpeed=1.0/8;
 }
+void drawSnake() {  
+ 
+    for(int x =10; x<15; x++) {
+        lcd.setPixel(x,25);
+       // lcd.drawRect(x,25,2,2,1);
+        snake[x][25]=1;
+    }
+}
+
+
+void checkSelectedDifficulty() {
+
+    switch(currentDifficulty) {
+        case EASY:
+
+            switch (joystick.direction) {
+
+                case UP:
+                    hardSelected();
+                    break;
+
+
+                case DOWN:
+                    mediumSelected();
+                    break;
+            }
+            break;
 
 
+        case MEDIUM:
+
+            switch (joystick.direction) {
+
+                case UP:
+                    easySelected();
+                    break;
+
+
+                case DOWN:
+                    hardSelected();
+                    break;
+            }
+
+
+
+            break;
+        case HARD:
+            switch (joystick.direction) {
+
+                case UP:
+                    mediumSelected();
+                    break;
+
+
+                case DOWN:
+                    easySelected();
+                    break;
+            }
+            break;
+    }
+    wait(0.2);
+}
+
+int updateGameFlag=0;
+void updateGameISR(){
+    updateGameFlag=1;
+    }
 
 int main()
 {
 
     // first need to initialise display
     lcd.init();
-    // pb.rise(&flip);
+    
     displaySplash();
-    wait(1);
+    wait(4);
     easySelected();
     joystick.direction=UNKNOWN;
     calibrateJoystick();  // get centred values of joystick
-    pollJoystick.attach(&updateJoystick,1.0/5.0);  // read joystick 10 times per second
-
-
-   
-        while(1) {
-            
-             if (gamePlaying==0) {
-
-
-            switch(currentDifficulty) {
-                case EASY:
-
-                    switch (joystick.direction) {
-
-                        case UP:
-                            hardSelected();
-                            break;
-
-
-                        case DOWN:
-                            mediumSelected();
-                            break;
-                    }
-                    break;
-
-
-                case MEDIUM:
-
-                    switch (joystick.direction) {
-
-                        case UP:
-                            easySelected();
-                            break;
-
-
-                        case DOWN:
-                            hardSelected();
-                            break;
-                    }
-
-
-
-                    break;
-                case HARD:
-                    switch (joystick.direction) {
-
-                        case UP:
-                            mediumSelected();
-                            break;
-
-
-                        case DOWN:
-                            easySelected();
-                            break;
-                    }
-                    break;
-                    wait(0.1);
-            }
-
-        }
-
+    pollJoystick.attach(&updateJoystick,1.0/5.0); 
+    startGame.attach(&updateGameISR,gameSpeed); // read joystick 10 times per second
+    startButton.mode(PullUp);
+    startButton.rise(&startButtonPressed);
+    resetButton.rise(&resetButtonPressed);
+  //  startGame.attach(&startButtonPressed,10);
+    
+    while(1) {
+        if (gamePlaying==0){
+        checkSelectedDifficulty();
     }
+    else if (gamePlaying==1){
+     lcd.clear();
+     lcd.normalMode();      // normal colour mode
+     Boundary();
+     snakeX[0]=21;
+    snakeX[1]=22;
+    snakeX[2]=23;
+    snakeX[3]=24;
+    snakeX[4]=25;
+ pollJoystick.detach();
+    startGame.attach(&updateGameISR,0.1);
+     startingSnake();
+         lcd.refresh();
+     //init game start time back food
+     while (1){
+       if(updateGameFlag==1){
+         //updateJoystick();
+         updateGameFlag=0;
+         updateJoystick();
+         // lcd.clearPixel(snakeX[0],snakeY[0]);
+          // updateSnakeDirection();
+          updateSnakeArray();
+          for (int i=0;i<snakeX.size();i++){
+             // lcd.drawRect(snakeX[i],snakeY[i],1,1,1);
+             lcd.setPixel(snakeX[i],snakeY[i]);
+              }
+              lcd.refresh(); 
+              previousDirection=joystick.direction;  
+              checkForCollision();      
+    }
+                  
+         }
+ 
+         }
+     }
+     
+    
 
 }
\ No newline at end of file
--- a/main.h	Thu Apr 09 13:10:14 2015 +0000
+++ b/main.h	Tue Apr 14 15:34:37 2015 +0000
@@ -6,10 +6,35 @@
 @author Michael Birney 
 @date   April 2015
 */
-
+#include "mbed.h"
 
 /** 
 Displays a Snake Logo Splash Screen 
 */
 void displaySplash();
 
+/*pin.mode( PullDown );
+    pin.attach_asserted( &keyPressed );
+    pin.attach_deasserted( &keyReleased );
+      pin.attach_asserted_held( &keyPressedHeld );
+      pin.attach_deasserted_held( &keyReleasedHeld );
+    pin.setSampleFrequency();*/
+
+
+/*
+void keyPressed( void ) {
+joystick.direction =DOWN;
+led2 = 1;
+}
+
+void keyReleased( void ) {
+joystick.direction =UNKNOWN;
+led2 = 0;
+}
+
+void keyPressedHeld( void ) {
+led2 = 1;
+}
+
+void keyReleasedHeld( void) {
+}*/
\ No newline at end of file