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

Committer:
Nathanj94
Date:
Thu May 04 12:24:25 2017 +0000
Revision:
18:99ccfa1bb2ca
Parent:
16:dae98691cc0e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nathanj94 0:8d02400f792a 1 #include "Catch_Model.h"
Nathanj94 0:8d02400f792a 2
Nathanj94 0:8d02400f792a 3 Catch_Model::Catch_Model()
Nathanj94 0:8d02400f792a 4 {
Nathanj94 0:8d02400f792a 5
Nathanj94 0:8d02400f792a 6 }
Nathanj94 0:8d02400f792a 7
Nathanj94 0:8d02400f792a 8 Catch_Model::~Catch_Model()
Nathanj94 0:8d02400f792a 9 {
Nathanj94 0:8d02400f792a 10
Nathanj94 0:8d02400f792a 11 }
Nathanj94 13:ae2dac4ab786 12 //INITILISATION FUNCTIONS//
Nathanj94 14:6764bb61d413 13 //Sets values used to initialise Basket and Objects, sets number of lives
Nathanj94 4:84e29254b988 14 void Catch_Model::init(int basket_y, int basket_width, int objects_speed, int lives)
Nathanj94 0:8d02400f792a 15 {
Nathanj94 0:8d02400f792a 16 _basket_y = basket_y;
Nathanj94 0:8d02400f792a 17 _basket_width = basket_width;
Nathanj94 1:43fbcc3584d6 18 _objects_speed = objects_speed;
Nathanj94 4:84e29254b988 19 _lives = lives;
Nathanj94 14:6764bb61d413 20 _delay = 1; //if this equals 1, check_a and check_b can be called
Nathanj94 1:43fbcc3584d6 21
Nathanj94 4:84e29254b988 22 basket.init(_basket_y, _basket_width);
Nathanj94 4:84e29254b988 23 objects.init(_objects_speed);
Nathanj94 0:8d02400f792a 24 }
Nathanj94 0:8d02400f792a 25
Nathanj94 14:6764bb61d413 26 //UPDATE FUNCTIONS//
Nathanj94 14:6764bb61d413 27 //Read input of joystick and assign the direction and magnitude to variables
Nathanj94 0:8d02400f792a 28 void Catch_Model::input(Gamepad &pad)
Nathanj94 0:8d02400f792a 29 {
Nathanj94 0:8d02400f792a 30 _d = pad.get_direction();
Nathanj94 0:8d02400f792a 31 _mag = pad.get_mag();
Nathanj94 0:8d02400f792a 32 }
Nathanj94 0:8d02400f792a 33
Nathanj94 14:6764bb61d413 34 //Update the game score/lives and move the basket/objects
Nathanj94 3:fc9133faec7a 35 void Catch_Model::update(N5110 &lcd, Gamepad &pad)
Nathanj94 0:8d02400f792a 36 {
Nathanj94 14:6764bb61d413 37 check_basket_catch(lcd, pad); //check for basket catch/miss before updating positions
Nathanj94 14:6764bb61d413 38 check_basket_miss(lcd, pad); //as that aspect must take priority
Nathanj94 3:fc9133faec7a 39
Nathanj94 10:6605cd9d374b 40 basket.move_stick(_d, _mag);
Nathanj94 10:6605cd9d374b 41 basket.move_LR(pad);
Nathanj94 12:f7d6003e5c6b 42 objects.move(_objects_speed);
Nathanj94 0:8d02400f792a 43 }
Nathanj94 0:8d02400f792a 44
Nathanj94 13:ae2dac4ab786 45 //GAME RULES FUNCTIONS//
Nathanj94 14:6764bb61d413 46 //Check if the object is caught
Nathanj94 3:fc9133faec7a 47 void Catch_Model::check_basket_catch(N5110 &lcd, Gamepad &pad)
Nathanj94 2:8410e09b77aa 48 {
Nathanj94 4:84e29254b988 49 int b_x_pos = basket.get_x();
Nathanj94 4:84e29254b988 50 int b_y_pos = basket.get_y();
Nathanj94 4:84e29254b988 51 int o_x_pos = objects.get_x();
Nathanj94 4:84e29254b988 52 int o_y_pos = objects.get_y();
Nathanj94 14:6764bb61d413 53
Nathanj94 14:6764bb61d413 54 //object y greater than/equal to basket y...
Nathanj94 14:6764bb61d413 55 //...and object x greater than/equal to basket x...
Nathanj94 14:6764bb61d413 56 //...and object x less than/equal to basket x+6 to compensate for width of object beyond x+6
Nathanj94 3:fc9133faec7a 57 if(
Nathanj94 14:6764bb61d413 58 (o_y_pos >= b_y_pos) &&
Nathanj94 14:6764bb61d413 59 (o_x_pos >= b_x_pos) &&
Nathanj94 14:6764bb61d413 60 (o_x_pos <= (b_x_pos + 6))
Nathanj94 3:fc9133faec7a 61 ) {
Nathanj94 8:db24c475f64f 62 pad.tone(1000, 0.2);
Nathanj94 14:6764bb61d413 63 objects.undraw(lcd); //FILL_WHITE so object disappears
Nathanj94 14:6764bb61d413 64 add_score(); //object has a variable that adds correct amount of score
Nathanj94 14:6764bb61d413 65 objects.init(_objects_speed); //drop another object upon contact
Nathanj94 16:dae98691cc0e 66
Nathanj94 16:dae98691cc0e 67 //printf("basket catch\n");
Nathanj94 3:fc9133faec7a 68 }
Nathanj94 2:8410e09b77aa 69 }
Nathanj94 2:8410e09b77aa 70
Nathanj94 14:6764bb61d413 71 //Check if the object is not caught
Nathanj94 3:fc9133faec7a 72 void Catch_Model::check_basket_miss(N5110 &lcd, Gamepad &pad)
Nathanj94 2:8410e09b77aa 73 {
Nathanj94 14:6764bb61d413 74 int b_y_pos = basket.get_y(); //x co-ordinates unnecessary
Nathanj94 14:6764bb61d413 75 int o_y_pos = objects.get_y(); //if x does not satisfy check_basket_catch, it will satisfy this
Nathanj94 3:fc9133faec7a 76
Nathanj94 6:61bcf98e0a88 77 int score_var;
Nathanj94 14:6764bb61d413 78 score_var = objects.get_score_var();
Nathanj94 6:61bcf98e0a88 79
Nathanj94 3:fc9133faec7a 80 if (o_y_pos > b_y_pos) {
Nathanj94 4:84e29254b988 81 objects.undraw(lcd);
Nathanj94 14:6764bb61d413 82 if (score_var != 5) { //if antifruit misses do nothing, otherwise reduce lives
Nathanj94 6:61bcf98e0a88 83 _lives--;
Nathanj94 14:6764bb61d413 84 }
Nathanj94 8:db24c475f64f 85 pad.tone(100, 0.2);
Nathanj94 14:6764bb61d413 86 objects.init(_objects_speed); //drop another object upon contact
Nathanj94 16:dae98691cc0e 87
Nathanj94 16:dae98691cc0e 88 //printf("basket miss\n");
Nathanj94 16:dae98691cc0e 89 }
Nathanj94 3:fc9133faec7a 90 }
Nathanj94 3:fc9133faec7a 91
Nathanj94 14:6764bb61d413 92 //Check which object is current and add appropriate score
Nathanj94 13:ae2dac4ab786 93 void Catch_Model::add_score()
Nathanj94 13:ae2dac4ab786 94 {
Nathanj94 13:ae2dac4ab786 95 int score_var;
Nathanj94 13:ae2dac4ab786 96 score_var = objects.get_score_var();
Nathanj94 13:ae2dac4ab786 97
Nathanj94 14:6764bb61d413 98 if (score_var == 1) { //strawberry = 1
Nathanj94 13:ae2dac4ab786 99 basket.add_score_1();
Nathanj94 14:6764bb61d413 100 } else if (score_var == 2) { //pineapple = 2
Nathanj94 13:ae2dac4ab786 101 basket.add_score_2();
Nathanj94 14:6764bb61d413 102 } else if (score_var == 3) { //pear = 5
Nathanj94 13:ae2dac4ab786 103 basket.add_score_5();
Nathanj94 14:6764bb61d413 104 } else if (score_var == 4) { //melon = 10
Nathanj94 13:ae2dac4ab786 105 basket.add_score_10();
Nathanj94 14:6764bb61d413 106 } else { //antifruit - if caught, reduce lives instead of add score
Nathanj94 13:ae2dac4ab786 107 _lives--;
Nathanj94 13:ae2dac4ab786 108 }
Nathanj94 16:dae98691cc0e 109 //printf("add score\n");
Nathanj94 13:ae2dac4ab786 110 }
Nathanj94 13:ae2dac4ab786 111
Nathanj94 14:6764bb61d413 112 //Return number of lives
Nathanj94 13:ae2dac4ab786 113 int Catch_Model::get_lives()
Nathanj94 13:ae2dac4ab786 114 {
Nathanj94 13:ae2dac4ab786 115 return _lives;
Nathanj94 13:ae2dac4ab786 116 }
Nathanj94 13:ae2dac4ab786 117
Nathanj94 13:ae2dac4ab786 118 //BUTTON FUNCTIONS//
Nathanj94 14:6764bb61d413 119 //If A is pressed, reset the object
Nathanj94 8:db24c475f64f 120 void Catch_Model::check_a(N5110 &lcd, Gamepad &pad)
Nathanj94 7:ec6dc66ee196 121 {
Nathanj94 7:ec6dc66ee196 122 if (
Nathanj94 7:ec6dc66ee196 123 (pad.check_event(Gamepad::A_PRESSED) == true) &&
Nathanj94 14:6764bb61d413 124 (_delay == 1) //delay stops powerups from being over-used
Nathanj94 7:ec6dc66ee196 125 ) {
Nathanj94 9:902b67101cdc 126 objects.undraw(lcd);
Nathanj94 9:902b67101cdc 127 objects.init(_objects_speed);
Nathanj94 14:6764bb61d413 128
Nathanj94 14:6764bb61d413 129 //while _delay = 0, A and B can't be used...
Nathanj94 14:6764bb61d413 130 //...in 10 seconds _delay = 1, A and B can be used again
Nathanj94 14:6764bb61d413 131 _delay = 0;
Nathanj94 14:6764bb61d413 132 timeout.attach(this, &Catch_Model::set_delay, 10.0);
Nathanj94 16:dae98691cc0e 133
Nathanj94 16:dae98691cc0e 134 //printf("A pressed\n");
Nathanj94 14:6764bb61d413 135 }
Nathanj94 14:6764bb61d413 136
Nathanj94 14:6764bb61d413 137 }
Nathanj94 14:6764bb61d413 138
Nathanj94 14:6764bb61d413 139 //If B is pressed, give another life
Nathanj94 14:6764bb61d413 140 void Catch_Model::check_b(N5110 &lcd, Gamepad &pad)
Nathanj94 14:6764bb61d413 141 {
Nathanj94 14:6764bb61d413 142 if (
Nathanj94 14:6764bb61d413 143 (pad.check_event(Gamepad::B_PRESSED) == true) &&
Nathanj94 14:6764bb61d413 144 (_delay == 1) //delay stops powerups from being over-used
Nathanj94 14:6764bb61d413 145 ) {
Nathanj94 14:6764bb61d413 146 _lives++;
Nathanj94 14:6764bb61d413 147
Nathanj94 14:6764bb61d413 148 //while _delay = 0, A and B can't be used...
Nathanj94 14:6764bb61d413 149 //...in 10 seconds _delay = 1, A and B can be used again
Nathanj94 9:902b67101cdc 150 _delay = 0;
Nathanj94 9:902b67101cdc 151 timeout.attach(this, &Catch_Model::set_delay, 10.0);
Nathanj94 16:dae98691cc0e 152
Nathanj94 16:dae98691cc0e 153 //printf("B pressed\n");
Nathanj94 8:db24c475f64f 154 }
Nathanj94 8:db24c475f64f 155
Nathanj94 8:db24c475f64f 156 }
Nathanj94 8:db24c475f64f 157
Nathanj94 14:6764bb61d413 158 //10 seconds after A or B is pressed, _delay = 1
Nathanj94 7:ec6dc66ee196 159 void Catch_Model::set_delay()
Nathanj94 7:ec6dc66ee196 160 {
Nathanj94 7:ec6dc66ee196 161 _delay = 1;
Nathanj94 7:ec6dc66ee196 162 }
Nathanj94 8:db24c475f64f 163
Nathanj94 13:ae2dac4ab786 164 //DISPLAY FUNCTIONS//
Nathanj94 14:6764bb61d413 165 //Re-draw the screen after updates to positions, scores etc are made
Nathanj94 15:1a0bd800f1f1 166 void Catch_Model::draw(N5110 &lcd, Gamepad &pad)
Nathanj94 5:7db3e43e5aca 167 {
Nathanj94 13:ae2dac4ab786 168 basket.draw(lcd);
Nathanj94 13:ae2dac4ab786 169 objects.draw(lcd);
Nathanj94 13:ae2dac4ab786 170 print_score(lcd);
Nathanj94 13:ae2dac4ab786 171 print_lives(lcd);
Nathanj94 13:ae2dac4ab786 172 print_delay(lcd);
Nathanj94 15:1a0bd800f1f1 173
Nathanj94 15:1a0bd800f1f1 174 if(get_lives() == 0)
Nathanj94 15:1a0bd800f1f1 175 {
Nathanj94 15:1a0bd800f1f1 176 do{
Nathanj94 15:1a0bd800f1f1 177 lcd.clear();
Nathanj94 15:1a0bd800f1f1 178 final_score(lcd);
Nathanj94 15:1a0bd800f1f1 179 } while (pad.check_event(Gamepad::START_PRESSED) == false);
Nathanj94 15:1a0bd800f1f1 180 }
Nathanj94 4:84e29254b988 181 }
Nathanj94 4:84e29254b988 182
Nathanj94 14:6764bb61d413 183 //Get the number of lives and print on the display
Nathanj94 4:84e29254b988 184 void Catch_Model::print_lives(N5110 &lcd)
Nathanj94 4:84e29254b988 185 {
Nathanj94 4:84e29254b988 186 char buffer[14];
Nathanj94 4:84e29254b988 187 int lives = get_lives();
Nathanj94 4:84e29254b988 188
Nathanj94 4:84e29254b988 189 int print_lives = sprintf(buffer, "LIVES:%d", lives);
Nathanj94 4:84e29254b988 190 if (print_lives <= 14) {
Nathanj94 4:84e29254b988 191 lcd.printString(buffer,0,0);
Nathanj94 4:84e29254b988 192 lcd.refresh();
Nathanj94 4:84e29254b988 193 }
Nathanj94 4:84e29254b988 194 }
Nathanj94 4:84e29254b988 195
Nathanj94 14:6764bb61d413 196 //Get the score and print on the display
Nathanj94 3:fc9133faec7a 197 void Catch_Model::print_score(N5110 &lcd)
Nathanj94 3:fc9133faec7a 198 {
Nathanj94 3:fc9133faec7a 199 char buffer[14];
Nathanj94 4:84e29254b988 200 int score = basket.get_score();
Nathanj94 2:8410e09b77aa 201
Nathanj94 4:84e29254b988 202 int print_score;
Nathanj94 4:84e29254b988 203
Nathanj94 14:6764bb61d413 204 if ((score == 0)||(score <= 9)) { //if loop keeps the length of the string fixed...
Nathanj94 4:84e29254b988 205 print_score = sprintf(buffer, "000%d", score);
Nathanj94 4:84e29254b988 206 } else if (score <= 99) {
Nathanj94 4:84e29254b988 207 print_score = sprintf(buffer, "00%2d", score);
Nathanj94 4:84e29254b988 208 } else if (score <= 999 ) {
Nathanj94 4:84e29254b988 209 print_score = sprintf(buffer, "0%3d", score);
Nathanj94 4:84e29254b988 210 } else {
Nathanj94 4:84e29254b988 211 print_score = sprintf(buffer, "%4d", score);
Nathanj94 14:6764bb61d413 212 } //...with maximum printable score of 9999
Nathanj94 4:84e29254b988 213
Nathanj94 3:fc9133faec7a 214 if (print_score <= 14) {
Nathanj94 4:84e29254b988 215 lcd.printString(buffer,59,0);
Nathanj94 3:fc9133faec7a 216 lcd.refresh();
Nathanj94 3:fc9133faec7a 217 }
Nathanj94 3:fc9133faec7a 218 }
Nathanj94 14:6764bb61d413 219
Nathanj94 15:1a0bd800f1f1 220 void Catch_Model::final_score(N5110 &lcd)
Nathanj94 15:1a0bd800f1f1 221 {
Nathanj94 15:1a0bd800f1f1 222 char buffer[14];
Nathanj94 15:1a0bd800f1f1 223 int score = basket.get_score();
Nathanj94 15:1a0bd800f1f1 224
Nathanj94 15:1a0bd800f1f1 225 int print_score;
Nathanj94 15:1a0bd800f1f1 226
Nathanj94 15:1a0bd800f1f1 227 if ((score == 0)||(score <= 9)) { //if loop keeps the length of the string fixed...
Nathanj94 15:1a0bd800f1f1 228 print_score = sprintf(buffer, "000%d", score);
Nathanj94 15:1a0bd800f1f1 229 } else if (score <= 99) {
Nathanj94 15:1a0bd800f1f1 230 print_score = sprintf(buffer, "00%2d", score);
Nathanj94 15:1a0bd800f1f1 231 } else if (score <= 999 ) {
Nathanj94 15:1a0bd800f1f1 232 print_score = sprintf(buffer, "0%3d", score);
Nathanj94 15:1a0bd800f1f1 233 } else {
Nathanj94 15:1a0bd800f1f1 234 print_score = sprintf(buffer, "%4d", score);
Nathanj94 15:1a0bd800f1f1 235 } //...with maximum printable score of 9999
Nathanj94 15:1a0bd800f1f1 236
Nathanj94 15:1a0bd800f1f1 237 lcd.printString(" GAME OVER ",0,2);
Nathanj94 15:1a0bd800f1f1 238 if (print_score <= 14) {
Nathanj94 15:1a0bd800f1f1 239 lcd.printString(buffer,34,3);
Nathanj94 15:1a0bd800f1f1 240 lcd.refresh();
Nathanj94 15:1a0bd800f1f1 241 }
Nathanj94 15:1a0bd800f1f1 242 }
Nathanj94 15:1a0bd800f1f1 243
Nathanj94 14:6764bb61d413 244 //Print a tick if _delay = 1, otherwise print a cross
Nathanj94 8:db24c475f64f 245 void Catch_Model::print_delay(N5110 &lcd)
Nathanj94 8:db24c475f64f 246 {
Nathanj94 8:db24c475f64f 247 if (_delay == 1) {
Nathanj94 8:db24c475f64f 248 lcd.drawLine(46,4,50,8,1);
Nathanj94 8:db24c475f64f 249 lcd.drawLine(50,8,54,0,1);
Nathanj94 8:db24c475f64f 250 } else {
Nathanj94 8:db24c475f64f 251 lcd.drawLine(46,0,54,8,1);
Nathanj94 8:db24c475f64f 252 lcd.drawLine(46,8,54,0,1);
Nathanj94 13:ae2dac4ab786 253 }
Nathanj94 8:db24c475f64f 254 }