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 Apr 27 12:39:53 2017 +0000
Revision:
14:6764bb61d413
Parent:
13:ae2dac4ab786
Child:
15:1a0bd800f1f1
All documentation added

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 3:fc9133faec7a 66 }
Nathanj94 2:8410e09b77aa 67 }
Nathanj94 2:8410e09b77aa 68
Nathanj94 14:6764bb61d413 69 //Check if the object is not caught
Nathanj94 3:fc9133faec7a 70 void Catch_Model::check_basket_miss(N5110 &lcd, Gamepad &pad)
Nathanj94 2:8410e09b77aa 71 {
Nathanj94 14:6764bb61d413 72 int b_y_pos = basket.get_y(); //x co-ordinates unnecessary
Nathanj94 14:6764bb61d413 73 int o_y_pos = objects.get_y(); //if x does not satisfy check_basket_catch, it will satisfy this
Nathanj94 3:fc9133faec7a 74
Nathanj94 6:61bcf98e0a88 75 int score_var;
Nathanj94 14:6764bb61d413 76 score_var = objects.get_score_var();
Nathanj94 6:61bcf98e0a88 77
Nathanj94 3:fc9133faec7a 78 if (o_y_pos > b_y_pos) {
Nathanj94 4:84e29254b988 79 objects.undraw(lcd);
Nathanj94 14:6764bb61d413 80 if (score_var != 5) { //if antifruit misses do nothing, otherwise reduce lives
Nathanj94 6:61bcf98e0a88 81 _lives--;
Nathanj94 14:6764bb61d413 82 }
Nathanj94 8:db24c475f64f 83 pad.tone(100, 0.2);
Nathanj94 14:6764bb61d413 84 objects.init(_objects_speed); //drop another object upon contact
Nathanj94 9:902b67101cdc 85 }
Nathanj94 3:fc9133faec7a 86 }
Nathanj94 3:fc9133faec7a 87
Nathanj94 14:6764bb61d413 88 //Check which object is current and add appropriate score
Nathanj94 13:ae2dac4ab786 89 void Catch_Model::add_score()
Nathanj94 13:ae2dac4ab786 90 {
Nathanj94 13:ae2dac4ab786 91 int score_var;
Nathanj94 13:ae2dac4ab786 92 score_var = objects.get_score_var();
Nathanj94 13:ae2dac4ab786 93
Nathanj94 14:6764bb61d413 94 if (score_var == 1) { //strawberry = 1
Nathanj94 13:ae2dac4ab786 95 basket.add_score_1();
Nathanj94 14:6764bb61d413 96 } else if (score_var == 2) { //pineapple = 2
Nathanj94 13:ae2dac4ab786 97 basket.add_score_2();
Nathanj94 14:6764bb61d413 98 } else if (score_var == 3) { //pear = 5
Nathanj94 13:ae2dac4ab786 99 basket.add_score_5();
Nathanj94 14:6764bb61d413 100 } else if (score_var == 4) { //melon = 10
Nathanj94 13:ae2dac4ab786 101 basket.add_score_10();
Nathanj94 14:6764bb61d413 102 } else { //antifruit - if caught, reduce lives instead of add score
Nathanj94 13:ae2dac4ab786 103 _lives--;
Nathanj94 13:ae2dac4ab786 104 }
Nathanj94 13:ae2dac4ab786 105 }
Nathanj94 13:ae2dac4ab786 106
Nathanj94 14:6764bb61d413 107 //Return number of lives
Nathanj94 13:ae2dac4ab786 108 int Catch_Model::get_lives()
Nathanj94 13:ae2dac4ab786 109 {
Nathanj94 13:ae2dac4ab786 110 return _lives;
Nathanj94 13:ae2dac4ab786 111 }
Nathanj94 13:ae2dac4ab786 112
Nathanj94 13:ae2dac4ab786 113 //BUTTON FUNCTIONS//
Nathanj94 14:6764bb61d413 114 //If A is pressed, reset the object
Nathanj94 8:db24c475f64f 115 void Catch_Model::check_a(N5110 &lcd, Gamepad &pad)
Nathanj94 7:ec6dc66ee196 116 {
Nathanj94 7:ec6dc66ee196 117 if (
Nathanj94 7:ec6dc66ee196 118 (pad.check_event(Gamepad::A_PRESSED) == true) &&
Nathanj94 14:6764bb61d413 119 (_delay == 1) //delay stops powerups from being over-used
Nathanj94 7:ec6dc66ee196 120 ) {
Nathanj94 9:902b67101cdc 121 objects.undraw(lcd);
Nathanj94 9:902b67101cdc 122 objects.init(_objects_speed);
Nathanj94 14:6764bb61d413 123
Nathanj94 14:6764bb61d413 124 //while _delay = 0, A and B can't be used...
Nathanj94 14:6764bb61d413 125 //...in 10 seconds _delay = 1, A and B can be used again
Nathanj94 14:6764bb61d413 126 _delay = 0;
Nathanj94 14:6764bb61d413 127 timeout.attach(this, &Catch_Model::set_delay, 10.0);
Nathanj94 14:6764bb61d413 128 }
Nathanj94 14:6764bb61d413 129
Nathanj94 14:6764bb61d413 130 }
Nathanj94 14:6764bb61d413 131
Nathanj94 14:6764bb61d413 132 //If B is pressed, give another life
Nathanj94 14:6764bb61d413 133 void Catch_Model::check_b(N5110 &lcd, Gamepad &pad)
Nathanj94 14:6764bb61d413 134 {
Nathanj94 14:6764bb61d413 135 if (
Nathanj94 14:6764bb61d413 136 (pad.check_event(Gamepad::B_PRESSED) == true) &&
Nathanj94 14:6764bb61d413 137 (_delay == 1) //delay stops powerups from being over-used
Nathanj94 14:6764bb61d413 138 ) {
Nathanj94 14:6764bb61d413 139 _lives++;
Nathanj94 14:6764bb61d413 140
Nathanj94 14:6764bb61d413 141 //while _delay = 0, A and B can't be used...
Nathanj94 14:6764bb61d413 142 //...in 10 seconds _delay = 1, A and B can be used again
Nathanj94 9:902b67101cdc 143 _delay = 0;
Nathanj94 9:902b67101cdc 144 timeout.attach(this, &Catch_Model::set_delay, 10.0);
Nathanj94 8:db24c475f64f 145 }
Nathanj94 8:db24c475f64f 146
Nathanj94 8:db24c475f64f 147 }
Nathanj94 8:db24c475f64f 148
Nathanj94 14:6764bb61d413 149 //10 seconds after A or B is pressed, _delay = 1
Nathanj94 7:ec6dc66ee196 150 void Catch_Model::set_delay()
Nathanj94 7:ec6dc66ee196 151 {
Nathanj94 7:ec6dc66ee196 152 _delay = 1;
Nathanj94 7:ec6dc66ee196 153 }
Nathanj94 8:db24c475f64f 154
Nathanj94 13:ae2dac4ab786 155 //DISPLAY FUNCTIONS//
Nathanj94 14:6764bb61d413 156 //Re-draw the screen after updates to positions, scores etc are made
Nathanj94 13:ae2dac4ab786 157 void Catch_Model::draw(N5110 &lcd)
Nathanj94 5:7db3e43e5aca 158 {
Nathanj94 13:ae2dac4ab786 159 basket.draw(lcd);
Nathanj94 13:ae2dac4ab786 160 objects.draw(lcd);
Nathanj94 13:ae2dac4ab786 161 print_score(lcd);
Nathanj94 13:ae2dac4ab786 162 print_lives(lcd);
Nathanj94 13:ae2dac4ab786 163 print_delay(lcd);
Nathanj94 4:84e29254b988 164 }
Nathanj94 4:84e29254b988 165
Nathanj94 14:6764bb61d413 166 //Get the number of lives and print on the display
Nathanj94 4:84e29254b988 167 void Catch_Model::print_lives(N5110 &lcd)
Nathanj94 4:84e29254b988 168 {
Nathanj94 4:84e29254b988 169 char buffer[14];
Nathanj94 4:84e29254b988 170 int lives = get_lives();
Nathanj94 4:84e29254b988 171
Nathanj94 4:84e29254b988 172 int print_lives = sprintf(buffer, "LIVES:%d", lives);
Nathanj94 4:84e29254b988 173 if (print_lives <= 14) {
Nathanj94 4:84e29254b988 174 lcd.printString(buffer,0,0);
Nathanj94 4:84e29254b988 175 lcd.refresh();
Nathanj94 4:84e29254b988 176 }
Nathanj94 4:84e29254b988 177 }
Nathanj94 4:84e29254b988 178
Nathanj94 14:6764bb61d413 179 //Get the score and print on the display
Nathanj94 3:fc9133faec7a 180 void Catch_Model::print_score(N5110 &lcd)
Nathanj94 3:fc9133faec7a 181 {
Nathanj94 3:fc9133faec7a 182 char buffer[14];
Nathanj94 4:84e29254b988 183 int score = basket.get_score();
Nathanj94 2:8410e09b77aa 184
Nathanj94 4:84e29254b988 185 int print_score;
Nathanj94 4:84e29254b988 186
Nathanj94 14:6764bb61d413 187 if ((score == 0)||(score <= 9)) { //if loop keeps the length of the string fixed...
Nathanj94 4:84e29254b988 188 print_score = sprintf(buffer, "000%d", score);
Nathanj94 4:84e29254b988 189 } else if (score <= 99) {
Nathanj94 4:84e29254b988 190 print_score = sprintf(buffer, "00%2d", score);
Nathanj94 4:84e29254b988 191 } else if (score <= 999 ) {
Nathanj94 4:84e29254b988 192 print_score = sprintf(buffer, "0%3d", score);
Nathanj94 4:84e29254b988 193 } else {
Nathanj94 4:84e29254b988 194 print_score = sprintf(buffer, "%4d", score);
Nathanj94 14:6764bb61d413 195 } //...with maximum printable score of 9999
Nathanj94 4:84e29254b988 196
Nathanj94 3:fc9133faec7a 197 if (print_score <= 14) {
Nathanj94 4:84e29254b988 198 lcd.printString(buffer,59,0);
Nathanj94 3:fc9133faec7a 199 lcd.refresh();
Nathanj94 3:fc9133faec7a 200 }
Nathanj94 3:fc9133faec7a 201 }
Nathanj94 14:6764bb61d413 202
Nathanj94 14:6764bb61d413 203 //Print a tick if _delay = 1, otherwise print a cross
Nathanj94 8:db24c475f64f 204 void Catch_Model::print_delay(N5110 &lcd)
Nathanj94 8:db24c475f64f 205 {
Nathanj94 8:db24c475f64f 206 if (_delay == 1) {
Nathanj94 8:db24c475f64f 207 lcd.drawLine(46,4,50,8,1);
Nathanj94 8:db24c475f64f 208 lcd.drawLine(50,8,54,0,1);
Nathanj94 8:db24c475f64f 209 } else {
Nathanj94 8:db24c475f64f 210 lcd.drawLine(46,0,54,8,1);
Nathanj94 8:db24c475f64f 211 lcd.drawLine(46,8,54,0,1);
Nathanj94 13:ae2dac4ab786 212 }
Nathanj94 8:db24c475f64f 213 }