Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
43:233f93860d08
Parent:
35:ce47cde57bd3
Child:
49:441c32f6603e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MenuClasses/Stats/Stats.cpp	Thu Apr 25 18:12:17 2019 +0000
@@ -0,0 +1,98 @@
+#include "Stats.h"
+
+Stats::Stats()
+{
+    
+}
+
+Stats::~Stats()
+{
+    
+}
+
+FILE *fp; // this is the file pointer
+//serial.baud(115200);  // full-speed!
+    
+    
+void Stats::StatsDisplay(N5110 &lcd, Gamepad &pad, SDFileSystem &sd)
+{
+    lcd.clear();
+    wait(0.2);
+    Stats::read(sd);
+    pad.init();
+    while (pad.check_event(Gamepad::BACK_PRESSED) == false) {  //Change this to if button pressed.
+        lcd.printString("Highest Level",3,1);
+        sprintf(bufferlevel,"%d",_stored_top_level);
+        lcd.printString(bufferlevel,38,3);
+        lcd.refresh();
+    }
+    lcd.clear();
+    pad.tone(1000.0,0.1);
+    /////////////////////// Deleting file ////////////////////////
+
+    // comment this line out if I don't want to delete the file!
+    //delete_file("/sd/highestlevel.txt");
+}
+
+void Stats::write(int level, SDFileSystem &sd)
+{
+    //////////////////////  writing to file while reading previous state //////////////////////////
+
+    // open file for writing ('w') - creates file if it doesn't exist and overwrites
+    // if it does. If you wish to add a score onto a list, then you can
+    // append instead 'a'.
+
+    Stats::read(sd);
+    fp = fopen("/sd/highestlevel.txt", "w");
+    _current_level = level;
+    if(_current_level == 0)  {
+        _current_level = 1;
+    }
+    if (_stored_top_level < _current_level)  {
+        _top_level = _current_level;
+    }
+
+    else {
+        _top_level = _stored_top_level;
+    }
+    
+    if (fp == NULL) {  // if it can't open the file then print error message
+        //serial.printf("Error! Unable to open file!\n");
+    } else {  // opened file so can write
+        //serial.printf("Writing to file....");
+        fprintf(fp, "%d",_top_level); // ensure data type matches
+        //serial.printf("Done.\n");
+        fclose(fp);  // ensure you close the file after writing
+    }
+
+
+}
+
+void Stats::read(SDFileSystem &sd)
+{
+    ////////////////////// reading from previous state //////////////////////////
+
+    // now open file for reading
+    fp = fopen("/sd/highestlevel.txt", "r");
+    _stored_top_level = 1;  // level 1
+
+    if (fp == NULL) {  // if it can't open the file then print error message
+        //serial.printf("Error! Unable to open file!\n");
+    } else {  // opened file so can write
+        fscanf(fp, "%d",&_stored_top_level); // ensure data type matches - note address operator (&)
+        //serial.printf("Read %d from file.\n",stored_top_level);
+        fclose(fp);  // ensure you close the file after reading
+    }
+}
+
+void Stats::delete_file(char filename[])
+{
+    //serial.printf("Deleting file '%s'...",filename);
+    FILE *fp = fopen(filename, "r");  // try and open file
+    if (fp != NULL) {  // if it does open...
+        fclose(fp);    // close it
+        remove(filename);  // and then delete
+        //serial.printf("Done!\n");
+    }
+    // if we can't open it, it doesn't exist and so we can't delete it
+}
\ No newline at end of file