Simple fish eat program

Dependencies:   mbed mbed-rtos N5110 ShiftReg Tone

classes/HighScore.cpp

Committer:
el18a2k
Date:
2021-08-02
Revision:
14:f1552b691274
Parent:
12:20ac766b3175

File content as of revision 14:f1552b691274:

/* Fish Eat Game v2.0
*   Author: Andrew Knowles
*   Date:   21/04/2021
*   Note: See header files for additional recognistion */  

#include "HighScore.h"

LocalFileSystem local("scores"); //define local file system

//display value in file
void HighScore::displayFile(N5110 &lcd){
    FILE* Scores = fopen("/scores/textfile.txt","r"); //opens and reads file
    fgets(readScore1, 8, Scores); //read first data value and stores it in string 'readScore'
    fclose(Scores); //closes Scores    
        
    lcd.clear();
    lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); //draws screen border
    lcd.printString(" Top Score", 12, 1);
    lcd.printString(readScore1, 30, 3);
    lcd.refresh();
}
    
//write/amend file
void HighScore::writeFile(int score){
    score1 = score;
                                //string            int
    stringCon1 = sprintf(writeScore1, "%i", score1); //converts int into string   
    FILE* Scores = fopen("/scores/textfile.txt","w"); //create a pointer called scores which opens a textfile and writes
    fputs(writeScore1, Scores); //writes string writeScore1 to file
    fclose(Scores);
}

//read from file
int HighScore::readFile(){
    FILE* Scores = fopen("/scores/textfile.txt","r"); //opens and reads file
    fgets(readScore1, 8, Scores); //read first data value and stores it in string 'readScore'
    fclose(Scores); //closes Scores 
    
    //converts string back to int
    readScore = atoi(readScore1);
    
    return readScore;
}