Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ClassicEngine.cpp
00001 #include "ClassicEngine.h" 00002 00003 //constructor 00004 ClassicEngine::ClassicEngine(){ 00005 } 00006 00007 //destructor 00008 ClassicEngine::~ClassicEngine(){ 00009 } 00010 00011 //initialiser 00012 void ClassicEngine::init(Ball &ball, Map &map){ 00013 _frames = 0; //set frames counted to 0 00014 _ball_coord.x = 42; //set ball to middle of screen 00015 _ball_coord.y = 24; 00016 _map_coord.x = 47; //set map to the start area 00017 _map_coord.y = 25; 00018 ball.set_position(_ball_coord); 00019 map.set_map_display(_map_coord); 00020 } 00021 00022 void ClassicEngine::classic_update(Ball &ball, FXOS8700CQ &accelerometer, Map &map){ 00023 map.read_input(accelerometer, ball); 00024 map.update(); //get map input from accelerometer and update region to display 00025 ball.set_position(_ball_coord); //keep ball fixed to middle 00026 _frames++; //increment frames count for timer 00027 _map_coord = map.get_map_display(); 00028 _abs_ball_pos.x = _ball_coord.x + _map_coord.x; //update the ball's position in relation to the map 00029 _abs_ball_pos.y = _ball_coord.y + _map_coord.y; //(even though it's fixed on the screen) 00030 } 00031 00032 void ClassicEngine::classic_draw(N5110 &lcd, Map &map, Ball &ball){ 00033 map.draw(lcd); //draw map 00034 ball.draw(lcd); //draw ball 00035 } 00036 00037 bool ClassicEngine::finished(){ 00038 bool finished = false; 00039 if(_abs_ball_pos.x > 402 && //these are the range of coords the ball 00040 _abs_ball_pos.x < 403 && //can have as it crosses the finish line 00041 _abs_ball_pos.y > 101 && //must be given as a range rather than an 00042 _abs_ball_pos.y < 141 ){ //exact number as .x and .y are floats so 00043 finished = true; //will never exactly equal 402 00044 } 00045 else{ finished = false; } 00046 return finished; //tell the game mode that the ball's reached the end of the course 00047 } 00048 00049 void ClassicEngine::mode_complete(N5110 &lcd, Gamepad &gamepad, int fps){ 00050 float time_taken; 00051 while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user advancing 00052 time_taken = _frames / fps; //calculate time taken from no. of frames that passed 00053 char buffer[6]; 00054 sprintf(buffer, "%4.f", time_taken); //write the time taken into a buffer 00055 lcd.clear(); 00056 lcd.printString("You win!", 18, 1); //win message 00057 lcd.printString("Your time:", 12, 3); 00058 lcd.printString(buffer, 18,4); //print the time taken 00059 lcd.printChar('s',54,4); 00060 lcd.printString("(A = back)", 24, 5); 00061 lcd.refresh(); 00062 wait(0.2); //slower frame rate to reduce button bounce effects 00063 } 00064 write_high_scores(time_taken); //check if high score, and update leaderboard if so 00065 } 00066 00067 bool ClassicEngine::mode_failed(N5110 &lcd, Gamepad &gamepad, Ball &ball, Map &map){ 00068 bool back_to_start_menu = false; //bool to store whether restarting or going to main menu 00069 while(1){ 00070 wait(0.2); 00071 lcd.clear(); 00072 lcd.printString("Game over!", 12, 1); //display potential options 00073 lcd.printString("Back = A", 18, 3); 00074 lcd.printString("Replay = B", 12, 4); 00075 lcd.refresh(); 00076 if(gamepad.check_event(gamepad.A_PRESSED)){ 00077 back_to_start_menu = true; //return to start menu 00078 break; } //use of break commands as while(!(a pressed || b pressed)) was buggy in run time 00079 if(gamepad.check_event(gamepad.B_PRESSED)){ 00080 back_to_start_menu = false; //restart 00081 break; } 00082 } 00083 return back_to_start_menu; 00084 } 00085 00086 void ClassicEngine::read_high_scores(){ 00087 FILE *fp; //open file stream 00088 fp = fopen("/sd/classichighscores.txt", "r"); //open classic high scores file in read mode 00089 00090 if(fp == NULL){ 00091 printf("Error: Could not open file"); //check open 00092 } else { 00093 int i = 0; 00094 rewind(fp); //go back to start of file 00095 00096 while(fscanf(fp, "%d,%f", &_index_array[i], &_array_of_values[i]) != EOF){ //read into index and value arrays until end of file 00097 i++; //increment which array row to read into 00098 } 00099 fclose(fp); //close file stream 00100 } 00101 } 00102 00103 void ClassicEngine::check_high_score(int time_taken){ 00104 read_high_scores(); 00105 for(int i = 4; i >= 0; i--){ //algorithm to reorder high scores if new 00106 if(_array_of_values[i] > time_taken){ //high score achieve or discard if not 00107 _array_of_values[i+1] = _array_of_values[i]; 00108 _array_of_values[i] = time_taken; //note: must seed high scores with ascending numbers when file made for first time 00109 } 00110 } 00111 } 00112 00113 void ClassicEngine::write_high_scores(int time_taken){ 00114 check_high_score(time_taken); 00115 FILE *fp; //open file stream 00116 fp = fopen("/sd/classichighscores.txt", "w"); //open classic high score file in write mode 00117 if(fp == NULL){ 00118 printf("Error: Could not open file"); //check open 00119 } else { 00120 for(int i = 0; i < 5; i++){ 00121 fprintf(fp, "%d, %f\n", _index_array[i], _array_of_values[i]); //write 5 highest values of high score array into file 00122 } 00123 fclose(fp); //close file stream 00124 } 00125 }
Generated on Thu Jul 14 2022 00:39:27 by
1.7.2