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:
Sat Apr 22 12:32:41 2017 +0000
Revision:
13:ae2dac4ab786
Parent:
12:f7d6003e5c6b
Child:
14:6764bb61d413
Redundancy removed and files organised ready for notes to be written

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 4:84e29254b988 13 void Catch_Model::init(int basket_y, int basket_width, int objects_speed, int lives)
Nathanj94 0:8d02400f792a 14 {
Nathanj94 0:8d02400f792a 15 _basket_y = basket_y;
Nathanj94 0:8d02400f792a 16 _basket_width = basket_width;
Nathanj94 1:43fbcc3584d6 17 _objects_speed = objects_speed;
Nathanj94 4:84e29254b988 18 _lives = lives;
Nathanj94 7:ec6dc66ee196 19 _delay = 1;
Nathanj94 1:43fbcc3584d6 20
Nathanj94 4:84e29254b988 21 basket.init(_basket_y, _basket_width);
Nathanj94 4:84e29254b988 22 objects.init(_objects_speed);
Nathanj94 0:8d02400f792a 23 }
Nathanj94 0:8d02400f792a 24
Nathanj94 0:8d02400f792a 25 void Catch_Model::input(Gamepad &pad)
Nathanj94 0:8d02400f792a 26 {
Nathanj94 0:8d02400f792a 27 _d = pad.get_direction();
Nathanj94 0:8d02400f792a 28 _mag = pad.get_mag();
Nathanj94 0:8d02400f792a 29 }
Nathanj94 0:8d02400f792a 30
Nathanj94 3:fc9133faec7a 31 void Catch_Model::update(N5110 &lcd, Gamepad &pad)
Nathanj94 0:8d02400f792a 32 {
Nathanj94 3:fc9133faec7a 33 check_basket_catch(lcd, pad);
Nathanj94 3:fc9133faec7a 34 check_basket_miss(lcd, pad);
Nathanj94 3:fc9133faec7a 35
Nathanj94 10:6605cd9d374b 36 basket.move_stick(_d, _mag);
Nathanj94 10:6605cd9d374b 37 basket.move_LR(pad);
Nathanj94 12:f7d6003e5c6b 38 objects.move(_objects_speed);
Nathanj94 0:8d02400f792a 39 }
Nathanj94 0:8d02400f792a 40
Nathanj94 13:ae2dac4ab786 41 //GAME RULES FUNCTIONS//
Nathanj94 3:fc9133faec7a 42 void Catch_Model::check_basket_catch(N5110 &lcd, Gamepad &pad)
Nathanj94 2:8410e09b77aa 43 {
Nathanj94 4:84e29254b988 44 int b_x_pos = basket.get_x();
Nathanj94 4:84e29254b988 45 int b_y_pos = basket.get_y();
Nathanj94 4:84e29254b988 46 int o_x_pos = objects.get_x();
Nathanj94 4:84e29254b988 47 int o_y_pos = objects.get_y();
Nathanj94 2:8410e09b77aa 48
Nathanj94 3:fc9133faec7a 49 if(
Nathanj94 3:fc9133faec7a 50 (o_y_pos >= b_y_pos) &&
Nathanj94 9:902b67101cdc 51 (o_x_pos >= b_x_pos) &&
Nathanj94 9:902b67101cdc 52 (o_x_pos <= (b_x_pos + 6))
Nathanj94 3:fc9133faec7a 53 ) {
Nathanj94 8:db24c475f64f 54 pad.tone(1000, 0.2);
Nathanj94 4:84e29254b988 55 objects.undraw(lcd);
Nathanj94 5:7db3e43e5aca 56 add_score();
Nathanj94 4:84e29254b988 57 objects.init(_objects_speed);
Nathanj94 3:fc9133faec7a 58 }
Nathanj94 2:8410e09b77aa 59 }
Nathanj94 2:8410e09b77aa 60
Nathanj94 3:fc9133faec7a 61 void Catch_Model::check_basket_miss(N5110 &lcd, Gamepad &pad)
Nathanj94 2:8410e09b77aa 62 {
Nathanj94 4:84e29254b988 63 int b_y_pos = basket.get_y();
Nathanj94 4:84e29254b988 64 int o_y_pos = objects.get_y();
Nathanj94 3:fc9133faec7a 65
Nathanj94 6:61bcf98e0a88 66 int score_var;
Nathanj94 6:61bcf98e0a88 67 score_var = objects.get_score_var();
Nathanj94 6:61bcf98e0a88 68
Nathanj94 3:fc9133faec7a 69 if (o_y_pos > b_y_pos) {
Nathanj94 4:84e29254b988 70 objects.undraw(lcd);
Nathanj94 6:61bcf98e0a88 71 if (score_var != 5) {
Nathanj94 6:61bcf98e0a88 72 _lives--;
Nathanj94 9:902b67101cdc 73 }
Nathanj94 8:db24c475f64f 74 pad.tone(100, 0.2);
Nathanj94 8:db24c475f64f 75 objects.init(_objects_speed);
Nathanj94 9:902b67101cdc 76 }
Nathanj94 3:fc9133faec7a 77 }
Nathanj94 3:fc9133faec7a 78
Nathanj94 13:ae2dac4ab786 79 void Catch_Model::add_score()
Nathanj94 13:ae2dac4ab786 80 {
Nathanj94 13:ae2dac4ab786 81 int score_var;
Nathanj94 13:ae2dac4ab786 82 score_var = objects.get_score_var();
Nathanj94 13:ae2dac4ab786 83
Nathanj94 13:ae2dac4ab786 84 if (score_var == 1) {
Nathanj94 13:ae2dac4ab786 85 basket.add_score_1();
Nathanj94 13:ae2dac4ab786 86 } else if (score_var == 2) {
Nathanj94 13:ae2dac4ab786 87 basket.add_score_2();
Nathanj94 13:ae2dac4ab786 88 } else if (score_var == 3) {
Nathanj94 13:ae2dac4ab786 89 basket.add_score_5();
Nathanj94 13:ae2dac4ab786 90 } else if (score_var == 4) {
Nathanj94 13:ae2dac4ab786 91 basket.add_score_10();
Nathanj94 13:ae2dac4ab786 92 } else {
Nathanj94 13:ae2dac4ab786 93 _lives--;
Nathanj94 13:ae2dac4ab786 94 }
Nathanj94 13:ae2dac4ab786 95 }
Nathanj94 13:ae2dac4ab786 96
Nathanj94 13:ae2dac4ab786 97 int Catch_Model::get_lives()
Nathanj94 13:ae2dac4ab786 98 {
Nathanj94 13:ae2dac4ab786 99 return _lives;
Nathanj94 13:ae2dac4ab786 100 }
Nathanj94 13:ae2dac4ab786 101
Nathanj94 13:ae2dac4ab786 102 //BUTTON FUNCTIONS//
Nathanj94 8:db24c475f64f 103 void Catch_Model::check_a(N5110 &lcd, Gamepad &pad)
Nathanj94 7:ec6dc66ee196 104 {
Nathanj94 7:ec6dc66ee196 105 if (
Nathanj94 7:ec6dc66ee196 106 (pad.check_event(Gamepad::A_PRESSED) == true) &&
Nathanj94 7:ec6dc66ee196 107 (_delay == 1)
Nathanj94 7:ec6dc66ee196 108 ) {
Nathanj94 9:902b67101cdc 109 objects.undraw(lcd);
Nathanj94 9:902b67101cdc 110 objects.init(_objects_speed);
Nathanj94 7:ec6dc66ee196 111
Nathanj94 9:902b67101cdc 112 _delay = 0;
Nathanj94 9:902b67101cdc 113 timeout.attach(this, &Catch_Model::set_delay, 10.0);
Nathanj94 8:db24c475f64f 114 }
Nathanj94 8:db24c475f64f 115
Nathanj94 8:db24c475f64f 116 }
Nathanj94 8:db24c475f64f 117
Nathanj94 8:db24c475f64f 118 void Catch_Model::check_b(N5110 &lcd, Gamepad &pad)
Nathanj94 8:db24c475f64f 119 {
Nathanj94 8:db24c475f64f 120 if (
Nathanj94 8:db24c475f64f 121 (pad.check_event(Gamepad::B_PRESSED) == true) &&
Nathanj94 8:db24c475f64f 122 (_delay == 1)
Nathanj94 8:db24c475f64f 123 ) {
Nathanj94 9:902b67101cdc 124 _lives++;
Nathanj94 8:db24c475f64f 125
Nathanj94 9:902b67101cdc 126 _delay = 0;
Nathanj94 9:902b67101cdc 127 timeout.attach(this, &Catch_Model::set_delay, 10.0);
Nathanj94 7:ec6dc66ee196 128 }
Nathanj94 7:ec6dc66ee196 129
Nathanj94 7:ec6dc66ee196 130 }
Nathanj94 7:ec6dc66ee196 131
Nathanj94 7:ec6dc66ee196 132 void Catch_Model::set_delay()
Nathanj94 7:ec6dc66ee196 133 {
Nathanj94 7:ec6dc66ee196 134 _delay = 1;
Nathanj94 7:ec6dc66ee196 135 }
Nathanj94 8:db24c475f64f 136
Nathanj94 13:ae2dac4ab786 137 //DISPLAY FUNCTIONS//
Nathanj94 13:ae2dac4ab786 138 void Catch_Model::draw(N5110 &lcd)
Nathanj94 5:7db3e43e5aca 139 {
Nathanj94 13:ae2dac4ab786 140 basket.draw(lcd);
Nathanj94 13:ae2dac4ab786 141 objects.draw(lcd);
Nathanj94 13:ae2dac4ab786 142 print_score(lcd);
Nathanj94 13:ae2dac4ab786 143 print_lives(lcd);
Nathanj94 13:ae2dac4ab786 144 print_delay(lcd);
Nathanj94 4:84e29254b988 145 }
Nathanj94 4:84e29254b988 146
Nathanj94 4:84e29254b988 147 void Catch_Model::print_lives(N5110 &lcd)
Nathanj94 4:84e29254b988 148 {
Nathanj94 4:84e29254b988 149 char buffer[14];
Nathanj94 4:84e29254b988 150 int lives = get_lives();
Nathanj94 4:84e29254b988 151
Nathanj94 4:84e29254b988 152 int print_lives = sprintf(buffer, "LIVES:%d", lives);
Nathanj94 4:84e29254b988 153 if (print_lives <= 14) {
Nathanj94 4:84e29254b988 154 lcd.printString(buffer,0,0);
Nathanj94 4:84e29254b988 155 lcd.refresh();
Nathanj94 4:84e29254b988 156 }
Nathanj94 4:84e29254b988 157 }
Nathanj94 4:84e29254b988 158
Nathanj94 3:fc9133faec7a 159 void Catch_Model::print_score(N5110 &lcd)
Nathanj94 3:fc9133faec7a 160 {
Nathanj94 3:fc9133faec7a 161 char buffer[14];
Nathanj94 4:84e29254b988 162 int score = basket.get_score();
Nathanj94 2:8410e09b77aa 163
Nathanj94 4:84e29254b988 164 int print_score;
Nathanj94 4:84e29254b988 165
Nathanj94 4:84e29254b988 166 if ((score == 0)||(score <= 9)) {
Nathanj94 4:84e29254b988 167 print_score = sprintf(buffer, "000%d", score);
Nathanj94 4:84e29254b988 168 } else if (score <= 99) {
Nathanj94 4:84e29254b988 169 print_score = sprintf(buffer, "00%2d", score);
Nathanj94 4:84e29254b988 170 } else if (score <= 999 ) {
Nathanj94 4:84e29254b988 171 print_score = sprintf(buffer, "0%3d", score);
Nathanj94 4:84e29254b988 172 } else {
Nathanj94 4:84e29254b988 173 print_score = sprintf(buffer, "%4d", score);
Nathanj94 4:84e29254b988 174 }
Nathanj94 4:84e29254b988 175
Nathanj94 3:fc9133faec7a 176 if (print_score <= 14) {
Nathanj94 4:84e29254b988 177 lcd.printString(buffer,59,0);
Nathanj94 3:fc9133faec7a 178 lcd.refresh();
Nathanj94 3:fc9133faec7a 179 }
Nathanj94 3:fc9133faec7a 180 }
Nathanj94 8:db24c475f64f 181
Nathanj94 8:db24c475f64f 182 void Catch_Model::print_delay(N5110 &lcd)
Nathanj94 8:db24c475f64f 183 {
Nathanj94 8:db24c475f64f 184 if (_delay == 1) {
Nathanj94 8:db24c475f64f 185 lcd.drawLine(46,4,50,8,1);
Nathanj94 8:db24c475f64f 186 lcd.drawLine(50,8,54,0,1);
Nathanj94 8:db24c475f64f 187 } else {
Nathanj94 8:db24c475f64f 188 lcd.drawLine(46,0,54,8,1);
Nathanj94 8:db24c475f64f 189 lcd.drawLine(46,8,54,0,1);
Nathanj94 13:ae2dac4ab786 190 }
Nathanj94 8:db24c475f64f 191 }