Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Fri Apr 26 17:16:22 2019 +0000
Revision:
49:441c32f6603e
Parent:
43:233f93860d08
Child:
50:3cf9a94a264e
Fixed multiple issues with menu code, the show credits still needs some work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AhmedPlaymaker 33:249cf423fb18 1 #include "Stats.h"
AhmedPlaymaker 33:249cf423fb18 2
AhmedPlaymaker 33:249cf423fb18 3 Stats::Stats()
AhmedPlaymaker 33:249cf423fb18 4 {
AhmedPlaymaker 33:249cf423fb18 5
AhmedPlaymaker 33:249cf423fb18 6 }
AhmedPlaymaker 33:249cf423fb18 7
AhmedPlaymaker 33:249cf423fb18 8 Stats::~Stats()
AhmedPlaymaker 33:249cf423fb18 9 {
AhmedPlaymaker 33:249cf423fb18 10
AhmedPlaymaker 33:249cf423fb18 11 }
AhmedPlaymaker 33:249cf423fb18 12
AhmedPlaymaker 33:249cf423fb18 13 FILE *fp; // this is the file pointer
AhmedPlaymaker 33:249cf423fb18 14 //serial.baud(115200); // full-speed!
AhmedPlaymaker 33:249cf423fb18 15
AhmedPlaymaker 33:249cf423fb18 16
AhmedPlaymaker 49:441c32f6603e 17 void Stats::StatsDisplay(N5110 &lcd, Gamepad &pad)
AhmedPlaymaker 33:249cf423fb18 18 {
AhmedPlaymaker 33:249cf423fb18 19 lcd.clear();
AhmedPlaymaker 33:249cf423fb18 20 wait(0.2);
AhmedPlaymaker 33:249cf423fb18 21 pad.init();
AhmedPlaymaker 33:249cf423fb18 22 while (pad.check_event(Gamepad::BACK_PRESSED) == false) { //Change this to if button pressed.
AhmedPlaymaker 33:249cf423fb18 23 lcd.printString("Highest Level",3,1);
AhmedPlaymaker 33:249cf423fb18 24 sprintf(bufferlevel,"%d",_stored_top_level);
AhmedPlaymaker 33:249cf423fb18 25 lcd.printString(bufferlevel,38,3);
AhmedPlaymaker 33:249cf423fb18 26 lcd.refresh();
AhmedPlaymaker 33:249cf423fb18 27 }
AhmedPlaymaker 33:249cf423fb18 28 lcd.clear();
AhmedPlaymaker 33:249cf423fb18 29 pad.tone(1000.0,0.1);
AhmedPlaymaker 33:249cf423fb18 30 /////////////////////// Deleting file ////////////////////////
AhmedPlaymaker 33:249cf423fb18 31
AhmedPlaymaker 35:ce47cde57bd3 32 // comment this line out if I don't want to delete the file!
AhmedPlaymaker 33:249cf423fb18 33 //delete_file("/sd/highestlevel.txt");
AhmedPlaymaker 33:249cf423fb18 34 }
AhmedPlaymaker 33:249cf423fb18 35
AhmedPlaymaker 33:249cf423fb18 36 void Stats::write(int level, SDFileSystem &sd)
AhmedPlaymaker 33:249cf423fb18 37 {
AhmedPlaymaker 33:249cf423fb18 38 ////////////////////// writing to file while reading previous state //////////////////////////
AhmedPlaymaker 33:249cf423fb18 39
AhmedPlaymaker 33:249cf423fb18 40 // open file for writing ('w') - creates file if it doesn't exist and overwrites
AhmedPlaymaker 33:249cf423fb18 41 // if it does. If you wish to add a score onto a list, then you can
AhmedPlaymaker 33:249cf423fb18 42 // append instead 'a'.
AhmedPlaymaker 33:249cf423fb18 43
AhmedPlaymaker 33:249cf423fb18 44 Stats::read(sd);
AhmedPlaymaker 33:249cf423fb18 45 fp = fopen("/sd/highestlevel.txt", "w");
AhmedPlaymaker 33:249cf423fb18 46 _current_level = level;
AhmedPlaymaker 33:249cf423fb18 47 if(_current_level == 0) {
AhmedPlaymaker 33:249cf423fb18 48 _current_level = 1;
AhmedPlaymaker 33:249cf423fb18 49 }
AhmedPlaymaker 33:249cf423fb18 50 if (_stored_top_level < _current_level) {
AhmedPlaymaker 33:249cf423fb18 51 _top_level = _current_level;
AhmedPlaymaker 33:249cf423fb18 52 }
AhmedPlaymaker 33:249cf423fb18 53
AhmedPlaymaker 33:249cf423fb18 54 else {
AhmedPlaymaker 33:249cf423fb18 55 _top_level = _stored_top_level;
AhmedPlaymaker 33:249cf423fb18 56 }
AhmedPlaymaker 33:249cf423fb18 57
AhmedPlaymaker 33:249cf423fb18 58 if (fp == NULL) { // if it can't open the file then print error message
AhmedPlaymaker 33:249cf423fb18 59 //serial.printf("Error! Unable to open file!\n");
AhmedPlaymaker 33:249cf423fb18 60 } else { // opened file so can write
AhmedPlaymaker 33:249cf423fb18 61 //serial.printf("Writing to file....");
AhmedPlaymaker 33:249cf423fb18 62 fprintf(fp, "%d",_top_level); // ensure data type matches
AhmedPlaymaker 33:249cf423fb18 63 //serial.printf("Done.\n");
AhmedPlaymaker 33:249cf423fb18 64 fclose(fp); // ensure you close the file after writing
AhmedPlaymaker 33:249cf423fb18 65 }
AhmedPlaymaker 33:249cf423fb18 66
AhmedPlaymaker 33:249cf423fb18 67
AhmedPlaymaker 33:249cf423fb18 68 }
AhmedPlaymaker 33:249cf423fb18 69
AhmedPlaymaker 33:249cf423fb18 70 void Stats::read(SDFileSystem &sd)
AhmedPlaymaker 33:249cf423fb18 71 {
AhmedPlaymaker 33:249cf423fb18 72 ////////////////////// reading from previous state //////////////////////////
AhmedPlaymaker 33:249cf423fb18 73
AhmedPlaymaker 33:249cf423fb18 74 // now open file for reading
AhmedPlaymaker 33:249cf423fb18 75 fp = fopen("/sd/highestlevel.txt", "r");
AhmedPlaymaker 33:249cf423fb18 76 _stored_top_level = 1; // level 1
AhmedPlaymaker 33:249cf423fb18 77
AhmedPlaymaker 33:249cf423fb18 78 if (fp == NULL) { // if it can't open the file then print error message
AhmedPlaymaker 33:249cf423fb18 79 //serial.printf("Error! Unable to open file!\n");
AhmedPlaymaker 33:249cf423fb18 80 } else { // opened file so can write
AhmedPlaymaker 33:249cf423fb18 81 fscanf(fp, "%d",&_stored_top_level); // ensure data type matches - note address operator (&)
AhmedPlaymaker 33:249cf423fb18 82 //serial.printf("Read %d from file.\n",stored_top_level);
AhmedPlaymaker 33:249cf423fb18 83 fclose(fp); // ensure you close the file after reading
AhmedPlaymaker 33:249cf423fb18 84 }
AhmedPlaymaker 33:249cf423fb18 85 }
AhmedPlaymaker 33:249cf423fb18 86
AhmedPlaymaker 33:249cf423fb18 87 void Stats::delete_file(char filename[])
AhmedPlaymaker 33:249cf423fb18 88 {
AhmedPlaymaker 33:249cf423fb18 89 //serial.printf("Deleting file '%s'...",filename);
AhmedPlaymaker 33:249cf423fb18 90 FILE *fp = fopen(filename, "r"); // try and open file
AhmedPlaymaker 33:249cf423fb18 91 if (fp != NULL) { // if it does open...
AhmedPlaymaker 33:249cf423fb18 92 fclose(fp); // close it
AhmedPlaymaker 33:249cf423fb18 93 remove(filename); // and then delete
AhmedPlaymaker 33:249cf423fb18 94 //serial.printf("Done!\n");
AhmedPlaymaker 33:249cf423fb18 95 }
AhmedPlaymaker 33:249cf423fb18 96 // if we can't open it, it doesn't exist and so we can't delete it
AhmedPlaymaker 33:249cf423fb18 97 }