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.cpp@9:902b67101cdc, 2017-04-15 (annotated)
- Committer:
- Nathanj94
- Date:
- Sat Apr 15 15:06:54 2017 +0000
- Revision:
- 9:902b67101cdc
- Parent:
- 8:db24c475f64f
- Child:
- 10:6605cd9d374b
Started x button function where the object will slow down until it is caught, so far keeps freezing the game
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:902b67101cdc | 20 | _bullet_time = 0; |
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 | 0:8d02400f792a | 26 | void Catch_Model::input(Gamepad &pad) |
Nathanj94 | 0:8d02400f792a | 27 | { |
Nathanj94 | 0:8d02400f792a | 28 | _d = pad.get_direction(); |
Nathanj94 | 0:8d02400f792a | 29 | _mag = pad.get_mag(); |
Nathanj94 | 0:8d02400f792a | 30 | } |
Nathanj94 | 0:8d02400f792a | 31 | |
Nathanj94 | 3:fc9133faec7a | 32 | void Catch_Model::update(N5110 &lcd, Gamepad &pad) |
Nathanj94 | 0:8d02400f792a | 33 | { |
Nathanj94 | 3:fc9133faec7a | 34 | check_basket_catch(lcd, pad); |
Nathanj94 | 3:fc9133faec7a | 35 | check_basket_miss(lcd, pad); |
Nathanj94 | 3:fc9133faec7a | 36 | |
Nathanj94 | 4:84e29254b988 | 37 | basket.move(_d, _mag, pad); |
Nathanj94 | 9:902b67101cdc | 38 | if (_bullet_time == 1) { |
Nathanj94 | 9:902b67101cdc | 39 | objects.bullet_time(); |
Nathanj94 | 9:902b67101cdc | 40 | } else { |
Nathanj94 | 9:902b67101cdc | 41 | objects.move(pad); |
Nathanj94 | 9:902b67101cdc | 42 | } |
Nathanj94 | 0:8d02400f792a | 43 | } |
Nathanj94 | 0:8d02400f792a | 44 | |
Nathanj94 | 0:8d02400f792a | 45 | void Catch_Model::draw(N5110 &lcd) |
Nathanj94 | 0:8d02400f792a | 46 | { |
Nathanj94 | 4:84e29254b988 | 47 | basket.draw(lcd); |
Nathanj94 | 4:84e29254b988 | 48 | objects.draw(lcd); |
Nathanj94 | 4:84e29254b988 | 49 | print_score(lcd); |
Nathanj94 | 4:84e29254b988 | 50 | print_lives(lcd); |
Nathanj94 | 8:db24c475f64f | 51 | print_delay(lcd); |
Nathanj94 | 2:8410e09b77aa | 52 | } |
Nathanj94 | 2:8410e09b77aa | 53 | |
Nathanj94 | 3:fc9133faec7a | 54 | void Catch_Model::check_basket_catch(N5110 &lcd, Gamepad &pad) |
Nathanj94 | 2:8410e09b77aa | 55 | { |
Nathanj94 | 4:84e29254b988 | 56 | int b_x_pos = basket.get_x(); |
Nathanj94 | 4:84e29254b988 | 57 | int b_y_pos = basket.get_y(); |
Nathanj94 | 4:84e29254b988 | 58 | int o_x_pos = objects.get_x(); |
Nathanj94 | 4:84e29254b988 | 59 | int o_y_pos = objects.get_y(); |
Nathanj94 | 2:8410e09b77aa | 60 | |
Nathanj94 | 3:fc9133faec7a | 61 | if( |
Nathanj94 | 3:fc9133faec7a | 62 | (o_y_pos >= b_y_pos) && |
Nathanj94 | 9:902b67101cdc | 63 | (o_x_pos >= b_x_pos) && |
Nathanj94 | 9:902b67101cdc | 64 | (o_x_pos <= (b_x_pos + 6)) |
Nathanj94 | 3:fc9133faec7a | 65 | ) { |
Nathanj94 | 8:db24c475f64f | 66 | pad.tone(1000, 0.2); |
Nathanj94 | 4:84e29254b988 | 67 | objects.undraw(lcd); |
Nathanj94 | 5:7db3e43e5aca | 68 | add_score(); |
Nathanj94 | 4:84e29254b988 | 69 | objects.init(_objects_speed); |
Nathanj94 | 3:fc9133faec7a | 70 | } |
Nathanj94 | 2:8410e09b77aa | 71 | } |
Nathanj94 | 2:8410e09b77aa | 72 | |
Nathanj94 | 3:fc9133faec7a | 73 | void Catch_Model::check_basket_miss(N5110 &lcd, Gamepad &pad) |
Nathanj94 | 2:8410e09b77aa | 74 | { |
Nathanj94 | 4:84e29254b988 | 75 | int b_y_pos = basket.get_y(); |
Nathanj94 | 4:84e29254b988 | 76 | int o_y_pos = objects.get_y(); |
Nathanj94 | 3:fc9133faec7a | 77 | |
Nathanj94 | 6:61bcf98e0a88 | 78 | int score_var; |
Nathanj94 | 6:61bcf98e0a88 | 79 | score_var = objects.get_score_var(); |
Nathanj94 | 6:61bcf98e0a88 | 80 | |
Nathanj94 | 3:fc9133faec7a | 81 | if (o_y_pos > b_y_pos) { |
Nathanj94 | 4:84e29254b988 | 82 | objects.undraw(lcd); |
Nathanj94 | 6:61bcf98e0a88 | 83 | if (score_var != 5) { |
Nathanj94 | 6:61bcf98e0a88 | 84 | _lives--; |
Nathanj94 | 9:902b67101cdc | 85 | } |
Nathanj94 | 8:db24c475f64f | 86 | pad.tone(100, 0.2); |
Nathanj94 | 8:db24c475f64f | 87 | objects.init(_objects_speed); |
Nathanj94 | 9:902b67101cdc | 88 | } |
Nathanj94 | 3:fc9133faec7a | 89 | } |
Nathanj94 | 3:fc9133faec7a | 90 | |
Nathanj94 | 8:db24c475f64f | 91 | void Catch_Model::check_a(N5110 &lcd, Gamepad &pad) |
Nathanj94 | 7:ec6dc66ee196 | 92 | { |
Nathanj94 | 7:ec6dc66ee196 | 93 | if ( |
Nathanj94 | 7:ec6dc66ee196 | 94 | (pad.check_event(Gamepad::A_PRESSED) == true) && |
Nathanj94 | 7:ec6dc66ee196 | 95 | (_delay == 1) |
Nathanj94 | 7:ec6dc66ee196 | 96 | ) { |
Nathanj94 | 9:902b67101cdc | 97 | objects.undraw(lcd); |
Nathanj94 | 9:902b67101cdc | 98 | objects.init(_objects_speed); |
Nathanj94 | 7:ec6dc66ee196 | 99 | |
Nathanj94 | 9:902b67101cdc | 100 | _delay = 0; |
Nathanj94 | 9:902b67101cdc | 101 | timeout.attach(this, &Catch_Model::set_delay, 10.0); |
Nathanj94 | 8:db24c475f64f | 102 | } |
Nathanj94 | 8:db24c475f64f | 103 | |
Nathanj94 | 8:db24c475f64f | 104 | } |
Nathanj94 | 8:db24c475f64f | 105 | |
Nathanj94 | 8:db24c475f64f | 106 | void Catch_Model::check_b(N5110 &lcd, Gamepad &pad) |
Nathanj94 | 8:db24c475f64f | 107 | { |
Nathanj94 | 8:db24c475f64f | 108 | if ( |
Nathanj94 | 8:db24c475f64f | 109 | (pad.check_event(Gamepad::B_PRESSED) == true) && |
Nathanj94 | 8:db24c475f64f | 110 | (_delay == 1) |
Nathanj94 | 8:db24c475f64f | 111 | ) { |
Nathanj94 | 9:902b67101cdc | 112 | _lives++; |
Nathanj94 | 8:db24c475f64f | 113 | |
Nathanj94 | 9:902b67101cdc | 114 | _delay = 0; |
Nathanj94 | 9:902b67101cdc | 115 | timeout.attach(this, &Catch_Model::set_delay, 10.0); |
Nathanj94 | 7:ec6dc66ee196 | 116 | } |
Nathanj94 | 7:ec6dc66ee196 | 117 | |
Nathanj94 | 7:ec6dc66ee196 | 118 | } |
Nathanj94 | 7:ec6dc66ee196 | 119 | |
Nathanj94 | 9:902b67101cdc | 120 | void Catch_Model::check_x(N5110 &lcd, Gamepad &pad) |
Nathanj94 | 9:902b67101cdc | 121 | { |
Nathanj94 | 9:902b67101cdc | 122 | if ( |
Nathanj94 | 9:902b67101cdc | 123 | (pad.check_event(Gamepad::X_PRESSED) == true) && |
Nathanj94 | 9:902b67101cdc | 124 | (_delay == 1) |
Nathanj94 | 9:902b67101cdc | 125 | ) { |
Nathanj94 | 9:902b67101cdc | 126 | /* |
Nathanj94 | 9:902b67101cdc | 127 | int b_y_pos = basket.get_y(); |
Nathanj94 | 9:902b67101cdc | 128 | int o_y_pos = objects.get_y(); |
Nathanj94 | 9:902b67101cdc | 129 | |
Nathanj94 | 9:902b67101cdc | 130 | do { |
Nathanj94 | 9:902b67101cdc | 131 | _bullet_time = 1; |
Nathanj94 | 9:902b67101cdc | 132 | } while (o_y_pos < b_y_pos); |
Nathanj94 | 9:902b67101cdc | 133 | |
Nathanj94 | 9:902b67101cdc | 134 | _bullet_time = 0; |
Nathanj94 | 9:902b67101cdc | 135 | */ |
Nathanj94 | 9:902b67101cdc | 136 | _delay = 0; |
Nathanj94 | 9:902b67101cdc | 137 | timeout.attach(this, &Catch_Model::set_delay, 10.0); |
Nathanj94 | 9:902b67101cdc | 138 | } |
Nathanj94 | 9:902b67101cdc | 139 | |
Nathanj94 | 9:902b67101cdc | 140 | } |
Nathanj94 | 9:902b67101cdc | 141 | |
Nathanj94 | 7:ec6dc66ee196 | 142 | void Catch_Model::set_delay() |
Nathanj94 | 7:ec6dc66ee196 | 143 | { |
Nathanj94 | 7:ec6dc66ee196 | 144 | _delay = 1; |
Nathanj94 | 7:ec6dc66ee196 | 145 | } |
Nathanj94 | 8:db24c475f64f | 146 | |
Nathanj94 | 5:7db3e43e5aca | 147 | void Catch_Model::add_score() |
Nathanj94 | 5:7db3e43e5aca | 148 | { |
Nathanj94 | 5:7db3e43e5aca | 149 | int score_var; |
Nathanj94 | 5:7db3e43e5aca | 150 | score_var = objects.get_score_var(); |
Nathanj94 | 5:7db3e43e5aca | 151 | |
Nathanj94 | 5:7db3e43e5aca | 152 | if (score_var == 1) { |
Nathanj94 | 5:7db3e43e5aca | 153 | basket.add_score_1(); |
Nathanj94 | 5:7db3e43e5aca | 154 | } else if (score_var == 2) { |
Nathanj94 | 5:7db3e43e5aca | 155 | basket.add_score_2(); |
Nathanj94 | 5:7db3e43e5aca | 156 | } else if (score_var == 3) { |
Nathanj94 | 5:7db3e43e5aca | 157 | basket.add_score_5(); |
Nathanj94 | 6:61bcf98e0a88 | 158 | } else if (score_var == 4) { |
Nathanj94 | 6:61bcf98e0a88 | 159 | basket.add_score_10(); |
Nathanj94 | 5:7db3e43e5aca | 160 | } else { |
Nathanj94 | 6:61bcf98e0a88 | 161 | _lives--; |
Nathanj94 | 5:7db3e43e5aca | 162 | } |
Nathanj94 | 5:7db3e43e5aca | 163 | } |
Nathanj94 | 5:7db3e43e5aca | 164 | |
Nathanj94 | 4:84e29254b988 | 165 | int Catch_Model::get_lives() |
Nathanj94 | 4:84e29254b988 | 166 | { |
Nathanj94 | 4:84e29254b988 | 167 | return _lives; |
Nathanj94 | 4:84e29254b988 | 168 | } |
Nathanj94 | 4:84e29254b988 | 169 | |
Nathanj94 | 4:84e29254b988 | 170 | void Catch_Model::print_lives(N5110 &lcd) |
Nathanj94 | 4:84e29254b988 | 171 | { |
Nathanj94 | 4:84e29254b988 | 172 | char buffer[14]; |
Nathanj94 | 4:84e29254b988 | 173 | int lives = get_lives(); |
Nathanj94 | 4:84e29254b988 | 174 | |
Nathanj94 | 4:84e29254b988 | 175 | int print_lives = sprintf(buffer, "LIVES:%d", lives); |
Nathanj94 | 4:84e29254b988 | 176 | if (print_lives <= 14) { |
Nathanj94 | 4:84e29254b988 | 177 | lcd.printString(buffer,0,0); |
Nathanj94 | 4:84e29254b988 | 178 | lcd.refresh(); |
Nathanj94 | 4:84e29254b988 | 179 | } |
Nathanj94 | 4:84e29254b988 | 180 | } |
Nathanj94 | 4:84e29254b988 | 181 | |
Nathanj94 | 3:fc9133faec7a | 182 | void Catch_Model::print_score(N5110 &lcd) |
Nathanj94 | 3:fc9133faec7a | 183 | { |
Nathanj94 | 3:fc9133faec7a | 184 | char buffer[14]; |
Nathanj94 | 4:84e29254b988 | 185 | int score = basket.get_score(); |
Nathanj94 | 2:8410e09b77aa | 186 | |
Nathanj94 | 4:84e29254b988 | 187 | int print_score; |
Nathanj94 | 4:84e29254b988 | 188 | |
Nathanj94 | 4:84e29254b988 | 189 | if ((score == 0)||(score <= 9)) { |
Nathanj94 | 4:84e29254b988 | 190 | print_score = sprintf(buffer, "000%d", score); |
Nathanj94 | 4:84e29254b988 | 191 | } else if (score <= 99) { |
Nathanj94 | 4:84e29254b988 | 192 | print_score = sprintf(buffer, "00%2d", score); |
Nathanj94 | 4:84e29254b988 | 193 | } else if (score <= 999 ) { |
Nathanj94 | 4:84e29254b988 | 194 | print_score = sprintf(buffer, "0%3d", score); |
Nathanj94 | 4:84e29254b988 | 195 | } else { |
Nathanj94 | 4:84e29254b988 | 196 | print_score = sprintf(buffer, "%4d", score); |
Nathanj94 | 4:84e29254b988 | 197 | } |
Nathanj94 | 4:84e29254b988 | 198 | |
Nathanj94 | 3:fc9133faec7a | 199 | if (print_score <= 14) { |
Nathanj94 | 4:84e29254b988 | 200 | lcd.printString(buffer,59,0); |
Nathanj94 | 3:fc9133faec7a | 201 | lcd.refresh(); |
Nathanj94 | 3:fc9133faec7a | 202 | } |
Nathanj94 | 3:fc9133faec7a | 203 | } |
Nathanj94 | 8:db24c475f64f | 204 | |
Nathanj94 | 8:db24c475f64f | 205 | void Catch_Model::print_delay(N5110 &lcd) |
Nathanj94 | 8:db24c475f64f | 206 | { |
Nathanj94 | 8:db24c475f64f | 207 | if (_delay == 1) { |
Nathanj94 | 8:db24c475f64f | 208 | lcd.drawLine(46,4,50,8,1); |
Nathanj94 | 8:db24c475f64f | 209 | lcd.drawLine(50,8,54,0,1); |
Nathanj94 | 8:db24c475f64f | 210 | } else { |
Nathanj94 | 8:db24c475f64f | 211 | lcd.drawLine(46,0,54,8,1); |
Nathanj94 | 8:db24c475f64f | 212 | lcd.drawLine(46,8,54,0,1); |
Nathanj94 | 8:db24c475f64f | 213 | } |
Nathanj94 | 8:db24c475f64f | 214 | } |