Uses accompanying Basket, Objects and Fruit libraries to create Fruit Basket game. If an object is caught, points are added; if an object in missed, a 'life' is lost.
Dependents: Game_Controller_Project
Catch_Model.h@18:99ccfa1bb2ca, 2017-05-04 (annotated)
- Committer:
- Nathanj94
- Date:
- Thu May 04 12:24:25 2017 +0000
- Revision:
- 18:99ccfa1bb2ca
- Parent:
- 17:20abf995c040
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nathanj94 | 0:8d02400f792a | 1 | #ifndef CATCH_MODEL_H |
Nathanj94 | 0:8d02400f792a | 2 | #define CATCH_MODEL_H |
Nathanj94 | 0:8d02400f792a | 3 | |
Nathanj94 | 0:8d02400f792a | 4 | #include "mbed.h" |
Nathanj94 | 0:8d02400f792a | 5 | #include "N5110.h" |
Nathanj94 | 0:8d02400f792a | 6 | #include "Gamepad.h" |
Nathanj94 | 0:8d02400f792a | 7 | #include "Basket.h" |
Nathanj94 | 1:43fbcc3584d6 | 8 | #include "Objects.h" |
Nathanj94 | 0:8d02400f792a | 9 | |
Nathanj94 | 18:99ccfa1bb2ca | 10 | /** Catch_Model Class |
Nathanj94 | 18:99ccfa1bb2ca | 11 | @brief Class brings together Basket, Objects and Fruit to create 'Fruit Basket' |
Nathanj94 | 18:99ccfa1bb2ca | 12 | @brief game. This class also uses variables from the other classes to display |
Nathanj94 | 18:99ccfa1bb2ca | 13 | @brief information at the top of the screen i.e. score, remaining lives and an |
Nathanj94 | 18:99ccfa1bb2ca | 14 | @brief indicator showing if the player can use one of the available 'power-ups'. |
Nathanj94 | 18:99ccfa1bb2ca | 15 | |
Nathanj94 | 18:99ccfa1bb2ca | 16 | @author Nathan Johnston |
Nathanj94 | 18:99ccfa1bb2ca | 17 | @date 14th March 2017 |
Nathanj94 | 18:99ccfa1bb2ca | 18 | */ |
Nathanj94 | 18:99ccfa1bb2ca | 19 | |
Nathanj94 | 0:8d02400f792a | 20 | class Catch_Model |
Nathanj94 | 0:8d02400f792a | 21 | { |
Nathanj94 | 14:6764bb61d413 | 22 | public: |
Nathanj94 | 14:6764bb61d413 | 23 | |
Nathanj94 | 0:8d02400f792a | 24 | Catch_Model(); |
Nathanj94 | 0:8d02400f792a | 25 | ~Catch_Model(); |
Nathanj94 | 14:6764bb61d413 | 26 | |
Nathanj94 | 13:ae2dac4ab786 | 27 | //INITILISATION FUNCTIONS// |
Nathanj94 | 14:6764bb61d413 | 28 | |
Nathanj94 | 14:6764bb61d413 | 29 | /** Initialise Game |
Nathanj94 | 17:20abf995c040 | 30 | * |
Nathanj94 | 17:20abf995c040 | 31 | * Main initialisation function. The parameters are used to call |
Nathanj94 | 17:20abf995c040 | 32 | * Basket::init(int y, int width) and Objects::init(int speed), as well |
Nathanj94 | 17:20abf995c040 | 33 | * as set the number of lives/misses the player will be allowed and set |
Nathanj94 | 17:20abf995c040 | 34 | * the default value of a variable that restricts how often the player |
Nathanj94 | 17:20abf995c040 | 35 | * can use the A and B buttons. |
Nathanj94 | 17:20abf995c040 | 36 | * @param basket_y - y co-ordinate of the basket (0 to 47) |
Nathanj94 | 17:20abf995c040 | 37 | * @param basket_width - width of the basket (0 to 83) |
Nathanj94 | 17:20abf995c040 | 38 | * @param objects_speed - "fall" speed of the object (2,3,4,5) |
Nathanj94 | 17:20abf995c040 | 39 | * @param lives - number of lives/misses the player has |
Nathanj94 | 17:20abf995c040 | 40 | */ |
Nathanj94 | 4:84e29254b988 | 41 | void init(int basket_y, int basket_width, int objects_speed, int lives); |
Nathanj94 | 14:6764bb61d413 | 42 | |
Nathanj94 | 14:6764bb61d413 | 43 | |
Nathanj94 | 14:6764bb61d413 | 44 | //UPDATE FUNCTIONS// |
Nathanj94 | 14:6764bb61d413 | 45 | |
Nathanj94 | 14:6764bb61d413 | 46 | /** Read Joystick Input |
Nathanj94 | 17:20abf995c040 | 47 | * |
Nathanj94 | 17:20abf995c040 | 48 | * Read the direction and magnitude of the joystick and assign |
Nathanj94 | 17:20abf995c040 | 49 | * the values to variables by calling functions from the Gamepad |
Nathanj94 | 17:20abf995c040 | 50 | * library. |
Nathanj94 | 17:20abf995c040 | 51 | * @param pad - Gamepad custom library |
Nathanj94 | 17:20abf995c040 | 52 | */ |
Nathanj94 | 0:8d02400f792a | 53 | void input(Gamepad &pad); |
Nathanj94 | 14:6764bb61d413 | 54 | |
Nathanj94 | 14:6764bb61d413 | 55 | /** Update Game |
Nathanj94 | 17:20abf995c040 | 56 | * |
Nathanj94 | 17:20abf995c040 | 57 | * Checks if an object is caught by the basket or not then increases |
Nathanj94 | 17:20abf995c040 | 58 | * score or reduces lives appropriately. Moves the basket with either |
Nathanj94 | 17:20abf995c040 | 59 | * joystick or buttons and moves the objects too. |
Nathanj94 | 17:20abf995c040 | 60 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 61 | * @param pad - Gamepad custom library |
Nathanj94 | 17:20abf995c040 | 62 | */ |
Nathanj94 | 3:fc9133faec7a | 63 | void update(N5110 &lcd, Gamepad &pad); |
Nathanj94 | 14:6764bb61d413 | 64 | |
Nathanj94 | 14:6764bb61d413 | 65 | |
Nathanj94 | 13:ae2dac4ab786 | 66 | //GAME RULES FUNCTIONS// |
Nathanj94 | 14:6764bb61d413 | 67 | |
Nathanj94 | 14:6764bb61d413 | 68 | /** Check Object has been Caught |
Nathanj94 | 17:20abf995c040 | 69 | * |
Nathanj94 | 17:20abf995c040 | 70 | * Gets the x and y reference co-ordinates of the basket and the |
Nathanj94 | 17:20abf995c040 | 71 | * falling object from their respective libraries; if the x co-ordinate |
Nathanj94 | 17:20abf995c040 | 72 | * of the object is within the width of the basket and the y co-ordinate |
Nathanj94 | 17:20abf995c040 | 73 | * of the object is greater than that of the basket the object will be |
Nathanj94 | 17:20abf995c040 | 74 | * undrawn, the appropriate score will be added and the object will be |
Nathanj94 | 17:20abf995c040 | 75 | * re-initialised. |
Nathanj94 | 17:20abf995c040 | 76 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 77 | * @param pad - Gamepad custom library |
Nathanj94 | 17:20abf995c040 | 78 | */ |
Nathanj94 | 3:fc9133faec7a | 79 | void check_basket_catch(N5110 &lcd, Gamepad &pad); |
Nathanj94 | 14:6764bb61d413 | 80 | |
Nathanj94 | 14:6764bb61d413 | 81 | /** Check Object has Missed |
Nathanj94 | 17:20abf995c040 | 82 | * |
Nathanj94 | 17:20abf995c040 | 83 | * Gets the x and y reference co-ordinates of the basket and the |
Nathanj94 | 17:20abf995c040 | 84 | * falling object from their respective libraries; if the x co-ordinate |
Nathanj94 | 17:20abf995c040 | 85 | * of the object is outside the width of the basket and the y co-ordinate |
Nathanj94 | 17:20abf995c040 | 86 | * of the object is greater than that of the basket the object will be |
Nathanj94 | 17:20abf995c040 | 87 | * undrawn, the number of lives will be reduced and the object will be |
Nathanj94 | 17:20abf995c040 | 88 | * re-initialised. |
Nathanj94 | 17:20abf995c040 | 89 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 90 | * @param pad - Gamepad custom library |
Nathanj94 | 17:20abf995c040 | 91 | */ |
Nathanj94 | 3:fc9133faec7a | 92 | void check_basket_miss(N5110 &lcd, Gamepad &pad); |
Nathanj94 | 14:6764bb61d413 | 93 | |
Nathanj94 | 14:6764bb61d413 | 94 | /** Add Correct Score |
Nathanj94 | 17:20abf995c040 | 95 | * |
Nathanj94 | 17:20abf995c040 | 96 | * Gets the value of a variable from Objects that is specific to each |
Nathanj94 | 17:20abf995c040 | 97 | * object (type of fruit) and uses it to call the appropriate add_score |
Nathanj94 | 17:20abf995c040 | 98 | * function from Basket; increasing the score by 1, 2, 5 or 10. |
Nathanj94 | 17:20abf995c040 | 99 | */ |
Nathanj94 | 5:7db3e43e5aca | 100 | void add_score(); |
Nathanj94 | 14:6764bb61d413 | 101 | |
Nathanj94 | 14:6764bb61d413 | 102 | /** Get Lives |
Nathanj94 | 17:20abf995c040 | 103 | * |
Nathanj94 | 17:20abf995c040 | 104 | * Return the remaining number of lives available to the player. |
Nathanj94 | 17:20abf995c040 | 105 | */ |
Nathanj94 | 4:84e29254b988 | 106 | int get_lives(); |
Nathanj94 | 14:6764bb61d413 | 107 | |
Nathanj94 | 14:6764bb61d413 | 108 | |
Nathanj94 | 13:ae2dac4ab786 | 109 | //BUTTON FUNCTIONS// |
Nathanj94 | 14:6764bb61d413 | 110 | |
Nathanj94 | 14:6764bb61d413 | 111 | /** Check A Button |
Nathanj94 | 17:20abf995c040 | 112 | * |
Nathanj94 | 17:20abf995c040 | 113 | * If the A button is pressed, call Objects::undraw(N5110 &lcd) wherever |
Nathanj94 | 17:20abf995c040 | 114 | * it is on the display and call Objects::init(int speed) to give |
Nathanj94 | 17:20abf995c040 | 115 | * the player a second chance. Also set the value of a variable to zero |
Nathanj94 | 17:20abf995c040 | 116 | * so that the function cannot be recalled straight away. |
Nathanj94 | 17:20abf995c040 | 117 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 118 | * @param pad - Gamepad custom library |
Nathanj94 | 17:20abf995c040 | 119 | */ |
Nathanj94 | 13:ae2dac4ab786 | 120 | void check_a(N5110 &lcd, Gamepad &pad); |
Nathanj94 | 14:6764bb61d413 | 121 | |
Nathanj94 | 14:6764bb61d413 | 122 | /** Check B Button |
Nathanj94 | 17:20abf995c040 | 123 | * |
Nathanj94 | 17:20abf995c040 | 124 | * If the B button is pressed, increase number of lives by 1. Also set |
Nathanj94 | 17:20abf995c040 | 125 | * the value of a variable to zero so that the function cannot be |
Nathanj94 | 17:20abf995c040 | 126 | * recalled straight away. |
Nathanj94 | 17:20abf995c040 | 127 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 128 | * @param pad - Gamepad custom library |
Nathanj94 | 17:20abf995c040 | 129 | */ |
Nathanj94 | 13:ae2dac4ab786 | 130 | void check_b(N5110 &lcd, Gamepad &pad); |
Nathanj94 | 14:6764bb61d413 | 131 | |
Nathanj94 | 14:6764bb61d413 | 132 | /** Set Delay to ON |
Nathanj94 | 17:20abf995c040 | 133 | * |
Nathanj94 | 17:20abf995c040 | 134 | * 10 seconds after the buttons A or B are pressed this function is called |
Nathanj94 | 17:20abf995c040 | 135 | * resetting the value of a variable to 1 so the buttons can be used again. |
Nathanj94 | 17:20abf995c040 | 136 | */ |
Nathanj94 | 13:ae2dac4ab786 | 137 | void set_delay(); |
Nathanj94 | 14:6764bb61d413 | 138 | |
Nathanj94 | 14:6764bb61d413 | 139 | |
Nathanj94 | 14:6764bb61d413 | 140 | //DISPLAY FUNCTIONS// |
Nathanj94 | 13:ae2dac4ab786 | 141 | |
Nathanj94 | 14:6764bb61d413 | 142 | /** Draw All Features |
Nathanj94 | 17:20abf995c040 | 143 | * |
Nathanj94 | 17:20abf995c040 | 144 | * Calls draw functions from the Basket and Objects libraries as well |
Nathanj94 | 17:20abf995c040 | 145 | * as functions to display the score, remaining lives and an indicator |
Nathanj94 | 17:20abf995c040 | 146 | * that tells the player if the A and B buttons are ready to be used. |
Nathanj94 | 17:20abf995c040 | 147 | * Once the game is finished, a 'Game Over' screen is displayed showing |
Nathanj94 | 17:20abf995c040 | 148 | * the final score. |
Nathanj94 | 17:20abf995c040 | 149 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 150 | * @param pad - Gamepad custom library |
Nathanj94 | 17:20abf995c040 | 151 | */ |
Nathanj94 | 15:1a0bd800f1f1 | 152 | void draw(N5110 &lcd, Gamepad &pad); |
Nathanj94 | 14:6764bb61d413 | 153 | |
Nathanj94 | 14:6764bb61d413 | 154 | /** Display the Number of Lives |
Nathanj94 | 17:20abf995c040 | 155 | * |
Nathanj94 | 17:20abf995c040 | 156 | * Print a string of characters to the buffer, indicating how many lives |
Nathanj94 | 17:20abf995c040 | 157 | * remain. |
Nathanj94 | 17:20abf995c040 | 158 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 159 | */ |
Nathanj94 | 4:84e29254b988 | 160 | void print_lives(N5110 &lcd); |
Nathanj94 | 14:6764bb61d413 | 161 | |
Nathanj94 | 14:6764bb61d413 | 162 | /** Display the Score |
Nathanj94 | 17:20abf995c040 | 163 | * |
Nathanj94 | 17:20abf995c040 | 164 | * Print a string of characters to the buffer, indicating the score. |
Nathanj94 | 17:20abf995c040 | 165 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 166 | */ |
Nathanj94 | 3:fc9133faec7a | 167 | void print_score(N5110 &lcd); |
Nathanj94 | 14:6764bb61d413 | 168 | |
Nathanj94 | 15:1a0bd800f1f1 | 169 | /** Display the Final Score |
Nathanj94 | 17:20abf995c040 | 170 | * |
Nathanj94 | 17:20abf995c040 | 171 | * Print two strings of characters to the buffer, one indicating the |
Nathanj94 | 17:20abf995c040 | 172 | * score and another saying 'Game Over'. |
Nathanj94 | 17:20abf995c040 | 173 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 174 | */ |
Nathanj94 | 15:1a0bd800f1f1 | 175 | void final_score(N5110 &lcd); |
Nathanj94 | 15:1a0bd800f1f1 | 176 | |
Nathanj94 | 14:6764bb61d413 | 177 | /** Display the Powerup Indicator |
Nathanj94 | 17:20abf995c040 | 178 | * |
Nathanj94 | 17:20abf995c040 | 179 | * Print a tick or cross to the buffer, indicating whether or not |
Nathanj94 | 17:20abf995c040 | 180 | * the A and B buttons are ready to be used. |
Nathanj94 | 17:20abf995c040 | 181 | * @param lcd - N5110 custom library |
Nathanj94 | 17:20abf995c040 | 182 | */ |
Nathanj94 | 8:db24c475f64f | 183 | void print_delay(N5110 &lcd); |
Nathanj94 | 14:6764bb61d413 | 184 | |
Nathanj94 | 14:6764bb61d413 | 185 | private: |
Nathanj94 | 14:6764bb61d413 | 186 | |
Nathanj94 | 14:6764bb61d413 | 187 | //OBJECTS// |
Nathanj94 | 4:84e29254b988 | 188 | Basket basket; |
Nathanj94 | 4:84e29254b988 | 189 | Objects objects; |
Nathanj94 | 7:ec6dc66ee196 | 190 | Timeout timeout; |
Nathanj94 | 14:6764bb61d413 | 191 | |
Nathanj94 | 14:6764bb61d413 | 192 | //VARIABLES// |
Nathanj94 | 0:8d02400f792a | 193 | int _basket_y; |
Nathanj94 | 0:8d02400f792a | 194 | int _basket_width; |
Nathanj94 | 1:43fbcc3584d6 | 195 | int _objects_speed; |
Nathanj94 | 4:84e29254b988 | 196 | int _lives; |
Nathanj94 | 7:ec6dc66ee196 | 197 | int _delay; |
Nathanj94 | 14:6764bb61d413 | 198 | |
Nathanj94 | 14:6764bb61d413 | 199 | //JOYSTICK PARAMETERS// |
Nathanj94 | 0:8d02400f792a | 200 | Direction _d; |
Nathanj94 | 0:8d02400f792a | 201 | float _mag; |
Nathanj94 | 14:6764bb61d413 | 202 | |
Nathanj94 | 0:8d02400f792a | 203 | }; |
Nathanj94 | 0:8d02400f792a | 204 | #endif |