Dependencies:   mbed

Revision:
21:d5b1160f349f
Parent:
20:c697902b844f
Child:
22:76bb0f52be8b
--- a/Game/Game.cpp	Sun May 24 17:40:56 2020 +0000
+++ b/Game/Game.cpp	Mon May 25 00:55:38 2020 +0000
@@ -1,8 +1,11 @@
 #include "Game.h"
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
 Ticker ticker;
 
+FILE *fp;
 volatile int sound_timer_flag = 0;
 volatile int sound_sample = 0;
+volatile int level_flag = 1; //initially set
 
 void timer_isr();
 
@@ -15,11 +18,11 @@
 
 void Game::init() {
     _is_goal = false;
+    set_highscore(0);
     _lives = 3;
     _new_lives_threshold = 0;
     _level = 1;
     _score = 0;
-    _highscore = 0;
     _speed = 0.30f;
     _new_speed_threshold = 0.00f;
     _x_val = 42.00f;
@@ -116,6 +119,10 @@
 }
 void Game::updateLevel() {
     _level = random_level_gen(10); //generate random level
+    if( level_flag == 1) { //first level is always level 1
+        level_flag = 0; //flag always stays 0 after first time
+        _level = 1; 
+    } 
     _ball->set_level(_level); //set level
     //printf("Level (in game) = %d \n", _level); 
 }
@@ -150,16 +157,33 @@
     }
 }
 void Game::set_highscore (int score) {
-    _highscore = score;   
+      _highscore = score;
+    fp = fopen("/sd/highscore.txt", "w");
+    if (fp == NULL) {  // if it can't open the file then print error message
+        printf("Error! Unable to open file!\n");
+    } else {  // opened file so can write
+        printf("Writing to file....");
+        fprintf(fp, "%d",_highscore); // ensure data type matches
+        printf("Done.\n");
+        fclose(fp);  // ensure you close the file after writing
+    }
 }
 int Game::get_highscore() {
+    fp = fopen("/sd/highscore.txt", "r");
+    if (fp == NULL) {  // if it can't open the file then print error message
+        printf("Error! Unable to open file!\n");
+    } else {  // opened file so can write
+        fscanf(fp, "%d",&_highscore); // ensure data type matches - note address operator (&)
+        //printf("Read %d from file.\n",_highscore);
+        fclose(fp);  // ensure you close the file after reading
+    }
     int val = _highscore;
     return val;   
 }
 void Game::pointer_input() {
     updateScore();
     _ball->displayBackground();
-    _ball->init();
+    _ball->drawBall(BALL_INIT_X, BALL_INIT_Y, 6);
     //draw aim pointer
     _lcd->drawLine(WIDTH / 2 - 5, 41, _x_val, HEIGHT / 2 + 7, 1);//left side
     _lcd->drawLine(WIDTH / 2 + 5, 41, _x_val, HEIGHT / 2 + 7, 1);//right side
@@ -177,13 +201,13 @@
 void Game::power_meter_input() {
     updateScore();
     _ball->displayBackground();
-    _ball->init();
+    _ball->drawBall(BALL_INIT_X, BALL_INIT_Y, 6);
     //keep direction of pointer
     _lcd->drawLine(WIDTH / 2 - 5, 41, _x_val, HEIGHT / 2 + 7, 1);
     _lcd->drawLine(WIDTH / 2 + 5, 41, _x_val, HEIGHT / 2 + 7, 1);
     _lcd->drawLine(WIDTH / 2 + 5, 41, WIDTH / 2 - 5, 41, 1);
     //fill and empty power meter
-    _lcd->drawRect(77,27,6,_y_val,FILL_BLACK);
+    _lcd->drawRect(77, 27, 6, _y_val, FILL_BLACK);
     _y_val += _speed/2;
     if((int)_y_val >= 20) { _speed = (-1 * _speed); } //power meter full
     else if((int)_y_val <= 0) { _speed = abs(_speed); } // power meter empty