Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Stats/Stats.cpp@33:249cf423fb18, 2019-04-17 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Wed Apr 17 07:47:06 2019 +0000
- Revision:
- 33:249cf423fb18
- Child:
- 35:ce47cde57bd3
Added Stats Class and now i can store the highest level.
Who changed what in which revision?
User | Revision | Line number | New 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 | 33:249cf423fb18 | 17 | void Stats::StatsDisplay(N5110 &lcd, Gamepad &pad, SDFileSystem &sd) |
AhmedPlaymaker | 33:249cf423fb18 | 18 | { |
AhmedPlaymaker | 33:249cf423fb18 | 19 | lcd.clear(); |
AhmedPlaymaker | 33:249cf423fb18 | 20 | wait(0.2); |
AhmedPlaymaker | 33:249cf423fb18 | 21 | Stats::read(sd); |
AhmedPlaymaker | 33:249cf423fb18 | 22 | pad.init(); |
AhmedPlaymaker | 33:249cf423fb18 | 23 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { //Change this to if button pressed. |
AhmedPlaymaker | 33:249cf423fb18 | 24 | lcd.printString("Highest Level",3,1); |
AhmedPlaymaker | 33:249cf423fb18 | 25 | sprintf(bufferlevel,"%d",_stored_top_level); |
AhmedPlaymaker | 33:249cf423fb18 | 26 | lcd.printString(bufferlevel,38,3); |
AhmedPlaymaker | 33:249cf423fb18 | 27 | lcd.refresh(); |
AhmedPlaymaker | 33:249cf423fb18 | 28 | } |
AhmedPlaymaker | 33:249cf423fb18 | 29 | lcd.clear(); |
AhmedPlaymaker | 33:249cf423fb18 | 30 | pad.tone(1000.0,0.1); |
AhmedPlaymaker | 33:249cf423fb18 | 31 | /////////////////////// Deleting file //////////////////////// |
AhmedPlaymaker | 33:249cf423fb18 | 32 | |
AhmedPlaymaker | 33:249cf423fb18 | 33 | // comment this line out if you don't want to delete the file! |
AhmedPlaymaker | 33:249cf423fb18 | 34 | //delete_file("/sd/highestlevel.txt"); |
AhmedPlaymaker | 33:249cf423fb18 | 35 | } |
AhmedPlaymaker | 33:249cf423fb18 | 36 | |
AhmedPlaymaker | 33:249cf423fb18 | 37 | void Stats::write(int level, SDFileSystem &sd) |
AhmedPlaymaker | 33:249cf423fb18 | 38 | { |
AhmedPlaymaker | 33:249cf423fb18 | 39 | ////////////////////// writing to file while reading previous state ////////////////////////// |
AhmedPlaymaker | 33:249cf423fb18 | 40 | |
AhmedPlaymaker | 33:249cf423fb18 | 41 | // open file for writing ('w') - creates file if it doesn't exist and overwrites |
AhmedPlaymaker | 33:249cf423fb18 | 42 | // if it does. If you wish to add a score onto a list, then you can |
AhmedPlaymaker | 33:249cf423fb18 | 43 | // append instead 'a'. |
AhmedPlaymaker | 33:249cf423fb18 | 44 | |
AhmedPlaymaker | 33:249cf423fb18 | 45 | Stats::read(sd); |
AhmedPlaymaker | 33:249cf423fb18 | 46 | fp = fopen("/sd/highestlevel.txt", "w"); |
AhmedPlaymaker | 33:249cf423fb18 | 47 | _current_level = level; |
AhmedPlaymaker | 33:249cf423fb18 | 48 | if(_current_level == 0) { |
AhmedPlaymaker | 33:249cf423fb18 | 49 | _current_level = 1; |
AhmedPlaymaker | 33:249cf423fb18 | 50 | } |
AhmedPlaymaker | 33:249cf423fb18 | 51 | if (_stored_top_level < _current_level) { |
AhmedPlaymaker | 33:249cf423fb18 | 52 | _top_level = _current_level; |
AhmedPlaymaker | 33:249cf423fb18 | 53 | } |
AhmedPlaymaker | 33:249cf423fb18 | 54 | |
AhmedPlaymaker | 33:249cf423fb18 | 55 | else { |
AhmedPlaymaker | 33:249cf423fb18 | 56 | _top_level = _stored_top_level; |
AhmedPlaymaker | 33:249cf423fb18 | 57 | } |
AhmedPlaymaker | 33:249cf423fb18 | 58 | |
AhmedPlaymaker | 33:249cf423fb18 | 59 | if (fp == NULL) { // if it can't open the file then print error message |
AhmedPlaymaker | 33:249cf423fb18 | 60 | //serial.printf("Error! Unable to open file!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 61 | } else { // opened file so can write |
AhmedPlaymaker | 33:249cf423fb18 | 62 | //serial.printf("Writing to file...."); |
AhmedPlaymaker | 33:249cf423fb18 | 63 | fprintf(fp, "%d",_top_level); // ensure data type matches |
AhmedPlaymaker | 33:249cf423fb18 | 64 | //serial.printf("Done.\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 65 | fclose(fp); // ensure you close the file after writing |
AhmedPlaymaker | 33:249cf423fb18 | 66 | } |
AhmedPlaymaker | 33:249cf423fb18 | 67 | |
AhmedPlaymaker | 33:249cf423fb18 | 68 | |
AhmedPlaymaker | 33:249cf423fb18 | 69 | } |
AhmedPlaymaker | 33:249cf423fb18 | 70 | |
AhmedPlaymaker | 33:249cf423fb18 | 71 | void Stats::read(SDFileSystem &sd) |
AhmedPlaymaker | 33:249cf423fb18 | 72 | { |
AhmedPlaymaker | 33:249cf423fb18 | 73 | ////////////////////// reading from previous state ////////////////////////// |
AhmedPlaymaker | 33:249cf423fb18 | 74 | |
AhmedPlaymaker | 33:249cf423fb18 | 75 | // now open file for reading |
AhmedPlaymaker | 33:249cf423fb18 | 76 | fp = fopen("/sd/highestlevel.txt", "r"); |
AhmedPlaymaker | 33:249cf423fb18 | 77 | _stored_top_level = 1; // level 1 |
AhmedPlaymaker | 33:249cf423fb18 | 78 | |
AhmedPlaymaker | 33:249cf423fb18 | 79 | if (fp == NULL) { // if it can't open the file then print error message |
AhmedPlaymaker | 33:249cf423fb18 | 80 | //serial.printf("Error! Unable to open file!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 81 | } else { // opened file so can write |
AhmedPlaymaker | 33:249cf423fb18 | 82 | fscanf(fp, "%d",&_stored_top_level); // ensure data type matches - note address operator (&) |
AhmedPlaymaker | 33:249cf423fb18 | 83 | //serial.printf("Read %d from file.\n",stored_top_level); |
AhmedPlaymaker | 33:249cf423fb18 | 84 | fclose(fp); // ensure you close the file after reading |
AhmedPlaymaker | 33:249cf423fb18 | 85 | } |
AhmedPlaymaker | 33:249cf423fb18 | 86 | } |
AhmedPlaymaker | 33:249cf423fb18 | 87 | |
AhmedPlaymaker | 33:249cf423fb18 | 88 | void Stats::delete_file(char filename[]) |
AhmedPlaymaker | 33:249cf423fb18 | 89 | { |
AhmedPlaymaker | 33:249cf423fb18 | 90 | //serial.printf("Deleting file '%s'...",filename); |
AhmedPlaymaker | 33:249cf423fb18 | 91 | FILE *fp = fopen(filename, "r"); // try and open file |
AhmedPlaymaker | 33:249cf423fb18 | 92 | if (fp != NULL) { // if it does open... |
AhmedPlaymaker | 33:249cf423fb18 | 93 | fclose(fp); // close it |
AhmedPlaymaker | 33:249cf423fb18 | 94 | remove(filename); // and then delete |
AhmedPlaymaker | 33:249cf423fb18 | 95 | //serial.printf("Done!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 96 | } |
AhmedPlaymaker | 33:249cf423fb18 | 97 | // if we can't open it, it doesn't exist and so we can't delete it |
AhmedPlaymaker | 33:249cf423fb18 | 98 | } |