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.
Classic_Engine/ClassicEngine.cpp@36:9f7463a65fe0, 2019-05-06 (annotated)
- Committer:
- JamesCummins
- Date:
- Mon May 06 00:05:52 2019 +0000
- Revision:
- 36:9f7463a65fe0
- Parent:
- 32:eff573ad8e42
- Child:
- 38:a85bc227b907
Finished, except for sounds. Doxygen still to do
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 29:42651f87522b | 1 | #include "ClassicEngine.h" |
JamesCummins | 29:42651f87522b | 2 | |
JamesCummins | 29:42651f87522b | 3 | ClassicEngine::ClassicEngine(){ |
JamesCummins | 29:42651f87522b | 4 | } |
JamesCummins | 29:42651f87522b | 5 | |
JamesCummins | 29:42651f87522b | 6 | ClassicEngine::~ClassicEngine(){ |
JamesCummins | 20:4a39a1a2be51 | 7 | } |
JamesCummins | 20:4a39a1a2be51 | 8 | |
JamesCummins | 29:42651f87522b | 9 | void ClassicEngine::init(Ball &ball, Map &map){ |
JamesCummins | 31:c95f1b1d6423 | 10 | _frames = 0; |
JamesCummins | 29:42651f87522b | 11 | _ball_coord.x = 42; |
JamesCummins | 29:42651f87522b | 12 | _ball_coord.y = 24; |
JamesCummins | 29:42651f87522b | 13 | _map_coord.x = 47; |
JamesCummins | 29:42651f87522b | 14 | _map_coord.y = 25; |
JamesCummins | 29:42651f87522b | 15 | ball.set_position(_ball_coord); |
JamesCummins | 29:42651f87522b | 16 | map.set_map_display(_map_coord); |
JamesCummins | 20:4a39a1a2be51 | 17 | } |
JamesCummins | 29:42651f87522b | 18 | |
JamesCummins | 29:42651f87522b | 19 | void ClassicEngine::classic_update(Ball &ball, FXOS8700CQ &accelerometer, Map &map){ |
JamesCummins | 29:42651f87522b | 20 | map.read_input(accelerometer, ball); |
JamesCummins | 29:42651f87522b | 21 | map.update(); |
JamesCummins | 29:42651f87522b | 22 | ball.set_position(_ball_coord); |
JamesCummins | 31:c95f1b1d6423 | 23 | _frames++; |
JamesCummins | 31:c95f1b1d6423 | 24 | _map_coord = map.get_map_display(); |
JamesCummins | 31:c95f1b1d6423 | 25 | _abs_ball_pos.x = _ball_coord.x + _map_coord.x; |
JamesCummins | 31:c95f1b1d6423 | 26 | _abs_ball_pos.y = _ball_coord.y + _map_coord.y; |
JamesCummins | 29:42651f87522b | 27 | } |
JamesCummins | 29:42651f87522b | 28 | |
JamesCummins | 29:42651f87522b | 29 | void ClassicEngine::classic_draw(N5110 &lcd, Map &map, Ball &ball){ |
JamesCummins | 29:42651f87522b | 30 | map.draw(lcd); |
JamesCummins | 29:42651f87522b | 31 | ball.draw(lcd); |
JamesCummins | 29:42651f87522b | 32 | } |
JamesCummins | 31:c95f1b1d6423 | 33 | |
JamesCummins | 31:c95f1b1d6423 | 34 | bool ClassicEngine::finished(){ |
JamesCummins | 31:c95f1b1d6423 | 35 | bool finished = false; |
JamesCummins | 32:eff573ad8e42 | 36 | if(_abs_ball_pos.x > 402 && //these are the range of coords the ball |
JamesCummins | 36:9f7463a65fe0 | 37 | _abs_ball_pos.x < 403 && //can have as it crosses the finish line |
JamesCummins | 32:eff573ad8e42 | 38 | _abs_ball_pos.y > 101 && //must be given as a range rather than an |
JamesCummins | 32:eff573ad8e42 | 39 | _abs_ball_pos.y < 141 ){ //exact number as .x and .y are floats so |
JamesCummins | 36:9f7463a65fe0 | 40 | finished = true; //will never exactly equal 402 |
JamesCummins | 31:c95f1b1d6423 | 41 | } |
JamesCummins | 31:c95f1b1d6423 | 42 | else{ finished = false; } |
JamesCummins | 32:eff573ad8e42 | 43 | printf("ball pos = %f , %f | finished = %d\n", _abs_ball_pos.x, _abs_ball_pos.y, finished); |
JamesCummins | 31:c95f1b1d6423 | 44 | return finished; |
JamesCummins | 31:c95f1b1d6423 | 45 | } |
JamesCummins | 31:c95f1b1d6423 | 46 | |
JamesCummins | 31:c95f1b1d6423 | 47 | void ClassicEngine::mode_complete(N5110 &lcd, Gamepad &gamepad, int fps){ |
JamesCummins | 36:9f7463a65fe0 | 48 | float time_taken; |
JamesCummins | 31:c95f1b1d6423 | 49 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ |
JamesCummins | 36:9f7463a65fe0 | 50 | time_taken = _frames / fps; |
JamesCummins | 31:c95f1b1d6423 | 51 | char buffer[6]; |
JamesCummins | 31:c95f1b1d6423 | 52 | sprintf(buffer, "%4.f", time_taken); |
JamesCummins | 31:c95f1b1d6423 | 53 | lcd.clear(); |
JamesCummins | 31:c95f1b1d6423 | 54 | lcd.printString("You win!", 18, 1); |
JamesCummins | 31:c95f1b1d6423 | 55 | lcd.printString("Your time:", 12, 3); |
JamesCummins | 31:c95f1b1d6423 | 56 | lcd.printString(buffer, 18,4); |
JamesCummins | 31:c95f1b1d6423 | 57 | lcd.printChar('s',54,4); |
JamesCummins | 31:c95f1b1d6423 | 58 | lcd.printString("(A = back)", 24, 5); |
JamesCummins | 31:c95f1b1d6423 | 59 | lcd.refresh(); |
JamesCummins | 31:c95f1b1d6423 | 60 | wait(0.2); |
JamesCummins | 31:c95f1b1d6423 | 61 | } |
JamesCummins | 36:9f7463a65fe0 | 62 | write_high_scores(time_taken); |
JamesCummins | 31:c95f1b1d6423 | 63 | } |
JamesCummins | 31:c95f1b1d6423 | 64 | |
JamesCummins | 31:c95f1b1d6423 | 65 | bool ClassicEngine::mode_failed(N5110 &lcd, Gamepad &gamepad, Ball &ball, Map &map){ |
JamesCummins | 31:c95f1b1d6423 | 66 | bool back_to_start_menu = false; |
JamesCummins | 31:c95f1b1d6423 | 67 | while(1){ |
JamesCummins | 31:c95f1b1d6423 | 68 | wait(0.2); |
JamesCummins | 31:c95f1b1d6423 | 69 | lcd.clear(); |
JamesCummins | 32:eff573ad8e42 | 70 | lcd.printString("Game over!", 12, 1); |
JamesCummins | 31:c95f1b1d6423 | 71 | lcd.printString("Back = A", 18, 3); |
JamesCummins | 31:c95f1b1d6423 | 72 | lcd.printString("Replay = B", 12, 4); |
JamesCummins | 31:c95f1b1d6423 | 73 | lcd.refresh(); |
JamesCummins | 31:c95f1b1d6423 | 74 | if(gamepad.check_event(gamepad.A_PRESSED)){ |
JamesCummins | 31:c95f1b1d6423 | 75 | back_to_start_menu = true; |
JamesCummins | 31:c95f1b1d6423 | 76 | break; } |
JamesCummins | 31:c95f1b1d6423 | 77 | if(gamepad.check_event(gamepad.B_PRESSED)){ |
JamesCummins | 31:c95f1b1d6423 | 78 | back_to_start_menu = false; |
JamesCummins | 31:c95f1b1d6423 | 79 | break; } |
JamesCummins | 31:c95f1b1d6423 | 80 | } |
JamesCummins | 31:c95f1b1d6423 | 81 | return back_to_start_menu; |
JamesCummins | 36:9f7463a65fe0 | 82 | } |
JamesCummins | 36:9f7463a65fe0 | 83 | |
JamesCummins | 36:9f7463a65fe0 | 84 | void ClassicEngine::read_high_scores(){ |
JamesCummins | 36:9f7463a65fe0 | 85 | FILE *fp; |
JamesCummins | 36:9f7463a65fe0 | 86 | fp = fopen("/sd/classichighscores.txt", "r"); |
JamesCummins | 36:9f7463a65fe0 | 87 | |
JamesCummins | 36:9f7463a65fe0 | 88 | if(fp == NULL){ |
JamesCummins | 36:9f7463a65fe0 | 89 | printf("Error: Could not open file"); |
JamesCummins | 36:9f7463a65fe0 | 90 | } else { |
JamesCummins | 36:9f7463a65fe0 | 91 | int i = 0; |
JamesCummins | 36:9f7463a65fe0 | 92 | rewind(fp); |
JamesCummins | 36:9f7463a65fe0 | 93 | |
JamesCummins | 36:9f7463a65fe0 | 94 | while(fscanf(fp, "%d,%f", &_index_array[i], &_array_of_values[i]) != EOF){ |
JamesCummins | 36:9f7463a65fe0 | 95 | i++; |
JamesCummins | 36:9f7463a65fe0 | 96 | } |
JamesCummins | 36:9f7463a65fe0 | 97 | fclose(fp); |
JamesCummins | 36:9f7463a65fe0 | 98 | } |
JamesCummins | 36:9f7463a65fe0 | 99 | } |
JamesCummins | 36:9f7463a65fe0 | 100 | |
JamesCummins | 36:9f7463a65fe0 | 101 | void ClassicEngine::check_high_score(int time_taken){ |
JamesCummins | 36:9f7463a65fe0 | 102 | read_high_scores(); |
JamesCummins | 36:9f7463a65fe0 | 103 | for(int i = 4; i >= 0; i--){ |
JamesCummins | 36:9f7463a65fe0 | 104 | if(_array_of_values[i] > time_taken){ |
JamesCummins | 36:9f7463a65fe0 | 105 | _array_of_values[i+1] = _array_of_values[i]; |
JamesCummins | 36:9f7463a65fe0 | 106 | _array_of_values[i] = time_taken; |
JamesCummins | 36:9f7463a65fe0 | 107 | } |
JamesCummins | 36:9f7463a65fe0 | 108 | } |
JamesCummins | 36:9f7463a65fe0 | 109 | } |
JamesCummins | 36:9f7463a65fe0 | 110 | |
JamesCummins | 36:9f7463a65fe0 | 111 | void ClassicEngine::write_high_scores(int time_taken){ |
JamesCummins | 36:9f7463a65fe0 | 112 | check_high_score(time_taken); |
JamesCummins | 36:9f7463a65fe0 | 113 | FILE *fp; |
JamesCummins | 36:9f7463a65fe0 | 114 | fp = fopen("/sd/classichighscores.txt", "w"); |
JamesCummins | 36:9f7463a65fe0 | 115 | if(fp == NULL){ |
JamesCummins | 36:9f7463a65fe0 | 116 | printf("Error: Could not open file"); |
JamesCummins | 36:9f7463a65fe0 | 117 | } else { |
JamesCummins | 36:9f7463a65fe0 | 118 | for(int i = 0; i < 5; i++){ |
JamesCummins | 36:9f7463a65fe0 | 119 | fprintf(fp, "%d, %f\n", _index_array[i], _array_of_values[i]); |
JamesCummins | 36:9f7463a65fe0 | 120 | } |
JamesCummins | 36:9f7463a65fe0 | 121 | fclose(fp); |
JamesCummins | 36:9f7463a65fe0 | 122 | } |
JamesCummins | 36:9f7463a65fe0 | 123 | } |