Yang Meng
/
207_program
Similar to the example code.
Cylinder/Cylinder.cpp@7:f3b57f157655, 2019-05-05 (annotated)
- Committer:
- 2016110307
- Date:
- Sun May 05 16:17:55 2019 +0000
- Revision:
- 7:f3b57f157655
- Parent:
- 2:533869513c4a
Final Submission. I have read and agreed with Statement of Academic Integrity.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
2016110307 | 0:97418ec4c37d | 1 | #include "Cylinder.h" |
2016110307 | 0:97418ec4c37d | 2 | |
2016110307 | 0:97418ec4c37d | 3 | void Cylinder::init() |
2016110307 | 0:97418ec4c37d | 4 | { |
2016110307 | 0:97418ec4c37d | 5 | _x1 = 72; |
2016110307 | 0:97418ec4c37d | 6 | _x2 = 102; |
2016110307 | 0:97418ec4c37d | 7 | _x3 = 132; |
2016110307 | 0:97418ec4c37d | 8 | _height1 = rand()%11+10; |
2016110307 | 0:97418ec4c37d | 9 | _height2 = rand()%11+10; |
2016110307 | 0:97418ec4c37d | 10 | _height3 = rand()%11+10; |
2016110307 | 0:97418ec4c37d | 11 | _gap1 = rand()%11+15; |
2016110307 | 0:97418ec4c37d | 12 | _gap2 = rand()%11+15; |
2016110307 | 0:97418ec4c37d | 13 | _gap3 = rand()%11+15; |
2016110307 | 1:85ab0d979b57 | 14 | _speed = 2; |
2016110307 | 0:97418ec4c37d | 15 | _score = 0; |
2016110307 | 1:85ab0d979b57 | 16 | _state = 0; |
2016110307 | 0:97418ec4c37d | 17 | |
2016110307 | 0:97418ec4c37d | 18 | } |
2016110307 | 0:97418ec4c37d | 19 | |
2016110307 | 1:85ab0d979b57 | 20 | void Cylinder::select(N5110 &lcd, Gamepad &pad) |
2016110307 | 1:85ab0d979b57 | 21 | { |
2016110307 | 1:85ab0d979b57 | 22 | pad.init(); |
2016110307 | 1:85ab0d979b57 | 23 | while( pad.check_event(Gamepad::START_PRESSED) == false ) { |
2016110307 | 1:85ab0d979b57 | 24 | lcd.clear(); |
2016110307 | 1:85ab0d979b57 | 25 | lcd.printString("Normal", 0,0); |
2016110307 | 1:85ab0d979b57 | 26 | lcd.printString("Difficult", 0,2); |
2016110307 | 1:85ab0d979b57 | 27 | lcd.printString(" Press Start ",0,4); |
2016110307 | 1:85ab0d979b57 | 28 | lcd.printString(" to continue ",0,5); |
2016110307 | 1:85ab0d979b57 | 29 | Direction d = pad.get_direction(); |
2016110307 | 1:85ab0d979b57 | 30 | if(d == S) { |
2016110307 | 1:85ab0d979b57 | 31 | _state = _state + 1; |
2016110307 | 1:85ab0d979b57 | 32 | } |
2016110307 | 1:85ab0d979b57 | 33 | if(d == N) { |
2016110307 | 1:85ab0d979b57 | 34 | _state = _state - 1; |
2016110307 | 1:85ab0d979b57 | 35 | } |
2016110307 | 1:85ab0d979b57 | 36 | if (_state < 0) { |
2016110307 | 1:85ab0d979b57 | 37 | _state = 1; |
2016110307 | 1:85ab0d979b57 | 38 | } |
2016110307 | 1:85ab0d979b57 | 39 | if(_state > 1) { |
2016110307 | 1:85ab0d979b57 | 40 | _state = 0; |
2016110307 | 1:85ab0d979b57 | 41 | } |
2016110307 | 1:85ab0d979b57 | 42 | switch(_state) { |
2016110307 | 1:85ab0d979b57 | 43 | //get the first option |
2016110307 | 1:85ab0d979b57 | 44 | case 0 : |
2016110307 | 1:85ab0d979b57 | 45 | |
2016110307 | 1:85ab0d979b57 | 46 | lcd.drawRect(42,3,13,3,FILL_BLACK); |
2016110307 | 1:85ab0d979b57 | 47 | lcd.drawRect(37,0,5,9,FILL_BLACK); |
2016110307 | 1:85ab0d979b57 | 48 | lcd.setPixel(36,4,true); |
2016110307 | 1:85ab0d979b57 | 49 | lcd.setPixel(37,0,false); |
2016110307 | 1:85ab0d979b57 | 50 | lcd.setPixel(38,0,false); |
2016110307 | 1:85ab0d979b57 | 51 | lcd.setPixel(39,0,false); |
2016110307 | 1:85ab0d979b57 | 52 | lcd.setPixel(37,1,false); |
2016110307 | 1:85ab0d979b57 | 53 | lcd.setPixel(38,1,false); |
2016110307 | 1:85ab0d979b57 | 54 | lcd.setPixel(37,2,false); |
2016110307 | 1:85ab0d979b57 | 55 | lcd.setPixel(37,6,false); |
2016110307 | 1:85ab0d979b57 | 56 | lcd.setPixel(37,7,false); |
2016110307 | 1:85ab0d979b57 | 57 | lcd.setPixel(38,7,false); |
2016110307 | 1:85ab0d979b57 | 58 | lcd.setPixel(37,8,false); |
2016110307 | 1:85ab0d979b57 | 59 | lcd.setPixel(38,8,false); |
2016110307 | 1:85ab0d979b57 | 60 | lcd.setPixel(39,8,false); |
2016110307 | 1:85ab0d979b57 | 61 | |
2016110307 | 1:85ab0d979b57 | 62 | break; |
2016110307 | 1:85ab0d979b57 | 63 | case 1 : |
2016110307 | 1:85ab0d979b57 | 64 | |
2016110307 | 1:85ab0d979b57 | 65 | lcd.drawRect(63,18,13,3,FILL_BLACK); |
2016110307 | 1:85ab0d979b57 | 66 | lcd.drawRect(58,15,5,9,FILL_BLACK); |
2016110307 | 1:85ab0d979b57 | 67 | lcd.setPixel(57,19,true); |
2016110307 | 1:85ab0d979b57 | 68 | lcd.setPixel(58,15,false); |
2016110307 | 1:85ab0d979b57 | 69 | lcd.setPixel(59,15,false); |
2016110307 | 1:85ab0d979b57 | 70 | lcd.setPixel(60,15,false); |
2016110307 | 1:85ab0d979b57 | 71 | lcd.setPixel(58,16,false); |
2016110307 | 1:85ab0d979b57 | 72 | lcd.setPixel(59,16,false); |
2016110307 | 1:85ab0d979b57 | 73 | lcd.setPixel(58,17,false); |
2016110307 | 1:85ab0d979b57 | 74 | lcd.setPixel(58,21,false); |
2016110307 | 1:85ab0d979b57 | 75 | lcd.setPixel(58,22,false); |
2016110307 | 1:85ab0d979b57 | 76 | lcd.setPixel(59,22,false); |
2016110307 | 1:85ab0d979b57 | 77 | lcd.setPixel(58,23,false); |
2016110307 | 1:85ab0d979b57 | 78 | lcd.setPixel(59,23,false); |
2016110307 | 1:85ab0d979b57 | 79 | lcd.setPixel(60,23,false); |
2016110307 | 1:85ab0d979b57 | 80 | _height1 = rand()%9+10; |
2016110307 | 1:85ab0d979b57 | 81 | _height2 = rand()%9+10; |
2016110307 | 1:85ab0d979b57 | 82 | _height3 = rand()%9+10; |
2016110307 | 1:85ab0d979b57 | 83 | _gap1 = rand()%3+13; |
2016110307 | 1:85ab0d979b57 | 84 | _gap2 = rand()%3+13; |
2016110307 | 1:85ab0d979b57 | 85 | _gap3 = rand()%3+13; |
2016110307 | 1:85ab0d979b57 | 86 | _speed = 3; |
2016110307 | 2:533869513c4a | 87 | _bird.set_speed(6); |
2016110307 | 1:85ab0d979b57 | 88 | |
2016110307 | 1:85ab0d979b57 | 89 | break; |
2016110307 | 1:85ab0d979b57 | 90 | } |
2016110307 | 1:85ab0d979b57 | 91 | lcd.refresh(); |
2016110307 | 1:85ab0d979b57 | 92 | wait(0.3); |
2016110307 | 1:85ab0d979b57 | 93 | } |
2016110307 | 1:85ab0d979b57 | 94 | |
2016110307 | 1:85ab0d979b57 | 95 | } |
2016110307 | 1:85ab0d979b57 | 96 | |
2016110307 | 1:85ab0d979b57 | 97 | |
2016110307 | 0:97418ec4c37d | 98 | void Cylinder::draw(N5110 &lcd) |
2016110307 | 0:97418ec4c37d | 99 | { |
2016110307 | 0:97418ec4c37d | 100 | lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); |
2016110307 | 0:97418ec4c37d | 101 | lcd.drawRect(64,0,20,20,FILL_TRANSPARENT); |
2016110307 | 0:97418ec4c37d | 102 | if(_x1 > 60 ) { |
2016110307 | 1:85ab0d979b57 | 103 | _a = 1; |
2016110307 | 1:85ab0d979b57 | 104 | _b = _height2; |
2016110307 | 1:85ab0d979b57 | 105 | _c = _height3; |
2016110307 | 0:97418ec4c37d | 106 | } else if (_x2 > 60) { |
2016110307 | 1:85ab0d979b57 | 107 | _a = _height1; |
2016110307 | 1:85ab0d979b57 | 108 | _b = 1; |
2016110307 | 0:97418ec4c37d | 109 | _c = _height3; |
2016110307 | 0:97418ec4c37d | 110 | } else if (_x3 > 60) { |
2016110307 | 0:97418ec4c37d | 111 | _a = _height1; |
2016110307 | 0:97418ec4c37d | 112 | _b = _height2; |
2016110307 | 0:97418ec4c37d | 113 | _c = 1; |
2016110307 | 0:97418ec4c37d | 114 | } else { |
2016110307 | 0:97418ec4c37d | 115 | _a = _height1; |
2016110307 | 0:97418ec4c37d | 116 | _b = _height2; |
2016110307 | 0:97418ec4c37d | 117 | _c = _height3; |
2016110307 | 0:97418ec4c37d | 118 | } |
2016110307 | 0:97418ec4c37d | 119 | |
2016110307 | 0:97418ec4c37d | 120 | |
2016110307 | 0:97418ec4c37d | 121 | lcd.drawRect(_x1,0,4,_a,FILL_BLACK); |
2016110307 | 0:97418ec4c37d | 122 | lcd.drawRect(_x1,_height1+_gap1,4,49-_height1-_gap1,FILL_BLACK); |
2016110307 | 0:97418ec4c37d | 123 | lcd.drawRect(_x2,0,4,_b,FILL_BLACK); |
2016110307 | 0:97418ec4c37d | 124 | lcd.drawRect(_x2, _height2+_gap2,4,49-_height2-_gap2,FILL_BLACK); |
2016110307 | 0:97418ec4c37d | 125 | lcd.drawRect(_x3,0,4,_c,FILL_BLACK); |
2016110307 | 0:97418ec4c37d | 126 | lcd.drawRect(_x3, _height3+_gap3, 4, 49-_height3-_gap3, FILL_BLACK); |
2016110307 | 0:97418ec4c37d | 127 | |
2016110307 | 0:97418ec4c37d | 128 | } |
2016110307 | 0:97418ec4c37d | 129 | |
2016110307 | 0:97418ec4c37d | 130 | void Cylinder::update() |
2016110307 | 0:97418ec4c37d | 131 | { |
2016110307 | 1:85ab0d979b57 | 132 | _x1 -= _speed; |
2016110307 | 1:85ab0d979b57 | 133 | _x2 -= _speed; |
2016110307 | 1:85ab0d979b57 | 134 | _x3 -= _speed; |
2016110307 | 0:97418ec4c37d | 135 | |
2016110307 | 0:97418ec4c37d | 136 | } |
2016110307 | 0:97418ec4c37d | 137 | |
2016110307 | 0:97418ec4c37d | 138 | void Cylinder::check() |
2016110307 | 0:97418ec4c37d | 139 | { |
2016110307 | 1:85ab0d979b57 | 140 | if(_state == 0 ) |
2016110307 | 1:85ab0d979b57 | 141 | { |
2016110307 | 0:97418ec4c37d | 142 | if(_x1 < 0) { |
2016110307 | 0:97418ec4c37d | 143 | _x1 = 86; |
2016110307 | 0:97418ec4c37d | 144 | _height1 = rand()%11+10; |
2016110307 | 0:97418ec4c37d | 145 | _gap1 = rand()%11+15; |
2016110307 | 0:97418ec4c37d | 146 | _score += 1; |
2016110307 | 0:97418ec4c37d | 147 | } |
2016110307 | 0:97418ec4c37d | 148 | if(_x2 < 0) { |
2016110307 | 0:97418ec4c37d | 149 | _x2 = 86; |
2016110307 | 0:97418ec4c37d | 150 | _height2 = rand()%11+10; |
2016110307 | 0:97418ec4c37d | 151 | _gap2 = rand()%11+15; |
2016110307 | 0:97418ec4c37d | 152 | _score += 1; |
2016110307 | 0:97418ec4c37d | 153 | } |
2016110307 | 0:97418ec4c37d | 154 | if(_x3 < 0) { |
2016110307 | 0:97418ec4c37d | 155 | _x3 = 86; |
2016110307 | 0:97418ec4c37d | 156 | _height3 = rand()%11+10; |
2016110307 | 0:97418ec4c37d | 157 | _gap3 = rand()%11+15; |
2016110307 | 0:97418ec4c37d | 158 | _score += 1; |
2016110307 | 0:97418ec4c37d | 159 | } |
2016110307 | 1:85ab0d979b57 | 160 | } |
2016110307 | 1:85ab0d979b57 | 161 | if(_state == 1) { |
2016110307 | 1:85ab0d979b57 | 162 | if(_x1 < 0) { |
2016110307 | 1:85ab0d979b57 | 163 | _x1 = 86; |
2016110307 | 1:85ab0d979b57 | 164 | _height1 = rand()%9+10; |
2016110307 | 1:85ab0d979b57 | 165 | _gap1 = rand()%3+13; |
2016110307 | 1:85ab0d979b57 | 166 | _score += 1; |
2016110307 | 1:85ab0d979b57 | 167 | } |
2016110307 | 1:85ab0d979b57 | 168 | if(_x2 < 0) { |
2016110307 | 1:85ab0d979b57 | 169 | _x2 = 86; |
2016110307 | 1:85ab0d979b57 | 170 | _height2 = rand()%9+10; |
2016110307 | 1:85ab0d979b57 | 171 | _gap2 = rand()%3+13; |
2016110307 | 1:85ab0d979b57 | 172 | _score += 1; |
2016110307 | 1:85ab0d979b57 | 173 | } |
2016110307 | 1:85ab0d979b57 | 174 | if(_x3 < 0) { |
2016110307 | 1:85ab0d979b57 | 175 | _x3 = 86; |
2016110307 | 1:85ab0d979b57 | 176 | _height3 = rand()%9+10; |
2016110307 | 1:85ab0d979b57 | 177 | _gap3 = rand()%3+13; |
2016110307 | 1:85ab0d979b57 | 178 | _score += 1; |
2016110307 | 1:85ab0d979b57 | 179 | } |
2016110307 | 1:85ab0d979b57 | 180 | } |
2016110307 | 0:97418ec4c37d | 181 | } |
2016110307 | 0:97418ec4c37d | 182 | |
2016110307 | 0:97418ec4c37d | 183 | void Cylinder::print_score(N5110 &lcd) |
2016110307 | 0:97418ec4c37d | 184 | { |
2016110307 | 0:97418ec4c37d | 185 | _yourscore = _score; |
2016110307 | 0:97418ec4c37d | 186 | char buffer[1]; |
2016110307 | 0:97418ec4c37d | 187 | int score = _yourscore; |
2016110307 | 0:97418ec4c37d | 188 | sprintf(buffer, "%2d", score); |
2016110307 | 1:85ab0d979b57 | 189 | lcd.printString(buffer, 65,1); |
2016110307 | 0:97418ec4c37d | 190 | |
2016110307 | 0:97418ec4c37d | 191 | } |
2016110307 | 0:97418ec4c37d | 192 | |
2016110307 | 0:97418ec4c37d | 193 | void Cylinder::print_yourscore(N5110 &lcd) |
2016110307 | 0:97418ec4c37d | 194 | { |
2016110307 | 0:97418ec4c37d | 195 | char buffer2[14]; |
2016110307 | 0:97418ec4c37d | 196 | int yourscore = _yourscore; |
2016110307 | 0:97418ec4c37d | 197 | sprintf(buffer2,"YourScore = %2d ",yourscore); |
2016110307 | 0:97418ec4c37d | 198 | lcd.printString(buffer2,0,1); |
2016110307 | 0:97418ec4c37d | 199 | } |
2016110307 | 0:97418ec4c37d | 200 | |
2016110307 | 0:97418ec4c37d | 201 | int Cylinder::get_highest_score(int high_score) |
2016110307 | 0:97418ec4c37d | 202 | { |
2016110307 | 0:97418ec4c37d | 203 | if (high_score <= _yourscore) { |
2016110307 | 0:97418ec4c37d | 204 | high_score = _yourscore; |
2016110307 | 0:97418ec4c37d | 205 | } |
2016110307 | 0:97418ec4c37d | 206 | return high_score; |
2016110307 | 0:97418ec4c37d | 207 | } |
2016110307 | 0:97418ec4c37d | 208 | |
2016110307 | 0:97418ec4c37d | 209 | |
2016110307 | 0:97418ec4c37d | 210 | Data Cylinder::get_data() |
2016110307 | 0:97418ec4c37d | 211 | { |
2016110307 | 0:97418ec4c37d | 212 | _data.x1 = _x1; |
2016110307 | 0:97418ec4c37d | 213 | _data.x2 = _x2; |
2016110307 | 0:97418ec4c37d | 214 | _data.x3 = _x3; |
2016110307 | 0:97418ec4c37d | 215 | |
2016110307 | 0:97418ec4c37d | 216 | _data.height1 = _height1; |
2016110307 | 0:97418ec4c37d | 217 | _data.height2 = _height2; |
2016110307 | 0:97418ec4c37d | 218 | _data.height3 = _height3; |
2016110307 | 0:97418ec4c37d | 219 | |
2016110307 | 0:97418ec4c37d | 220 | _data.gap1 = _gap1; |
2016110307 | 0:97418ec4c37d | 221 | _data.gap2 = _gap2; |
2016110307 | 0:97418ec4c37d | 222 | _data.gap3 = _gap3; |
2016110307 | 0:97418ec4c37d | 223 | |
2016110307 | 0:97418ec4c37d | 224 | return _data; |
2016110307 | 0:97418ec4c37d | 225 | |
2016110307 | 0:97418ec4c37d | 226 | } |
2016110307 | 0:97418ec4c37d | 227 | |
2016110307 | 0:97418ec4c37d | 228 | |
2016110307 | 0:97418ec4c37d | 229 | |
2016110307 | 0:97418ec4c37d | 230 |