Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Mon May 06 14:28:35 2019 +0000
Revision:
83:329da564799a
Parent:
62:ebf6ecf8a6d5
Child:
86:fafb72e08195
Now, I have put gamepad and lcd objects from the main() in a pointer, so that i can declare them in init() in every class i use them in and use them as global objects for each class, without having to send their addresses to all of the functions.

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