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 20 14:26:32 2017 +0000
Revision:
12:f7d6003e5c6b
Parent:
11:68e7c942f67b
Child:
13:ae2dac4ab786
Variable for Objects::move(int speed) changed

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