ELEC2645 (2016/17) / Mbed 2 deprecated Stick_Runner

Dependencies:   FXOS8700CQ Gamepad N5110 SDFileSystem mbed

Fork of Stick_Runner by Samrudh Sharma

Files at this revision

API Documentation at this revision

Comitter:
el15ss
Date:
Tue May 02 18:42:45 2017 +0000
Parent:
1:db9ff66f67c8
Child:
3:0c690f1c04d8
Commit message:
Added SD card functionality

Changed in this revision

Character/Character.cpp Show annotated file Show diff for this revision Revisions of this file
Character/Character.h Show annotated file Show diff for this revision Revisions of this file
Gems/Gems.cpp Show annotated file Show diff for this revision Revisions of this file
Gems/Gems.h Show annotated file Show diff for this revision Revisions of this file
Obstacles/Obstacles.cpp Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.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
--- a/Character/Character.cpp	Fri Apr 28 16:30:47 2017 +0000
+++ b/Character/Character.cpp	Tue May 02 18:42:45 2017 +0000
@@ -1,26 +1,18 @@
 #include "Character.h"
 
-Character::Character()
-{
 
-}
-
-Character::~Character()
-{
-
-}
 
 void Character::init()
 {
     charPosX = 42;
     charPosY = 75;
     charStatus = true;
-   // counter = 0;
+  
 }
 
 void Character::draw(N5110 &lcd)
 {
-    // draw Character in screen buffer. 
+     
    
    
      lcd.setPixel(charPosX,charPosY);
@@ -49,14 +41,6 @@
    
 }
 
-void Character::characterStatus(Vector2D p)
-{
-    if(((charPosX-5<p.x)&&(charPosX+5>p.x))&&((charPosY-5<p.y)&&(charPosY+5>p.y)))
-    {
-        
-        charStatus = false;
-    }
-}
 
 
 void Character::updateCharacter(Direction d,float mag)
@@ -140,9 +124,21 @@
     Vector2D p = {charPosX,charPosY};
     return p;    
 }
