Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ball/Ball.cpp@25:28c57be06933, 2018-05-08 (annotated)
- Committer:
- ahmedhedait
- Date:
- Tue May 08 14:20:24 2018 +0000
- Revision:
- 25:28c57be06933
- Parent:
- 22:745b4d352183
I have re-adjusted my last comments so the code to be completed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ahmedhedait | 17:68d4b4095d80 | 1 | #include "Ball.h" |
ahmedhedait | 17:68d4b4095d80 | 2 | |
ahmedhedait | 17:68d4b4095d80 | 3 | // nothing doing in the constructor and destructor |
ahmedhedait | 17:68d4b4095d80 | 4 | Ball::Ball() |
ahmedhedait | 17:68d4b4095d80 | 5 | { |
ahmedhedait | 17:68d4b4095d80 | 6 | |
ahmedhedait | 17:68d4b4095d80 | 7 | } |
ahmedhedait | 17:68d4b4095d80 | 8 | |
ahmedhedait | 17:68d4b4095d80 | 9 | Ball::~Ball() |
ahmedhedait | 17:68d4b4095d80 | 10 | { |
ahmedhedait | 17:68d4b4095d80 | 11 | |
ahmedhedait | 20:041affa5e242 | 12 | } |
ahmedhedait | 20:041affa5e242 | 13 | |
ahmedhedait | 20:041affa5e242 | 14 | void Ball::init() |
ahmedhedait | 20:041affa5e242 | 15 | { |
ahmedhedait | 20:041affa5e242 | 16 | _circy = 5; |
ahmedhedait | 20:041affa5e242 | 17 | _circx = 5; |
ahmedhedait | 20:041affa5e242 | 18 | _speed = 1; |
ahmedhedait | 20:041affa5e242 | 19 | |
ahmedhedait | 20:041affa5e242 | 20 | } |
ahmedhedait | 20:041affa5e242 | 21 | |
ahmedhedait | 20:041affa5e242 | 22 | void Ball::draw(N5110 &lcd) |
ahmedhedait | 20:041affa5e242 | 23 | { |
ahmedhedait | 20:041affa5e242 | 24 | // VERY SIMPLE CODE IN WHCIH I DREW THE BALL OF THE MAZE. |
ahmedhedait | 20:041affa5e242 | 25 | lcd.drawCircle(_circx,_circy,2,FILL_BLACK); |
ahmedhedait | 20:041affa5e242 | 26 | } |
ahmedhedait | 20:041affa5e242 | 27 | |
ahmedhedait | 20:041affa5e242 | 28 | void Ball::update(Direction dir) |
ahmedhedait | 20:041affa5e242 | 29 | { |
ahmedhedait | 25:28c57be06933 | 30 | // ADJUSTING THE SPEED OF THE BALL FOR THE 4 DIFFERENT DIRECTIONS. |
ahmedhedait | 20:041affa5e242 | 31 | if (dir == N) { |
ahmedhedait | 20:041affa5e242 | 32 | _circy -= _speed; |
ahmedhedait | 20:041affa5e242 | 33 | } else if (dir == S) { |
ahmedhedait | 20:041affa5e242 | 34 | _circy += _speed; |
ahmedhedait | 20:041affa5e242 | 35 | } |
ahmedhedait | 20:041affa5e242 | 36 | |
ahmedhedait | 20:041affa5e242 | 37 | if (dir == W) { |
ahmedhedait | 20:041affa5e242 | 38 | _circx -= _speed; |
ahmedhedait | 20:041affa5e242 | 39 | } else if (dir == E) { |
ahmedhedait | 20:041affa5e242 | 40 | _circx += _speed; |
ahmedhedait | 20:041affa5e242 | 41 | } |
ahmedhedait | 20:041affa5e242 | 42 | |
ahmedhedait | 25:28c57be06933 | 43 | // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT SURPASS THE DIMENSIONS OF THE LCD SCREEN. |
ahmedhedait | 20:041affa5e242 | 44 | if (_circy < 3) { |
ahmedhedait | 20:041affa5e242 | 45 | _circy = 3; |
ahmedhedait | 20:041affa5e242 | 46 | } |
ahmedhedait | 20:041affa5e242 | 47 | |
ahmedhedait | 20:041affa5e242 | 48 | if (_circy > HEIGHT - 4) { |
ahmedhedait | 20:041affa5e242 | 49 | _circy = HEIGHT - 4; |
ahmedhedait | 20:041affa5e242 | 50 | } |
ahmedhedait | 20:041affa5e242 | 51 | |
ahmedhedait | 20:041affa5e242 | 52 | if (_circx < 3) { |
ahmedhedait | 20:041affa5e242 | 53 | _circx = 3; |
ahmedhedait | 20:041affa5e242 | 54 | } |
ahmedhedait | 21:bcc84d5cb068 | 55 | } |
ahmedhedait | 21:bcc84d5cb068 | 56 | |
ahmedhedait | 21:bcc84d5cb068 | 57 | Vector2D Ball::get_pos() { |
ahmedhedait | 21:bcc84d5cb068 | 58 | Vector2D p = {_circx,_circy}; |
ahmedhedait | 21:bcc84d5cb068 | 59 | return p; |
ahmedhedait | 22:745b4d352183 | 60 | } |
ahmedhedait | 22:745b4d352183 | 61 | |
ahmedhedait | 22:745b4d352183 | 62 | void Ball::check_wall_collision(Gamepad &pad) |
ahmedhedait | 22:745b4d352183 | 63 | { |
ahmedhedait | 25:28c57be06933 | 64 | // FOR LOOP TO STOP BALL FROM GOING THROUGH WALLS OF THE MAZE. |
ahmedhedait | 22:745b4d352183 | 65 | for (int i = 0; i <= 12; i++) { |
ahmedhedait | 22:745b4d352183 | 66 | //printf(" Stage %d\n", i); |
ahmedhedait | 22:745b4d352183 | 67 | |
ahmedhedait | 22:745b4d352183 | 68 | if(i == 0) { // SET VARIABLES FOR EACH WALL |
ahmedhedait | 22:745b4d352183 | 69 | _a = 10; |
ahmedhedait | 22:745b4d352183 | 70 | _b = 0; |
ahmedhedait | 22:745b4d352183 | 71 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 72 | _d = 39; |
ahmedhedait | 22:745b4d352183 | 73 | |
ahmedhedait | 22:745b4d352183 | 74 | } else if (i == 1) { |
ahmedhedait | 22:745b4d352183 | 75 | _a = 18; |
ahmedhedait | 22:745b4d352183 | 76 | _b = 32; |
ahmedhedait | 22:745b4d352183 | 77 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 78 | _d = 15; |
ahmedhedait | 22:745b4d352183 | 79 | } |
ahmedhedait | 22:745b4d352183 | 80 | |
ahmedhedait | 22:745b4d352183 | 81 | else if (i == 2) { |
ahmedhedait | 22:745b4d352183 | 82 | _a = 36; |
ahmedhedait | 22:745b4d352183 | 83 | _b = 25; |
ahmedhedait | 22:745b4d352183 | 84 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 85 | _d = 25; |
ahmedhedait | 22:745b4d352183 | 86 | } |
ahmedhedait | 22:745b4d352183 | 87 | |
ahmedhedait | 22:745b4d352183 | 88 | else if(i == 3) { |
ahmedhedait | 22:745b4d352183 | 89 | _a = 45; |
ahmedhedait | 22:745b4d352183 | 90 | _b = 0; |
ahmedhedait | 22:745b4d352183 | 91 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 92 | _d = 11; |
ahmedhedait | 22:745b4d352183 | 93 | } |
ahmedhedait | 22:745b4d352183 | 94 | |
ahmedhedait | 22:745b4d352183 | 95 | else if (i == 4) { |
ahmedhedait | 22:745b4d352183 | 96 | _a = 45; |
ahmedhedait | 22:745b4d352183 | 97 | _b = 18; |
ahmedhedait | 22:745b4d352183 | 98 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 99 | _d = 30; |
ahmedhedait | 22:745b4d352183 | 100 | } |
ahmedhedait | 22:745b4d352183 | 101 | |
ahmedhedait | 22:745b4d352183 | 102 | else if (i == 5) { |
ahmedhedait | 22:745b4d352183 | 103 | _a = 55; |
ahmedhedait | 22:745b4d352183 | 104 | _b = 6; |
ahmedhedait | 22:745b4d352183 | 105 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 106 | _d = 45; |
ahmedhedait | 22:745b4d352183 | 107 | } |
ahmedhedait | 22:745b4d352183 | 108 | |
ahmedhedait | 22:745b4d352183 | 109 | else if (i == 6) { |
ahmedhedait | 22:745b4d352183 | 110 | _a = 64; |
ahmedhedait | 22:745b4d352183 | 111 | _b = 0; |
ahmedhedait | 22:745b4d352183 | 112 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 113 | _d = 20; |
ahmedhedait | 22:745b4d352183 | 114 | } |
ahmedhedait | 22:745b4d352183 | 115 | |
ahmedhedait | 22:745b4d352183 | 116 | else if (i == 7) { |
ahmedhedait | 22:745b4d352183 | 117 | _a = 64; |
ahmedhedait | 22:745b4d352183 | 118 | _b = 27; |
ahmedhedait | 22:745b4d352183 | 119 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 120 | _d = 13; |
ahmedhedait | 22:745b4d352183 | 121 | } |
ahmedhedait | 22:745b4d352183 | 122 | |
ahmedhedait | 22:745b4d352183 | 123 | else if (i == 8) { |
ahmedhedait | 22:745b4d352183 | 124 | _a = 72; |
ahmedhedait | 22:745b4d352183 | 125 | _b = 10; |
ahmedhedait | 22:745b4d352183 | 126 | _c = 1; |
ahmedhedait | 22:745b4d352183 | 127 | _d = 30; |
ahmedhedait | 22:745b4d352183 | 128 | } |
ahmedhedait | 22:745b4d352183 | 129 | |
ahmedhedait | 22:745b4d352183 | 130 | else if (i == 9) { |
ahmedhedait | 22:745b4d352183 | 131 | _a = 18; |
ahmedhedait | 22:745b4d352183 | 132 | _b = 25; |
ahmedhedait | 22:745b4d352183 | 133 | _c = 18; |
ahmedhedait | 22:745b4d352183 | 134 | _d = 1; |
ahmedhedait | 22:745b4d352183 | 135 | } else if (i == 10) { |
ahmedhedait | 22:745b4d352183 | 136 | _a = 18; |
ahmedhedait | 22:745b4d352183 | 137 | _b = 18; |
ahmedhedait | 22:745b4d352183 | 138 | _c = 27; |
ahmedhedait | 22:745b4d352183 | 139 | _d = 1; |
ahmedhedait | 22:745b4d352183 | 140 | } |
ahmedhedait | 22:745b4d352183 | 141 | |
ahmedhedait | 22:745b4d352183 | 142 | else if (i == 11) { |
ahmedhedait | 22:745b4d352183 | 143 | _a = 18; |
ahmedhedait | 22:745b4d352183 | 144 | _b = 10; |
ahmedhedait | 22:745b4d352183 | 145 | _c = 27; |
ahmedhedait | 22:745b4d352183 | 146 | _d = 1; |
ahmedhedait | 22:745b4d352183 | 147 | } |
ahmedhedait | 22:745b4d352183 | 148 | |
ahmedhedait | 22:745b4d352183 | 149 | else if (i == 12) { |
ahmedhedait | 22:745b4d352183 | 150 | _a = 64; |
ahmedhedait | 22:745b4d352183 | 151 | _b = 40; |
ahmedhedait | 22:745b4d352183 | 152 | _c = 20; |
ahmedhedait | 22:745b4d352183 | 153 | _d = 1; |
ahmedhedait | 22:745b4d352183 | 154 | } |
ahmedhedait | 22:745b4d352183 | 155 | |
ahmedhedait | 25:28c57be06933 | 156 | // ADDED A PRINT FUNCTION TO DETECT IF COLLISION OCCURED BETWEEN BALL AND WALL OR NOT IN THE GIVEN DIMENSIONS. |
ahmedhedait | 22:745b4d352183 | 157 | if ( |
ahmedhedait | 22:745b4d352183 | 158 | (_circy >= _b - 2) && //top |
ahmedhedait | 22:745b4d352183 | 159 | (_circy <= 1 + _b + _d) && //bottom |
ahmedhedait | 22:745b4d352183 | 160 | (_circx >= _a - 2) && //left |
ahmedhedait | 22:745b4d352183 | 161 | (_circx <= _a + _c + 1) //right |
ahmedhedait | 22:745b4d352183 | 162 | ) { |
ahmedhedait | 25:28c57be06933 | 163 | // printf("COLLISION"); |
ahmedhedait | 25:28c57be06933 | 164 | |
ahmedhedait | 25:28c57be06933 | 165 | /* CONDITIONS FOR THE 4 DIRECTIONS FOR EACH RECTANGLE NEEDS TO BE MET SO THAT THE BALL DOES NOT PASS |
ahmedhedait | 25:28c57be06933 | 166 | THE DIFFERENT WALLS OF THE MAZE. */ |
ahmedhedait | 22:745b4d352183 | 167 | //left |
ahmedhedait | 22:745b4d352183 | 168 | if (_circx <= _a - 2) { |
ahmedhedait | 22:745b4d352183 | 169 | if(_circx >= _a - 3) { |
ahmedhedait | 22:745b4d352183 | 170 | _circx = _a - 3; |
ahmedhedait | 22:745b4d352183 | 171 | } |
ahmedhedait | 22:745b4d352183 | 172 | } |
ahmedhedait | 22:745b4d352183 | 173 | |
ahmedhedait | 22:745b4d352183 | 174 | //right |
ahmedhedait | 22:745b4d352183 | 175 | if(_circx >= _a + 2) { |
ahmedhedait | 22:745b4d352183 | 176 | if(_circx <= _a + 3) { |
ahmedhedait | 22:745b4d352183 | 177 | _circx = _a + 3; |
ahmedhedait | 22:745b4d352183 | 178 | } |
ahmedhedait | 22:745b4d352183 | 179 | } |
ahmedhedait | 22:745b4d352183 | 180 | |
ahmedhedait | 22:745b4d352183 | 181 | //top |
ahmedhedait | 22:745b4d352183 | 182 | if(_circy <= _b - 2) { |
ahmedhedait | 22:745b4d352183 | 183 | if(_circy >= _b - 3) { |
ahmedhedait | 22:745b4d352183 | 184 | _circy = _b - 3; |
ahmedhedait | 22:745b4d352183 | 185 | } |
ahmedhedait | 22:745b4d352183 | 186 | } |
ahmedhedait | 22:745b4d352183 | 187 | |
ahmedhedait | 22:745b4d352183 | 188 | //bottom |
ahmedhedait | 22:745b4d352183 | 189 | if(_circy >= _b + _d) { |
ahmedhedait | 22:745b4d352183 | 190 | if(_circy <= 2 + _b + _d) { |
ahmedhedait | 22:745b4d352183 | 191 | (_circy = 2 + _b + _d); |
ahmedhedait | 22:745b4d352183 | 192 | } |
ahmedhedait | 22:745b4d352183 | 193 | } |
ahmedhedait | 22:745b4d352183 | 194 | } |
ahmedhedait | 22:745b4d352183 | 195 | } |
ahmedhedait | 22:745b4d352183 | 196 | |
ahmedhedait | 22:745b4d352183 | 197 | |
ahmedhedait | 25:28c57be06933 | 198 | /* GAVE THE AXIS NEEEDED FOR THE BALL TO GET OUT AND ONLY OUT OF THE OPENING OF THE MAZE. WEHN THE CONDITIONS ARE |
ahmedhedait | 25:28c57be06933 | 199 | NOT MEANT, THEN IT RESTRICTS THE BALL FROM MOVING OUTSIDE THE MAZE */ |
ahmedhedait | 25:28c57be06933 | 200 | |
ahmedhedait | 22:745b4d352183 | 201 | if (_circy == 27) { |
ahmedhedait | 22:745b4d352183 | 202 | if (_circx > WIDTH) { |
ahmedhedait | 22:745b4d352183 | 203 | _circx = WIDTH; |
ahmedhedait | 22:745b4d352183 | 204 | } |
ahmedhedait | 22:745b4d352183 | 205 | } else if (_circx > 80) { |
ahmedhedait | 22:745b4d352183 | 206 | _circx = 80; |
ahmedhedait | 22:745b4d352183 | 207 | } |
ahmedhedait | 17:68d4b4095d80 | 208 | } |