Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
MenuClasses/Stats/Stats.cpp@62:ebf6ecf8a6d5, 2019-04-30 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Tue Apr 30 14:53:38 2019 +0000
- Revision:
- 62:ebf6ecf8a6d5
- Parent:
- 61:f3c402bc2ad0
- Child:
- 83:329da564799a
Cleaned the code style of the Block Side Collision
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 | 62:ebf6ecf8a6d5 | 5 | |
AhmedPlaymaker | 33:249cf423fb18 | 6 | } |
AhmedPlaymaker | 33:249cf423fb18 | 7 | |
AhmedPlaymaker | 33:249cf423fb18 | 8 | Stats::~Stats() |
AhmedPlaymaker | 33:249cf423fb18 | 9 | { |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 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 | 62:ebf6ecf8a6d5 | 15 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 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 | 61:f3c402bc2ad0 | 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 (_stored_top_level < _current_level) { |
AhmedPlaymaker | 33:249cf423fb18 | 48 | _top_level = _current_level; |
AhmedPlaymaker | 33:249cf423fb18 | 49 | } |
AhmedPlaymaker | 33:249cf423fb18 | 50 | |
AhmedPlaymaker | 33:249cf423fb18 | 51 | else { |
AhmedPlaymaker | 33:249cf423fb18 | 52 | _top_level = _stored_top_level; |
AhmedPlaymaker | 33:249cf423fb18 | 53 | } |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 54 | |
AhmedPlaymaker | 33:249cf423fb18 | 55 | if (fp == NULL) { // if it can't open the file then print error message |
AhmedPlaymaker | 33:249cf423fb18 | 56 | //serial.printf("Error! Unable to open file!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 57 | } else { // opened file so can write |
AhmedPlaymaker | 33:249cf423fb18 | 58 | //serial.printf("Writing to file...."); |
AhmedPlaymaker | 33:249cf423fb18 | 59 | fprintf(fp, "%d",_top_level); // ensure data type matches |
AhmedPlaymaker | 33:249cf423fb18 | 60 | //serial.printf("Done.\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 61 | fclose(fp); // ensure you close the file after writing |
AhmedPlaymaker | 33:249cf423fb18 | 62 | } |
AhmedPlaymaker | 33:249cf423fb18 | 63 | |
AhmedPlaymaker | 33:249cf423fb18 | 64 | |
AhmedPlaymaker | 33:249cf423fb18 | 65 | } |
AhmedPlaymaker | 33:249cf423fb18 | 66 | |
AhmedPlaymaker | 33:249cf423fb18 | 67 | void Stats::read(SDFileSystem &sd) |
AhmedPlaymaker | 33:249cf423fb18 | 68 | { |
AhmedPlaymaker | 33:249cf423fb18 | 69 | ////////////////////// reading from previous state ////////////////////////// |
AhmedPlaymaker | 33:249cf423fb18 | 70 | |
AhmedPlaymaker | 33:249cf423fb18 | 71 | // now open file for reading |
AhmedPlaymaker | 33:249cf423fb18 | 72 | fp = fopen("/sd/highestlevel.txt", "r"); |
AhmedPlaymaker | 50:3cf9a94a264e | 73 | _stored_top_level = 0; // level 0 |
AhmedPlaymaker | 33:249cf423fb18 | 74 | |
AhmedPlaymaker | 33:249cf423fb18 | 75 | if (fp == NULL) { // if it can't open the file then print error message |
AhmedPlaymaker | 33:249cf423fb18 | 76 | //serial.printf("Error! Unable to open file!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 77 | } else { // opened file so can write |
AhmedPlaymaker | 33:249cf423fb18 | 78 | fscanf(fp, "%d",&_stored_top_level); // ensure data type matches - note address operator (&) |
AhmedPlaymaker | 33:249cf423fb18 | 79 | //serial.printf("Read %d from file.\n",stored_top_level); |
AhmedPlaymaker | 33:249cf423fb18 | 80 | fclose(fp); // ensure you close the file after reading |
AhmedPlaymaker | 33:249cf423fb18 | 81 | } |
AhmedPlaymaker | 33:249cf423fb18 | 82 | } |
AhmedPlaymaker | 33:249cf423fb18 | 83 | |
AhmedPlaymaker | 33:249cf423fb18 | 84 | void Stats::delete_file(char filename[]) |
AhmedPlaymaker | 33:249cf423fb18 | 85 | { |
AhmedPlaymaker | 33:249cf423fb18 | 86 | //serial.printf("Deleting file '%s'...",filename); |
AhmedPlaymaker | 33:249cf423fb18 | 87 | FILE *fp = fopen(filename, "r"); // try and open file |
AhmedPlaymaker | 33:249cf423fb18 | 88 | if (fp != NULL) { // if it does open... |
AhmedPlaymaker | 33:249cf423fb18 | 89 | fclose(fp); // close it |
AhmedPlaymaker | 33:249cf423fb18 | 90 | remove(filename); // and then delete |
AhmedPlaymaker | 33:249cf423fb18 | 91 | //serial.printf("Done!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 92 | } |
AhmedPlaymaker | 33:249cf423fb18 | 93 | // if we can't open it, it doesn't exist and so we can't delete it |
AhmedPlaymaker | 33:249cf423fb18 | 94 | } |