James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Committer:
JamesCummins
Date:
Thu May 09 01:09:18 2019 +0000
Revision:
38:a85bc227b907
Parent:
36:9f7463a65fe0
Child:
40:a1cdb6ab08af
Doxygen documentation written out and in line commenting completed. Game working and finished. Tests.h still to write out

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 32:eff573ad8e42 46 printf("ball pos = %f , %f | finished = %d\n", _abs_ball_pos.x, _abs_ball_pos.y, finished);
JamesCummins 38:a85bc227b907 47 return finished; //tell the game mode that the ball's reached the end of the course
JamesCummins 31:c95f1b1d6423 48 }
JamesCummins 31:c95f1b1d6423 49
JamesCummins 31:c95f1b1d6423 50 void ClassicEngine::mode_complete(N5110 &lcd, Gamepad &gamepad, int fps){
JamesCummins 36:9f7463a65fe0 51 float time_taken;
JamesCummins 38:a85bc227b907 52 while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user advancing
JamesCummins 38:a85bc227b907 53 time_taken = _frames / fps; //calculate time taken from no. of frames that passed
JamesCummins 31:c95f1b1d6423 54 char buffer[6];
JamesCummins 38:a85bc227b907 55 sprintf(buffer, "%4.f", time_taken); //write the time taken into a buffer
JamesCummins 31:c95f1b1d6423 56 lcd.clear();
JamesCummins 38:a85bc227b907 57 lcd.printString("You win!", 18, 1); //win message
JamesCummins 31:c95f1b1d6423 58 lcd.printString("Your time:", 12, 3);
JamesCummins 38:a85bc227b907 59 lcd.printString(buffer, 18,4); //print the time taken
JamesCummins 31:c95f1b1d6423 60 lcd.printChar('s',54,4);
JamesCummins 31:c95f1b1d6423 61 lcd.printString("(A = back)", 24, 5);
JamesCummins 31:c95f1b1d6423 62 lcd.refresh();
JamesCummins 38:a85bc227b907 63 wait(0.2); //slower frame rate to reduce button bounce effects
JamesCummins 31:c95f1b1d6423 64 }
JamesCummins 38:a85bc227b907 65 write_high_scores(time_taken); //check if high score, and update leaderboard if so
JamesCummins 31:c95f1b1d6423 66 }
JamesCummins 31:c95f1b1d6423 67
JamesCummins 31:c95f1b1d6423 68 bool ClassicEngine::mode_failed(N5110 &lcd, Gamepad &gamepad, Ball &ball, Map &map){
JamesCummins 38:a85bc227b907 69 bool back_to_start_menu = false; //bool to store whether restarting or going to main menu
JamesCummins 31:c95f1b1d6423 70 while(1){
JamesCummins 31:c95f1b1d6423 71 wait(0.2);
JamesCummins 31:c95f1b1d6423 72 lcd.clear();
JamesCummins 38:a85bc227b907 73 lcd.printString("Game over!", 12, 1); //display potential options
JamesCummins 31:c95f1b1d6423 74 lcd.printString("Back = A", 18, 3);
JamesCummins 31:c95f1b1d6423 75 lcd.printString("Replay = B", 12, 4);
JamesCummins 31:c95f1b1d6423 76 lcd.refresh();
JamesCummins 31:c95f1b1d6423 77 if(gamepad.check_event(gamepad.A_PRESSED)){
JamesCummins 38:a85bc227b907 78 back_to_start_menu = true; //return to start menu
JamesCummins 38:a85bc227b907 79 break; } //use of break commands as while(!(a pressed || b pressed)) was buggy in run time
JamesCummins 31:c95f1b1d6423 80 if(gamepad.check_event(gamepad.B_PRESSED)){
JamesCummins 38:a85bc227b907 81 back_to_start_menu = false; //restart
JamesCummins 31:c95f1b1d6423 82 break; }
JamesCummins 31:c95f1b1d6423 83 }
JamesCummins 31:c95f1b1d6423 84 return back_to_start_menu;
JamesCummins 36:9f7463a65fe0 85 }
JamesCummins 36:9f7463a65fe0 86
JamesCummins 36:9f7463a65fe0 87 void ClassicEngine::read_high_scores(){
JamesCummins 38:a85bc227b907 88 FILE *fp; //open file stream
JamesCummins 38:a85bc227b907 89 fp = fopen("/sd/classichighscores.txt", "r"); //open classic high scores file in read mode
JamesCummins 36:9f7463a65fe0 90
JamesCummins 36:9f7463a65fe0 91 if(fp == NULL){
JamesCummins 38:a85bc227b907 92 printf("Error: Could not open file"); //check open
JamesCummins 36:9f7463a65fe0 93 } else {
JamesCummins 36:9f7463a65fe0 94 int i = 0;
JamesCummins 38:a85bc227b907 95 rewind(fp); //go back to start of file
JamesCummins 36:9f7463a65fe0 96
JamesCummins 38:a85bc227b907 97 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 98 i++; //increment which array row to read into
JamesCummins 36:9f7463a65fe0 99 }
JamesCummins 38:a85bc227b907 100 fclose(fp); //close file stream
JamesCummins 36:9f7463a65fe0 101 }
JamesCummins 36:9f7463a65fe0 102 }
JamesCummins 36:9f7463a65fe0 103
JamesCummins 36:9f7463a65fe0 104 void ClassicEngine::check_high_score(int time_taken){
JamesCummins 36:9f7463a65fe0 105 read_high_scores();
JamesCummins 38:a85bc227b907 106 for(int i = 4; i >= 0; i--){ //algorithm to reorder high scores if new
JamesCummins 38:a85bc227b907 107 if(_array_of_values[i] > time_taken){ //high score achieve or discard if not
JamesCummins 36:9f7463a65fe0 108 _array_of_values[i+1] = _array_of_values[i];
JamesCummins 38:a85bc227b907 109 _array_of_values[i] = time_taken; //note: must seed high scores with ascending numbers when file made for first time
JamesCummins 36:9f7463a65fe0 110 }
JamesCummins 36:9f7463a65fe0 111 }
JamesCummins 36:9f7463a65fe0 112 }
JamesCummins 36:9f7463a65fe0 113
JamesCummins 36:9f7463a65fe0 114 void ClassicEngine::write_high_scores(int time_taken){
JamesCummins 36:9f7463a65fe0 115 check_high_score(time_taken);
JamesCummins 38:a85bc227b907 116 FILE *fp; //open file stream
JamesCummins 38:a85bc227b907 117 fp = fopen("/sd/classichighscores.txt", "w"); //open classic high score file in write mode
JamesCummins 36:9f7463a65fe0 118 if(fp == NULL){
JamesCummins 38:a85bc227b907 119 printf("Error: Could not open file"); //check open
JamesCummins 36:9f7463a65fe0 120 } else {
JamesCummins 36:9f7463a65fe0 121 for(int i = 0; i < 5; i++){
JamesCummins 38:a85bc227b907 122 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 123 }
JamesCummins 38:a85bc227b907 124 fclose(fp); //close file stream
JamesCummins 36:9f7463a65fe0 125 }
JamesCummins 36:9f7463a65fe0 126 }