Ahmed Hedait / Mbed 2 deprecated el16ah

Dependencies:   mbed

Committer:
ahmedhedait
Date:
Tue May 08 12:55:17 2018 +0000
Revision:
22:745b4d352183
Parent:
21:bcc84d5cb068
Child:
25:28c57be06933
I divided the code now into different classes and it works perfectly.

Who changed what in which revision?

UserRevisionLine numberNew 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 20:041affa5e242 30 if (dir == N) {
ahmedhedait 20:041affa5e242 31 _circy -= _speed;
ahmedhedait 20:041affa5e242 32 } else if (dir == S) {
ahmedhedait 20:041affa5e242 33 _circy += _speed;
ahmedhedait 20:041affa5e242 34 }
ahmedhedait 20:041affa5e242 35
ahmedhedait 20:041affa5e242 36 if (dir == W) {
ahmedhedait 20:041affa5e242 37 _circx -= _speed;
ahmedhedait 20:041affa5e242 38 } else if (dir == E) {
ahmedhedait 20:041affa5e242 39 _circx += _speed;
ahmedhedait 20:041affa5e242 40 }
ahmedhedait 20:041affa5e242 41
ahmedhedait 20:041affa5e242 42 // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT OFF THE DIMENSIONS OF THE LCD SCREEN.
ahmedhedait 20:041affa5e242 43 if (_circy < 3) {
ahmedhedait 20:041affa5e242 44 _circy = 3;
ahmedhedait 20:041affa5e242 45 }
ahmedhedait 20:041affa5e242 46
ahmedhedait 20:041affa5e242 47 if (_circy > HEIGHT - 4) {
ahmedhedait 20:041affa5e242 48 _circy = HEIGHT - 4;
ahmedhedait 20:041affa5e242 49 }
ahmedhedait 20:041affa5e242 50
ahmedhedait 20:041affa5e242 51 if (_circx < 3) {
ahmedhedait 20:041affa5e242 52 _circx = 3;
ahmedhedait 20:041affa5e242 53 }
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 22:745b4d352183 64 // FOR LOOP TO STOP BALL FROM GOING THROUGH WALLS
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 22:745b4d352183 156
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 22:745b4d352183 163 printf("COLLISION");
ahmedhedait 22:745b4d352183 164 //left
ahmedhedait 22:745b4d352183 165 if (_circx <= _a - 2) {
ahmedhedait 22:745b4d352183 166 if(_circx >= _a - 3) {
ahmedhedait 22:745b4d352183 167 _circx = _a - 3;
ahmedhedait 22:745b4d352183 168 }
ahmedhedait 22:745b4d352183 169 }
ahmedhedait 22:745b4d352183 170
ahmedhedait 22:745b4d352183 171 //right
ahmedhedait 22:745b4d352183 172 if(_circx >= _a + 2) {
ahmedhedait 22:745b4d352183 173 if(_circx <= _a + 3) {
ahmedhedait 22:745b4d352183 174 _circx = _a + 3;
ahmedhedait 22:745b4d352183 175 }
ahmedhedait 22:745b4d352183 176 }
ahmedhedait 22:745b4d352183 177
ahmedhedait 22:745b4d352183 178 //top
ahmedhedait 22:745b4d352183 179 if(_circy <= _b - 2) {
ahmedhedait 22:745b4d352183 180 if(_circy >= _b - 3) {
ahmedhedait 22:745b4d352183 181 _circy = _b - 3;
ahmedhedait 22:745b4d352183 182 }
ahmedhedait 22:745b4d352183 183 }
ahmedhedait 22:745b4d352183 184
ahmedhedait 22:745b4d352183 185 //bottom
ahmedhedait 22:745b4d352183 186 if(_circy >= _b + _d) {
ahmedhedait 22:745b4d352183 187 if(_circy <= 2 + _b + _d) {
ahmedhedait 22:745b4d352183 188 (_circy = 2 + _b + _d);
ahmedhedait 22:745b4d352183 189 }
ahmedhedait 22:745b4d352183 190 }
ahmedhedait 22:745b4d352183 191 }
ahmedhedait 22:745b4d352183 192 }
ahmedhedait 22:745b4d352183 193
ahmedhedait 22:745b4d352183 194
ahmedhedait 22:745b4d352183 195 // WHEN THE BALL REACHES THE Y-AXIS NEEDED WHICH IS 27, THEN THE JOYSTICK FREELY MOVE THE BALL RIGHT THROUGH THE OPENING OF THE SMAZE WALL,
ahmedhedait 22:745b4d352183 196 // HOWEVER, IF THE BALL IS NOT EQUAL TO THE Y-AXIS NEEDED, THEN THE BALL MUST BE RESTRICTED TO MOVING SO THAT IT DOES NOT PASS THE WALLS.
ahmedhedait 22:745b4d352183 197 if (_circy == 27) {
ahmedhedait 22:745b4d352183 198 if (_circx > WIDTH) {
ahmedhedait 22:745b4d352183 199 _circx = WIDTH;
ahmedhedait 22:745b4d352183 200 }
ahmedhedait 22:745b4d352183 201 } else if (_circx > 80) {
ahmedhedait 22:745b4d352183 202 _circx = 80;
ahmedhedait 22:745b4d352183 203 }
ahmedhedait 17:68d4b4095d80 204 }