James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Committer:
JamesCummins
Date:
Thu May 09 10:52:00 2019 +0000
Revision:
40:a1cdb6ab08af
Parent:
38:a85bc227b907
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JamesCummins 29:42651f87522b 1 #include "ClassicEngine.h"
JamesCummins 29:42651f87522b 2
JamesCummins 38:a85bc227b907 3 //constructor
JamesCummins 29:42651f87522b 4 ClassicEngine::ClassicEngine(){
JamesCummins 29:42651f87522b 5 }
JamesCummins 29:42651f87522b 6
JamesCummins 38:a85bc227b907 7 //destructor
JamesCummins 29:42651f87522b 8 ClassicEngine::~ClassicEngine(){
JamesCummins 20:4a39a1a2be51 9 }
JamesCummins 20:4a39a1a2be51 10
JamesCummins 38:a85bc227b907 11 //initialiser
JamesCummins 29:42651f87522b 12 void ClassicEngine::init(Ball &ball, Map &map){
JamesCummins 38:a85bc227b907 13 _frames = 0; //set frames counted to 0
JamesCummins 38:a85bc227b907 14 _ball_coord.x = 42; //set ball to middle of screen
JamesCummins 29:42651f87522b 15 _ball_coord.y = 24;
JamesCummins 38:a85bc227b907 16 _map_coord.x = 47; //set map to the start area
JamesCummins 29:42651f87522b 17 _map_coord.y = 25;
JamesCummins 29:42651f87522b 18 ball.set_position(_ball_coord);
JamesCummins 29:42651f87522b 19 map.set_map_display(_map_coord);
JamesCummins 20:4a39a1a2be51 20 }
JamesCummins 29:42651f87522b 21
JamesCummins 29:42651f87522b 22 void ClassicEngine::classic_update(Ball &ball, FXOS8700CQ &accelerometer, Map &map){
JamesCummins 29:42651f87522b 23 map.read_input(accelerometer, ball);
JamesCummins 38:a85bc227b907 24 map.update(); //get map input from accelerometer and update region to display
JamesCummins 38:a85bc227b907 25 ball.set_position(_ball_coord); //keep ball fixed to middle
JamesCummins 38:a85bc227b907 26 _frames++; //increment frames count for timer
JamesCummins 31:c95f1b1d6423 27 _map_coord = map.get_map_display();
JamesCummins 38:a85bc227b907 28 _abs_ball_pos.x = _ball_coord.x + _map_coord.x; //update the ball's position in relation to the map
JamesCummins 38:a85bc227b907 29 _abs_ball_pos.y = _ball_coord.y + _map_coord.y; //(even though it's fixed on the screen)
JamesCummins 29:42651f87522b 30 }
JamesCummins 29:42651f87522b 31
JamesCummins 29:42651f87522b 32 void ClassicEngine::classic_draw(N5110 &lcd, Map &map, Ball &ball){
JamesCummins 38:a85bc227b907 33 map.draw(lcd); //draw map
JamesCummins 38:a85bc227b907 34 ball.draw(lcd); //draw ball
JamesCummins 29:42651f87522b 35 }
JamesCummins 31:c95f1b1d6423 36
JamesCummins 31:c95f1b1d6423 37 bool ClassicEngine::finished(){
JamesCummins 31:c95f1b1d6423 38 bool finished = false;
JamesCummins 32:eff573ad8e42 39 if(_abs_ball_pos.x > 402 && //these are the range of coords the ball
JamesCummins 36:9f7463a65fe0 40 _abs_ball_pos.x < 403 && //can have as it crosses the finish line
JamesCummins 32:eff573ad8e42 41 _abs_ball_pos.y > 101 && //must be given as a range rather than an
JamesCummins 32:eff573ad8e42 42 _abs_ball_pos.y < 141 ){ //exact number as .x and .y are floats so
JamesCummins 36:9f7463a65fe0 43 finished = true; //will never exactly equal 402
JamesCummins 31:c95f1b1d6423 44 }
JamesCummins 31:c95f1b1d6423 45 else{ finished = false; }
JamesCummins 38:a85bc227b907 46 return finished; //tell the game mode that the ball's reached the end of the course
JamesCummins 31:c95f1b1d6423 47 }
JamesCummins 31:c95f1b1d6423 48
JamesCummins 31:c95f1b1d6423 49 void ClassicEngine::mode_complete(N5110 &lcd, Gamepad &gamepad, int fps){
JamesCummins 36:9f7463a65fe0 50 float time_taken;
JamesCummins 38:a85bc227b907 51 while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user advancing
JamesCummins 38:a85bc227b907 52 time_taken = _frames / fps; //calculate time taken from no. of frames that passed
JamesCummins 31:c95f1b1d6423 53 char buffer[6];
JamesCummins 38:a85bc227b907 54 sprintf(buffer, "%4.f", time_taken); //write the time taken into a buffer
JamesCummins 31:c95f1b1d6423 55 lcd.clear();
JamesCummins 38:a85bc227b907 56 lcd.printString("You win!", 18, 1); //win message
JamesCummins 31:c95f1b1d6423 57 lcd.printString("Your time:", 12, 3);
JamesCummins 38:a85bc227b907 58 lcd.printString(buffer, 18,4); //print the time taken
JamesCummins 31:c95f1b1d6423 59 lcd.printChar('s',54,4);
JamesCummins 31:c95f1b1d6423 60 lcd.printString("(A = back)", 24, 5);
JamesCummins 31:c95f1b1d6423 61 lcd.refresh();
JamesCummins 38:a85bc227b907 62 wait(0.2); //slower frame rate to reduce button bounce effects
JamesCummins 31:c95f1b1d6423 63 }
JamesCummins 38:a85bc227b907 64 write_high_scores(time_taken); //check if high score, and update leaderboard if so
JamesCummins 31:c95f1b1d6423 65 }
JamesCummins 31:c95f1b1d6423 66
JamesCummins 31:c95f1b1d6423 67 bool ClassicEngine::mode_failed(N5110 &lcd, Gamepad &gamepad, Ball &ball, Map &map){
JamesCummins 38:a85bc227b907 68 bool back_to_start_menu = false; //bool to store whether restarting or going to main menu
JamesCummins 31:c95f1b1d6423 69 while(1){
JamesCummins 31:c95f1b1d6423 70 wait(0.2);
JamesCummins 31:c95f1b1d6423 71 lcd.clear();
JamesCummins 38:a85bc227b907 72 lcd.printString("Game over!", 12, 1); //display potential options
JamesCummins 31:c95f1b1d6423 73 lcd.printString("Back = A", 18, 3);
JamesCummins 31:c95f1b1d6423 74 lcd.printString("Replay = B", 12, 4);
JamesCummins 31:c95f1b1d6423 75 lcd.refresh();
JamesCummins 31:c95f1b1d6423 76 if(gamepad.check_event(gamepad.A_PRESSED)){
JamesCummins 38:a85bc227b907 77 back_to_start_menu = true; //return to start menu
JamesCummins 38:a85bc227b907 78 break; } //use of break commands as while(!(a pressed || b pressed)) was buggy in run time
JamesCummins 31:c95f1b1d6423 79 if(gamepad.check_event(gamepad.B_PRESSED)){
JamesCummins 38:a85bc227b907 80 back_to_start_menu = false; //restart
JamesCummins 31:c95f1b1d6423 81 break; }
JamesCummins 31:c95f1b1d6423 82 }
JamesCummins 31:c95f1b1d6423 83 return back_to_start_menu;
JamesCummins 36:9f7463a65fe0 84 }
JamesCummins 36:9f7463a65fe0 85
JamesCummins 36:9f7463a65fe0 86 void ClassicEngine::read_high_scores(){
JamesCummins 38:a85bc227b907 87 FILE *fp; //open file stream
JamesCummins 38:a85bc227b907 88 fp = fopen("/sd/classichighscores.txt", "r"); //open classic high scores file in read mode
JamesCummins 36:9f7463a65fe0 89
JamesCummins 36:9f7463a65fe0 90 if(fp == NULL){
JamesCummins 38:a85bc227b907 91 printf("Error: Could not open file"); //check open
JamesCummins 36:9f7463a65fe0 92 } else {
JamesCummins 36:9f7463a65fe0 93 int i = 0;
JamesCummins 38:a85bc227b907 94 rewind(fp); //go back to start of file
JamesCummins 36:9f7463a65fe0 95
JamesCummins 38:a85bc227b907 96 while(fscanf(fp, "%d,%f", &_index_array[i], &_array_of_values[i]) != EOF){ //read into index and value arrays until end of file
JamesCummins 38:a85bc227b907 97 i++; //increment which array row to read into
JamesCummins 36:9f7463a65fe0 98 }
JamesCummins 38:a85bc227b907 99 fclose(fp); //close file stream
JamesCummins 36:9f7463a65fe0 100 }
JamesCummins 36:9f7463a65fe0 101 }
JamesCummins 36:9f7463a65fe0 102
JamesCummins 36:9f7463a65fe0 103 void ClassicEngine::check_high_score(int time_taken){
JamesCummins 36:9f7463a65fe0 104 read_high_scores();
JamesCummins 38:a85bc227b907 105 for(int i = 4; i >= 0; i--){ //algorithm to reorder high scores if new
JamesCummins 38:a85bc227b907 106 if(_array_of_values[i] > time_taken){ //high score achieve or discard if not
JamesCummins 36:9f7463a65fe0 107 _array_of_values[i+1] = _array_of_values[i];
JamesCummins 38:a85bc227b907 108 _array_of_values[i] = time_taken; //note: must seed high scores with ascending numbers when file made for first time
JamesCummins 36:9f7463a65fe0 109 }
JamesCummins 36:9f7463a65fe0 110 }
JamesCummins 36:9f7463a65fe0 111 }
JamesCummins 36:9f7463a65fe0 112
JamesCummins 36:9f7463a65fe0 113 void ClassicEngine::write_high_scores(int time_taken){
JamesCummins 36:9f7463a65fe0 114 check_high_score(time_taken);
JamesCummins 38:a85bc227b907 115 FILE *fp; //open file stream
JamesCummins 38:a85bc227b907 116 fp = fopen("/sd/classichighscores.txt", "w"); //open classic high score file in write mode
JamesCummins 36:9f7463a65fe0 117 if(fp == NULL){
JamesCummins 38:a85bc227b907 118 printf("Error: Could not open file"); //check open
JamesCummins 36:9f7463a65fe0 119 } else {
JamesCummins 36:9f7463a65fe0 120 for(int i = 0; i < 5; i++){
JamesCummins 38:a85bc227b907 121 fprintf(fp, "%d, %f\n", _index_array[i], _array_of_values[i]); //write 5 highest values of high score array into file
JamesCummins 36:9f7463a65fe0 122 }
JamesCummins 38:a85bc227b907 123 fclose(fp); //close file stream
JamesCummins 36:9f7463a65fe0 124 }
JamesCummins 36:9f7463a65fe0 125 }