-bool Character::getCharacterStatus(){
+
+void Character::characterStatus(Vector2D p)
+{
+    if(((charPosX-5<p.x)&&(charPosX+5>p.x))&&((charPosY-5<p.y)&&(charPosY+5>p.y)))
+    {
+        
+        charStatus = false;
+    }
+}
+
+
+bool Character::getCharacterStatus()
+{
     return charStatus;
     
-    }
+}
     
     
\ No newline at end of file
--- a/Character/Character.h	Fri Apr 28 16:30:47 2017 +0000
+++ b/Character/Character.h	Tue May 02 18:42:45 2017 +0000
@@ -9,15 +9,12 @@
 {
 public:
 
-    Character();
-    ~Character();
     void init();
     void draw(N5110 &lcd);
     void updateCharacter(Direction d,float mag);
      void characterStatus(Vector2D p);
-     void characterScore(Vector2D p);
-    void add_score();
-    int get_score();
+//void characterScore(Vector2D p);
+   
     Vector2D getCharacterPos();
     bool getCharacterStatus();
 
--- a/Gems/Gems.cpp	Fri Apr 28 16:30:47 2017 +0000
+++ b/Gems/Gems.cpp	Tue May 02 18:42:45 2017 +0000
@@ -1,14 +1,5 @@
 #include "Gems.h"
 
-Gems::Gems()
-{
-
-}
-
-Gems::~Gems()
-{
-
-}
 
 void Gems::init()
 {
@@ -21,7 +12,7 @@
 
 void Gems::draw(N5110 &lcd)
 {
-    // draw Gemss in screen buffer. 
+   
    //lcd.drawRect(gemPosX-4,gemPosY-3,3,3,FILL_BLACK);
    //lcd.drawLine(gemPosX-3, gemPosY-3,gemPosX-3,gemPosY+1,1);
    
@@ -37,6 +28,20 @@
    
 }
 
+
+
+int Gems::gemScore()
+{
+     //return counter;
+}
+
+void Gems::updateGems()
+{
+   gemPosY =gemPosY+2;
+  
+}
+
+
 void Gems::gemStatus(Vector2D p)
 {
    if(((gemPosX>p.x-5)&&(gemPosX<p.x+5))&&(gemPosY>p.y))
@@ -54,17 +59,6 @@
    }
 }
 
-int Gems::gemScore()
-{
-     //return counter;
-}
-
-void Gems::updateGems()
-{
-   gemPosY =gemPosY+2;
-  
-}
-
 
 
 Vector2D Gems::getGemPos() 
--- a/Gems/Gems.h	Fri Apr 28 16:30:47 2017 +0000
+++ b/Gems/Gems.h	Tue May 02 18:42:45 2017 +0000
@@ -9,18 +9,16 @@
 {
 public:
 
-    Gems();
-    ~Gems();
     void init();
     void draw(N5110 &lcd);
     void updateGems();
     void gemStatus(Vector2D p);
     int gemScore();
-    void add_score();
-    int get_score();
+    
     Vector2D getGemPos();
+    
     bool getGemStatus();
-   // int counter;
+ 
 
 private:
  int gemPosX;
--- a/Obstacles/Obstacles.cpp	Fri Apr 28 16:30:47 2017 +0000
+++ b/Obstacles/Obstacles.cpp	Tue May 02 18:42:45 2017 +0000
@@ -12,7 +12,6 @@
 
 void Obstacles::draw(N5110 &lcd)
 {
-    // draw Obstacless in screen buffer. 
    //lcd.drawRect(obsPosX-4,obsPosY-3,3,3,FILL_BLACK);
    //lcd.drawLine(obsPosX-3, obsPosY-3,obsPosX-3,obsPosY+1,1);
    
@@ -25,6 +24,13 @@
    
 }
 
+
+void Obstacles::updateObstacle()
+{
+   obsPosY =obsPosY+1;
+  
+}
+
 void Obstacles::obstacleStatus(Vector2D p)
 {
    /* if(((p.x<obsPosX+3)&&(p.x>obsPosX-3))&&((p.y<obsPosY+3)&&(p.y>obsPosY-3))){
@@ -38,13 +44,6 @@
         }
 }
 
-void Obstacles::updateObstacle()
-{
-   obsPosY =obsPosY+1;
-  
-}
-
-
 
 Vector2D Obstacles::getObstaclePos()
 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue May 02 18:42:45 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/neilt6/code/SDFileSystem/#e4d2567200db
--- a/main.cpp	Fri Apr 28 16:30:47 2017 +0000
+++ b/main.cpp	Tue May 02 18:42:45 2017 +0000
@@ -7,12 +7,14 @@
 #include "Character.h"
 #include "Obstacles.h"
 #include "Gems.h"
-#include <iostream>
+#include "SDFileSystem.h"
+
 
 #define No_OBS 8
 #define No_GEMS 4
 
-int i,j,counter;
+int i,j,counter,highScore;
+ char score[50];
 
 //Structs
 struct UserInput {
@@ -26,6 +28,9 @@
 Obstacles obstacle[No_OBS];
 Gems gems[No_GEMS],g;
 
+SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd");
+FILE *file;
+
 
 /*            Function Prototypes                    */
 void init();
@@ -59,7 +64,7 @@
     // game loop - read input, update the game state and render the display
     while (1) {
          
-        
+        counter++;
         
         c.updateCharacter(pad.get_direction(),pad.get_mag());
         if(pad.check_event(Gamepad::BACK_PRESSED))
@@ -78,7 +83,7 @@
                 obstacle[i].init();
             }
                 c.characterStatus(obstacle[i].getObstaclePos());
-                obstacle[i].obstacleStatus(c.getCharacterPos());
+              //  obstacle[i].obstacleStatus(c.getCharacterPos());
                 
         }
         
@@ -90,10 +95,13 @@
             if(gems[j].getGemStatus() == false)
             {
                 gems[j].init();
+                
             }
             
           
         }
+        
+        //To make the obstacles and gems move along the screen 
         i =0;
         
         for(i=0;i<No_OBS;i++)
@@ -107,6 +115,7 @@
         for(j=0;j<No_GEMS;j++)
         {
                gems[j].updateGems();
+              
         }
             
         render();
@@ -117,7 +126,7 @@
 
 void init()
 {
-    // need to initialise LCD and Gamepad 
+   
     lcd.init();
     pad.init();
     c.init();
@@ -138,13 +147,20 @@
 
 void render()
 {
-    // clear screen, re-draw and refresh
+    
     lcd.clear();  
     
      if(c.getCharacterStatus())
      {  
         c.draw(lcd);
      }
+     
+      if(c.getCharacterStatus() == false)
+   {
+      over();
+   }
+   
+   
     //obstacle[i].draw(lcd);
     
     for(i=0;i<No_OBS;i++)
@@ -164,16 +180,14 @@
         
       }
       
-      else{
+      /*else{
          counter++;
-      }
+      }*/
                
     }
+      
             
-   if(c.getCharacterStatus() == false)
-   {
-      over();
-   }
+  
    
    
     lcd.refresh();
@@ -192,7 +206,7 @@
     //pad.tone(1500.0,0.5);
       
      
-    // wait flashing LEDs until start button is pressed 
+   
     while ( pad.check_event(Gamepad::START_PRESSED) == false) 
     {
         pad.leds_on();
@@ -205,21 +219,49 @@
 }
 
 void over() {
-    lcd.clear();
+    
+    pad.init();
+   // lcd.clear();
+    sd.mount();
+   
     
-    char buffer[50];
+    sprintf (score, " Score : %d",counter);
+
+    lcd.printString(score,0,2);
+    lcd.printString("GAME  OVER!!  ",0,0);  
+    //lcd.printString("     ",0,1);
     
-    sprintf (buffer, "%d",counter);
-
-    lcd.printString(buffer,0,3);
-    lcd.printString("    GAME      ",0,1);  
-    lcd.printString("  OVER!!!!!   ",0,2);
+    file = fopen("/sd/scoreFile.txt", "r");
+    if(file ==NULL)
+    {
+        file = fopen("/sd/scoreFile.txt", "w");
+        fprintf(file,"%d",counter);
+        fclose(file);
+         lcd.printString("HIGH SCORE",0,3);
+        
+    }
+    else{
+            fscanf(file,"%d", &highScore);
+            fclose(file);
+            
+            if(counter>highScore)
+            {
+                
+                file = fopen("/sd/scoreFile.txt", "w");
+        fprintf(file,"%d",counter);
+        fclose(file);
+         lcd.printString("HIGH SCORE",0,3);
+            }
+        
+        }
+        
    
     lcd.printString(" PRESS START  ",0,4);  
     
     lcd.refresh();
+    sd.unmount();
      
-    // wait flashing LEDs until start button is pressed 
+    
     while ( pad.check_event(Gamepad::START_PRESSED) == false) {
         pad.leds_on();
         //pad.tone(1000.0,0.5);
@@ -235,15 +277,70 @@
  
 }
 
+void displayHighScore()
+{
+    sd.mount();
+    lcd.clear();
+      
+   
+     file = fopen("/sd/scoreFile.txt", "r");
+     if(file ==NULL)
+    {
+        highScore = 0;
+        
+    }
+    else{
+            fscanf(file,"%d", &highScore);
+            fclose(file);
+        
+        } 
+        
+    sprintf (score, "High Score : %d",highScore);
+
+    lcd.printString(score,0,2);
+    lcd.printString(" START - reset  ",0,4); 
+    lcd.printString(" BACK - menu   ",0,5);  
+      lcd.refresh();
+      sd.unmount();
+      
+      while(1)
+      {
+          if( pad.check_event(Gamepad::START_PRESSED)) {
+             sd.mount();
+             file = fopen("/sd/scoreFile.txt", "r");
+            if(!file ==NULL)
+                {
+                    fclose(file);
+                     remove("/sd/scoreFile.txt");
+                      
+                }
+                 sd.unmount();
+                    displayHighScore();
+            
+            }
+            
+             if( pad.check_event(Gamepad::BACK_PRESSED)) {
+                        menu();
+            }
+            
+            sleep();
+          
+      }
+    
+       
+}
+
 void menu() {
     int fps =8;
      bool i = true;
     while(i == true)
     {
-    lcd.printString("     Menu       ",0,1);  
+        lcd.clear();
+    lcd.printString("     Menu       ",0,0);  
     lcd.printString("A)New Game     ",0,2);
     lcd.printString("B)Continue     ",0,3);
     lcd.printString("X)Instructions ",0,4);
+    lcd.printString("Y)High Score   ",0,5);
     
     lcd.refresh();
      
@@ -270,7 +367,14 @@
     else if( pad.check_event(Gamepad::X_PRESSED) ) {
       // pad.tone(1000.0,0.5);
       Instructions();
-         wait(100.0f/fps);
+         wait(1.0f/fps);
+       i = false;
+    }
+    
+    else if( pad.check_event(Gamepad::Y_PRESSED) ) {
+      // pad.tone(1000.0,0.5);
+      displayHighScore();
+         wait(10.0f/fps);
        i = false;
     }
     
@@ -282,10 +386,11 @@
 {
      bool i = true;
      lcd.clear();
-       lcd.printString("INSTURCTIONS:     ",0,1);  
+       lcd.printString("INSTURCTIONS:     ",0,0);  
        lcd.printString("Collect the       ",0,2);
-       lcd.printString("gems to get       ",0,3);
-       lcd.printString("points            ",0,4);
+       lcd.printString("gems and dodge    ",0,3);
+       lcd.printString("the obstacles     ",0,4);
+       lcd.printString("to get points     ",0,5);
        lcd.refresh();
        
        while(i == true){