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