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.
Dependencies: mbed
Engine/Engine.h@31:5c4acae51026, 2019-05-09 (annotated)
- Committer:
- el17m2h
- Date:
- Thu May 09 10:15:20 2019 +0000
- Revision:
- 31:5c4acae51026
- Parent:
- 29:15e9640646b7
- Child:
- 34:a9b14a4ccd46
Added comments to the doodler cpp.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17m2h | 2:360a6c301a4e | 1 | #ifndef ENGINE_H |
| el17m2h | 2:360a6c301a4e | 2 | #define ENGINE_H |
| el17m2h | 2:360a6c301a4e | 3 | |
| el17m2h | 22:0d2ac98a8b48 | 4 | #include <iostream> |
| el17m2h | 2:360a6c301a4e | 5 | #include "mbed.h" |
| el17m2h | 2:360a6c301a4e | 6 | #include "N5110.h" |
| el17m2h | 2:360a6c301a4e | 7 | #include "Gamepad.h" |
| el17m2h | 2:360a6c301a4e | 8 | #include "Floors.h" |
| el17m2h | 4:8ec314f806ae | 9 | #include "Doodler.h" |
| el17m2h | 17:74de8c17ddac | 10 | #include "Bullet.h" |
| el17m2h | 2:360a6c301a4e | 11 | |
| el17m2h | 24:67dc71a8f009 | 12 | /** Engine class |
| el17m2h | 24:67dc71a8f009 | 13 | @brief Class for the engine of the game |
| el17m2h | 24:67dc71a8f009 | 14 | @author Melissa Hartmann |
| el17m2h | 24:67dc71a8f009 | 15 | @date May 2019 |
| el17m2h | 24:67dc71a8f009 | 16 | */ |
| el17m2h | 29:15e9640646b7 | 17 | class Engine |
| el17m2h | 29:15e9640646b7 | 18 | { |
| el17m2h | 2:360a6c301a4e | 19 | public: |
| el17m2h | 2:360a6c301a4e | 20 | Engine(); |
| el17m2h | 2:360a6c301a4e | 21 | ~Engine(); |
| el17m2h | 29:15e9640646b7 | 22 | |
| el17m2h | 29:15e9640646b7 | 23 | /** |
| el17m2h | 29:15e9640646b7 | 24 | @brief Initializing game |
| el17m2h | 29:15e9640646b7 | 25 | @details The function defines the screen's size, and the initial position of the doodler, |
| el17m2h | 29:15e9640646b7 | 26 | the bullet and the 6 floors. This is done by adding the functions of initialization to the |
| el17m2h | 26:d16a5b1e0ace | 27 | corresponding class. A for loop is used to repeat the process to create 6 floors, by defining |
| el17m2h | 26:d16a5b1e0ace | 28 | them as an array of 6 objects. |
| el17m2h | 24:67dc71a8f009 | 29 | */ |
| el17m2h | 26:d16a5b1e0ace | 30 | void init(); |
| el17m2h | 29:15e9640646b7 | 31 | |
| el17m2h | 29:15e9640646b7 | 32 | /** |
| el17m2h | 26:d16a5b1e0ace | 33 | @brief Initializes the screen's size and the initial score |
| el17m2h | 29:15e9640646b7 | 34 | @details The function defines the screen's position, height and width (which remains the same |
| el17m2h | 29:15e9640646b7 | 35 | throughout the game). It also sets an initial score of 0. |
| el17m2h | 24:67dc71a8f009 | 36 | */ |
| el17m2h | 23:9be87557b89a | 37 | void screen_init(); |
| el17m2h | 29:15e9640646b7 | 38 | |
| el17m2h | 29:15e9640646b7 | 39 | /** |
| el17m2h | 26:d16a5b1e0ace | 40 | @brief Sets the inputed gamepad parameters |
| el17m2h | 29:15e9640646b7 | 41 | @param Gamepad &pad |
| el17m2h | 29:15e9640646b7 | 42 | @details The function sets the inputed gamepad parameters for the direction of the joystick, |
| el17m2h | 29:15e9640646b7 | 43 | the magnitude at which the joystick is moved and checks if the button Y is pressed. |
| el17m2h | 24:67dc71a8f009 | 44 | */ |
| el17m2h | 5:8814d6de77d0 | 45 | void read_input(Gamepad &pad); |
| el17m2h | 29:15e9640646b7 | 46 | |
| el17m2h | 29:15e9640646b7 | 47 | /** |
| el17m2h | 26:d16a5b1e0ace | 48 | @brief Prints the doodler, floors and bullet |
| el17m2h | 26:d16a5b1e0ace | 49 | @param N5110 &lcd |
| el17m2h | 26:d16a5b1e0ace | 50 | @details The function calls the draw functions for the doodler, floors and bullet. For the floors, a |
| el17m2h | 26:d16a5b1e0ace | 51 | for loop is used to repeat the process to draw the 6 floors. It also draws the screen rectangle and the score. |
| el17m2h | 26:d16a5b1e0ace | 52 | */ |
| el17m2h | 26:d16a5b1e0ace | 53 | void draw(N5110 &lcd); |
| el17m2h | 29:15e9640646b7 | 54 | |
| el17m2h | 29:15e9640646b7 | 55 | /** |
| el17m2h | 26:d16a5b1e0ace | 56 | @brief Updates the classes in the game by calling it's update functions |
| el17m2h | 26:d16a5b1e0ace | 57 | @param Gamepad &pad |
| el17m2h | 29:15e9640646b7 | 58 | @details Firstly, the function checks the collision of the doodler with the floors by getting their current positions |
| el17m2h | 27:af14fcd5a520 | 59 | and inputing them to the "check_floors_collision" function, which will update the velocity of the doodler. Secondly, it |
| el17m2h | 29:15e9640646b7 | 60 | calls the "update" functions for the floors and doodler which will update their positions. It then calls the |
| el17m2h | 27:af14fcd5a520 | 61 | "add_score" function to update the score value. Next, it checks if the doodler fire's by inputing the doodler's |
| el17m2h | 27:af14fcd5a520 | 62 | previous position and updated position, as well as the pad's input. Finally, it calls a function to check if the doodler |
| el17m2h | 27:af14fcd5a520 | 63 | has reached the bottom of the screen to end the game. |
| el17m2h | 24:67dc71a8f009 | 64 | */ |
| el17m2h | 5:8814d6de77d0 | 65 | void update(Gamepad &pad); |
| el17m2h | 29:15e9640646b7 | 66 | |
| el17m2h | 29:15e9640646b7 | 67 | /** |
| el17m2h | 26:d16a5b1e0ace | 68 | @brief Checks a collision and sets the doodler's velocity (if it jumps) direction |
| el17m2h | 29:15e9640646b7 | 69 | @param Gamepad &pad to make a sound each time the doodler jumps |
| el17m2h | 26:d16a5b1e0ace | 70 | @param float doodler_pos_x |
| el17m2h | 26:d16a5b1e0ace | 71 | @param float doodler_pos_y |
| el17m2h | 26:d16a5b1e0ace | 72 | @param double doodler_vel_y |
| el17m2h | 29:15e9640646b7 | 73 | @details Checks a collision by comparing the doodler's position with the floors' ones with 3 conditions: |
| el17m2h | 29:15e9640646b7 | 74 | if the doodler is moving downwards (its velocity is positive), if the doodler's x-coordinate is within the |
| el17m2h | 29:15e9640646b7 | 75 | floor's x-coordinate and if the floors' y-coordinate is equal or +/-1 bits of the doodler's feet. |
| el17m2h | 29:15e9640646b7 | 76 | If the conditions are met, it means there is a collision, which means the doodler's velocity will change direction |
| el17m2h | 29:15e9640646b7 | 77 | (will be set to a negative value). The function also checks the doodler's velocity function to update the velocity |
| el17m2h | 26:d16a5b1e0ace | 78 | accordingly (make it increase or decrease by multiplying it with constant values) so that the doodler accelerates |
| el17m2h | 29:15e9640646b7 | 79 | or decelerates according to its current velocity. |
| el17m2h | 24:67dc71a8f009 | 80 | */ |
| el17m2h | 29:15e9640646b7 | 81 | void check_floors_collision(Gamepad &pad, float doodler_pos_x, float doodler_pos_y, double doodler_vel_y); |
| el17m2h | 29:15e9640646b7 | 82 | |
| el17m2h | 29:15e9640646b7 | 83 | /** |
| el17m2h | 26:d16a5b1e0ace | 84 | @brief Checks if the doodler fires |
| el17m2h | 26:d16a5b1e0ace | 85 | @param Gamepad &pad |
| el17m2h | 26:d16a5b1e0ace | 86 | @param float updated_doodler_pos_x |
| el17m2h | 26:d16a5b1e0ace | 87 | @param float updated_doodler_pos_y |
| el17m2h | 26:d16a5b1e0ace | 88 | @param float doodler_pos_y |
| el17m2h | 29:15e9640646b7 | 89 | @details The function checks the bullet's position and the input of the user (if the |
| el17m2h | 29:15e9640646b7 | 90 | Y button has been pressed). If the bullet is not currently fired (its position |
| el17m2h | 29:15e9640646b7 | 91 | equals the current doodler's trunk position) and the button Y is pressed, then the bullet is |
| el17m2h | 26:d16a5b1e0ace | 92 | updated to a new position (upwards) and the pad makes a sound indicating it is shooting. If |
| el17m2h | 29:15e9640646b7 | 93 | it is currently above the doodler's position, it means it has been fired, so continues being |
| el17m2h | 29:15e9640646b7 | 94 | updated. The update function will eventually check if the bullet has reached the end top of the |
| el17m2h | 26:d16a5b1e0ace | 95 | screen, which will make the bullet return to the doodler's trunk's position. If this is the case |
| el17m2h | 29:15e9640646b7 | 96 | and the button Y has not been pressed, the bullet remains in the doodler's position (by inputing |
| el17m2h | 26:d16a5b1e0ace | 97 | the doodler's position to the initialize function instead of the update one). |
| el17m2h | 24:67dc71a8f009 | 98 | */ |
| el17m2h | 23:9be87557b89a | 99 | void check_fire(Gamepad &pad, float updated_doodler_pos_x, float updated_doodler_pos_y, float doodler_pos_y); |
| el17m2h | 29:15e9640646b7 | 100 | |
| el17m2h | 29:15e9640646b7 | 101 | /** |
| el17m2h | 26:d16a5b1e0ace | 102 | @brief Increases score by checking the position of the floor |
| el17m2h | 29:15e9640646b7 | 103 | @details The function uses a foor loop to check the current position of every floor and |
| el17m2h | 29:15e9640646b7 | 104 | if they have reached the top of the screen (meaning they have re-appeared at the top, the |
| el17m2h | 26:d16a5b1e0ace | 105 | score will have one point added. This means that the score depends on how many floors re-appear, |
| el17m2h | 29:15e9640646b7 | 106 | which represents how high the doodler has moved upwards. |
| el17m2h | 24:67dc71a8f009 | 107 | */ |
| el17m2h | 21:6b16ca9834e6 | 108 | void add_score(); |
| el17m2h | 24:67dc71a8f009 | 109 | |
| el17m2h | 29:15e9640646b7 | 110 | |
| el17m2h | 29:15e9640646b7 | 111 | void subtract_score(); |
| el17m2h | 29:15e9640646b7 | 112 | |
| el17m2h | 29:15e9640646b7 | 113 | |
| el17m2h | 29:15e9640646b7 | 114 | bool get_score_check(); |
| el17m2h | 29:15e9640646b7 | 115 | |
| el17m2h | 29:15e9640646b7 | 116 | |
| el17m2h | 29:15e9640646b7 | 117 | /** |
| el17m2h | 29:15e9640646b7 | 118 | @brief Returns the current score |
| el17m2h | 29:15e9640646b7 | 119 | @details The function returns the current score of the game. It is called in the main.cpp to display |
| el17m2h | 29:15e9640646b7 | 120 | the final score. |
| el17m2h | 29:15e9640646b7 | 121 | */ |
| el17m2h | 28:e0161a52a8b9 | 122 | int get_score(); |
| el17m2h | 28:e0161a52a8b9 | 123 | |
| el17m2h | 29:15e9640646b7 | 124 | /** |
| el17m2h | 29:15e9640646b7 | 125 | @brief Sets the decision statement to end the game depending on the doodler's y-coordinate position |
| el17m2h | 29:15e9640646b7 | 126 | @param float doodler_pos_y |
| el17m2h | 29:15e9640646b7 | 127 | @details The function sets the decision statement to end the game by using an if statement dependent on |
| el17m2h | 29:15e9640646b7 | 128 | the read doodler's position. If the position has reached the bottom of the screen, the game should end, so |
| el17m2h | 29:15e9640646b7 | 129 | the decision will be true. |
| el17m2h | 29:15e9640646b7 | 130 | */ |
| el17m2h | 27:af14fcd5a520 | 131 | void set_game_over(float doodler_pos_y); |
| el17m2h | 29:15e9640646b7 | 132 | |
| el17m2h | 29:15e9640646b7 | 133 | /** |
| el17m2h | 29:15e9640646b7 | 134 | @brief Returns a true/false statement for ending the game or not |
| el17m2h | 29:15e9640646b7 | 135 | @details The function returns a bool type decision true/false, which will be called on the main.cpp |
| el17m2h | 29:15e9640646b7 | 136 | to decide if the game should end or not. |
| el17m2h | 29:15e9640646b7 | 137 | */ |
| el17m2h | 27:af14fcd5a520 | 138 | bool get_game_over(); |
| el17m2h | 29:15e9640646b7 | 139 | |
| el17m2h | 19:5a7b0cdf013b | 140 | private: |
| el17m2h | 18:9f40a2f8c2c5 | 141 | Bullet _b; |
| el17m2h | 19:5a7b0cdf013b | 142 | float _b_pos_x; |
| el17m2h | 19:5a7b0cdf013b | 143 | float _b_pos_y; |
| el17m2h | 29:15e9640646b7 | 144 | |
| el17m2h | 21:6b16ca9834e6 | 145 | int _score; |
| el17m2h | 29:15e9640646b7 | 146 | bool _score_check; |
| el17m2h | 31:5c4acae51026 | 147 | bool enemy_collision[6]; |
| el17m2h | 27:af14fcd5a520 | 148 | bool game_ends; |
| el17m2h | 19:5a7b0cdf013b | 149 | |
| el17m2h | 23:9be87557b89a | 150 | Floors _f[6]; // array of 6 floors |
| el17m2h | 29:15e9640646b7 | 151 | |
| el17m2h | 2:360a6c301a4e | 152 | int _floors_height; |
| el17m2h | 2:360a6c301a4e | 153 | int _floors_width; |
| el17m2h | 29:15e9640646b7 | 154 | |
| el17m2h | 2:360a6c301a4e | 155 | // x and y positions of the floors |
| el17m2h | 23:9be87557b89a | 156 | Vector2D _f_pos[6]; // |
| el17m2h | 29:15e9640646b7 | 157 | |
| el17m2h | 5:8814d6de77d0 | 158 | Doodler _dood; |
| el17m2h | 24:67dc71a8f009 | 159 | float doodler_pos_x; |
| el17m2h | 24:67dc71a8f009 | 160 | float doodler_pos_y; |
| el17m2h | 24:67dc71a8f009 | 161 | float updated_doodler_pos_x; |
| el17m2h | 24:67dc71a8f009 | 162 | float updated_doodler_pos_y; |
| el17m2h | 24:67dc71a8f009 | 163 | float doodler_vel_x; |
| el17m2h | 29:15e9640646b7 | 164 | double doodler_vel_y; |
| el17m2h | 29:15e9640646b7 | 165 | |
| el17m2h | 19:5a7b0cdf013b | 166 | int _screen_pos_x; |
| el17m2h | 19:5a7b0cdf013b | 167 | int _screen_pos_y; |
| el17m2h | 19:5a7b0cdf013b | 168 | int _screen_width; |
| el17m2h | 19:5a7b0cdf013b | 169 | int _screen_height; |
| el17m2h | 29:15e9640646b7 | 170 | |
| el17m2h | 5:8814d6de77d0 | 171 | Direction _d; |
| el17m2h | 5:8814d6de77d0 | 172 | float _mag; |
| el17m2h | 16:e0542761fc8c | 173 | bool _button_A; |
| el17m2h | 16:e0542761fc8c | 174 | bool _button_B; |
| el17m2h | 16:e0542761fc8c | 175 | bool _button_X; |
| el17m2h | 16:e0542761fc8c | 176 | bool _button_Y; |
| el17m2h | 2:360a6c301a4e | 177 | }; |
| el17m2h | 2:360a6c301a4e | 178 | #endif |