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