Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
MenuClasses/Stats/Stats.cpp@104:17040265b7b4, 2019-05-09 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu May 09 14:52:19 2019 +0000
- Revision:
- 104:17040265b7b4
- Parent:
- 90:741120c09784
Final Submission. I have read and agreed with Statement of Academic Integrity.
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 | 86:fafb72e08195 | 16 | int LevelBoundary[11][25] = { |
AhmedPlaymaker | 86:fafb72e08195 | 17 | {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, |
AhmedPlaymaker | 86:fafb72e08195 | 18 | {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0}, |
AhmedPlaymaker | 86:fafb72e08195 | 19 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
AhmedPlaymaker | 86:fafb72e08195 | 20 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
AhmedPlaymaker | 86:fafb72e08195 | 21 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
AhmedPlaymaker | 86:fafb72e08195 | 22 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
AhmedPlaymaker | 86:fafb72e08195 | 23 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
AhmedPlaymaker | 86:fafb72e08195 | 24 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
AhmedPlaymaker | 86:fafb72e08195 | 25 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
AhmedPlaymaker | 86:fafb72e08195 | 26 | {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0}, |
AhmedPlaymaker | 86:fafb72e08195 | 27 | {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, |
AhmedPlaymaker | 86:fafb72e08195 | 28 | |
AhmedPlaymaker | 86:fafb72e08195 | 29 | }; |
AhmedPlaymaker | 86:fafb72e08195 | 30 | |
AhmedPlaymaker | 83:329da564799a | 31 | void Stats::init(N5110 *lcd, Gamepad *pad) |
AhmedPlaymaker | 83:329da564799a | 32 | { |
AhmedPlaymaker | 83:329da564799a | 33 | _lcd = lcd; |
AhmedPlaymaker | 83:329da564799a | 34 | _pad = pad; |
AhmedPlaymaker | 83:329da564799a | 35 | } |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 36 | |
AhmedPlaymaker | 83:329da564799a | 37 | void Stats::StatsDisplay() |
AhmedPlaymaker | 33:249cf423fb18 | 38 | { |
AhmedPlaymaker | 83:329da564799a | 39 | _lcd->clear(); |
AhmedPlaymaker | 33:249cf423fb18 | 40 | wait(0.2); |
AhmedPlaymaker | 61:f3c402bc2ad0 | 41 | //pad.init(); |
AhmedPlaymaker | 83:329da564799a | 42 | while (_pad->check_event(Gamepad::BACK_PRESSED) == false) { //Change this to if button pressed. |
AhmedPlaymaker | 86:fafb72e08195 | 43 | _lcd->printString("Highest Level",3,0); |
AhmedPlaymaker | 86:fafb72e08195 | 44 | _lcd->printString("Reached~",18,1); |
AhmedPlaymaker | 90:741120c09784 | 45 | _lcd->drawSprite(32,22,11,25,(int *)LevelBoundary); //Function used to draw the boundary sprite. |
AhmedPlaymaker | 90:741120c09784 | 46 | sprintf(_bufferlevel,"%d",_stored_top_level); |
AhmedPlaymaker | 90:741120c09784 | 47 | _lcd->printString(_bufferlevel,38,3); //important to draw after boundary because it needs to overlap the sprite from the centre to be seen. |
AhmedPlaymaker | 83:329da564799a | 48 | _lcd->refresh(); |
AhmedPlaymaker | 33:249cf423fb18 | 49 | } |
AhmedPlaymaker | 83:329da564799a | 50 | _lcd->clear(); |
AhmedPlaymaker | 83:329da564799a | 51 | _pad->tone(1000.0,0.1); |
AhmedPlaymaker | 33:249cf423fb18 | 52 | /////////////////////// Deleting file //////////////////////// |
AhmedPlaymaker | 33:249cf423fb18 | 53 | |
AhmedPlaymaker | 35:ce47cde57bd3 | 54 | // comment this line out if I don't want to delete the file! |
AhmedPlaymaker | 33:249cf423fb18 | 55 | //delete_file("/sd/highestlevel.txt"); |
AhmedPlaymaker | 33:249cf423fb18 | 56 | } |
AhmedPlaymaker | 33:249cf423fb18 | 57 | |
AhmedPlaymaker | 33:249cf423fb18 | 58 | void Stats::write(int level, SDFileSystem &sd) |
AhmedPlaymaker | 33:249cf423fb18 | 59 | { |
AhmedPlaymaker | 33:249cf423fb18 | 60 | ////////////////////// writing to file while reading previous state ////////////////////////// |
AhmedPlaymaker | 33:249cf423fb18 | 61 | |
AhmedPlaymaker | 33:249cf423fb18 | 62 | // open file for writing ('w') - creates file if it doesn't exist and overwrites |
AhmedPlaymaker | 33:249cf423fb18 | 63 | // if it does. If you wish to add a score onto a list, then you can |
AhmedPlaymaker | 33:249cf423fb18 | 64 | // append instead 'a'. |
AhmedPlaymaker | 33:249cf423fb18 | 65 | |
AhmedPlaymaker | 33:249cf423fb18 | 66 | Stats::read(sd); |
AhmedPlaymaker | 33:249cf423fb18 | 67 | fp = fopen("/sd/highestlevel.txt", "w"); |
AhmedPlaymaker | 33:249cf423fb18 | 68 | _current_level = level; |
AhmedPlaymaker | 33:249cf423fb18 | 69 | if (_stored_top_level < _current_level) { |
AhmedPlaymaker | 33:249cf423fb18 | 70 | _top_level = _current_level; |
AhmedPlaymaker | 33:249cf423fb18 | 71 | } |
AhmedPlaymaker | 33:249cf423fb18 | 72 | |
AhmedPlaymaker | 33:249cf423fb18 | 73 | else { |
AhmedPlaymaker | 33:249cf423fb18 | 74 | _top_level = _stored_top_level; |
AhmedPlaymaker | 33:249cf423fb18 | 75 | } |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 76 | |
AhmedPlaymaker | 33:249cf423fb18 | 77 | if (fp == NULL) { // if it can't open the file then print error message |
AhmedPlaymaker | 33:249cf423fb18 | 78 | //serial.printf("Error! Unable to open file!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 79 | } else { // opened file so can write |
AhmedPlaymaker | 33:249cf423fb18 | 80 | //serial.printf("Writing to file...."); |
AhmedPlaymaker | 33:249cf423fb18 | 81 | fprintf(fp, "%d",_top_level); // ensure data type matches |
AhmedPlaymaker | 33:249cf423fb18 | 82 | //serial.printf("Done.\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 83 | fclose(fp); // ensure you close the file after writing |
AhmedPlaymaker | 33:249cf423fb18 | 84 | } |
AhmedPlaymaker | 33:249cf423fb18 | 85 | |
AhmedPlaymaker | 33:249cf423fb18 | 86 | |
AhmedPlaymaker | 33:249cf423fb18 | 87 | } |
AhmedPlaymaker | 33:249cf423fb18 | 88 | |
AhmedPlaymaker | 33:249cf423fb18 | 89 | void Stats::read(SDFileSystem &sd) |
AhmedPlaymaker | 33:249cf423fb18 | 90 | { |
AhmedPlaymaker | 33:249cf423fb18 | 91 | ////////////////////// reading from previous state ////////////////////////// |
AhmedPlaymaker | 33:249cf423fb18 | 92 | |
AhmedPlaymaker | 33:249cf423fb18 | 93 | // now open file for reading |
AhmedPlaymaker | 33:249cf423fb18 | 94 | fp = fopen("/sd/highestlevel.txt", "r"); |
AhmedPlaymaker | 90:741120c09784 | 95 | _stored_top_level = 0; // To save level 0 as the default value if game has not been played yet. |
AhmedPlaymaker | 33:249cf423fb18 | 96 | |
AhmedPlaymaker | 33:249cf423fb18 | 97 | if (fp == NULL) { // if it can't open the file then print error message |
AhmedPlaymaker | 33:249cf423fb18 | 98 | //serial.printf("Error! Unable to open file!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 99 | } else { // opened file so can write |
AhmedPlaymaker | 33:249cf423fb18 | 100 | fscanf(fp, "%d",&_stored_top_level); // ensure data type matches - note address operator (&) |
AhmedPlaymaker | 33:249cf423fb18 | 101 | //serial.printf("Read %d from file.\n",stored_top_level); |
AhmedPlaymaker | 33:249cf423fb18 | 102 | fclose(fp); // ensure you close the file after reading |
AhmedPlaymaker | 33:249cf423fb18 | 103 | } |
AhmedPlaymaker | 33:249cf423fb18 | 104 | } |
AhmedPlaymaker | 33:249cf423fb18 | 105 | |
AhmedPlaymaker | 33:249cf423fb18 | 106 | void Stats::delete_file(char filename[]) |
AhmedPlaymaker | 33:249cf423fb18 | 107 | { |
AhmedPlaymaker | 33:249cf423fb18 | 108 | //serial.printf("Deleting file '%s'...",filename); |
AhmedPlaymaker | 33:249cf423fb18 | 109 | FILE *fp = fopen(filename, "r"); // try and open file |
AhmedPlaymaker | 33:249cf423fb18 | 110 | if (fp != NULL) { // if it does open... |
AhmedPlaymaker | 33:249cf423fb18 | 111 | fclose(fp); // close it |
AhmedPlaymaker | 33:249cf423fb18 | 112 | remove(filename); // and then delete |
AhmedPlaymaker | 33:249cf423fb18 | 113 | //serial.printf("Done!\n"); |
AhmedPlaymaker | 33:249cf423fb18 | 114 | } |
AhmedPlaymaker | 33:249cf423fb18 | 115 | // if we can't open it, it doesn't exist and so we can't delete it |
AhmedPlaymaker | 33:249cf423fb18 | 116 | } |