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.
Dependencies: mbed
Ball/Ball.cpp@10:6c6e09023942, 2019-04-22 (annotated)
- Committer:
- shahidsajid
- Date:
- Mon Apr 22 17:34:11 2019 +0000
- Revision:
- 10:6c6e09023942
- Parent:
- 9:a81db6a703b7
- Child:
- 11:f481ec642cc5
Created a new folder called Cricket to modularize the code for efficiency
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shahidsajid | 3:bd3465a70a5a | 1 | #include "Ball.h" |
shahidsajid | 3:bd3465a70a5a | 2 | |
shahidsajid | 4:55a0509c4874 | 3 | |
shahidsajid | 3:bd3465a70a5a | 4 | Ball::Ball() |
shahidsajid | 3:bd3465a70a5a | 5 | { |
shahidsajid | 3:bd3465a70a5a | 6 | |
shahidsajid | 3:bd3465a70a5a | 7 | } |
shahidsajid | 3:bd3465a70a5a | 8 | |
shahidsajid | 3:bd3465a70a5a | 9 | Ball::~Ball() |
shahidsajid | 3:bd3465a70a5a | 10 | { |
shahidsajid | 3:bd3465a70a5a | 11 | |
shahidsajid | 3:bd3465a70a5a | 12 | } |
shahidsajid | 3:bd3465a70a5a | 13 | |
shahidsajid | 3:bd3465a70a5a | 14 | void Ball::init(int size,int speed) |
shahidsajid | 3:bd3465a70a5a | 15 | { |
shahidsajid | 3:bd3465a70a5a | 16 | _size = size; |
shahidsajid | 7:a1a6bff238c1 | 17 | _x1=42; |
shahidsajid | 7:a1a6bff238c1 | 18 | _y1=3; |
shahidsajid | 3:bd3465a70a5a | 19 | _x = 42; |
shahidsajid | 3:bd3465a70a5a | 20 | _y = 15; |
shahidsajid | 6:3e50f2cf4366 | 21 | bat.init(3,4); |
shahidsajid | 9:a81db6a703b7 | 22 | bowled=0; |
shahidsajid | 3:bd3465a70a5a | 23 | |
shahidsajid | 3:bd3465a70a5a | 24 | } |
shahidsajid | 7:a1a6bff238c1 | 25 | int Ball::bowler_start(){ |
shahidsajid | 7:a1a6bff238c1 | 26 | if(_y1!=15){ |
shahidsajid | 7:a1a6bff238c1 | 27 | _y1++; |
shahidsajid | 7:a1a6bff238c1 | 28 | return 0; |
shahidsajid | 7:a1a6bff238c1 | 29 | } |
shahidsajid | 7:a1a6bff238c1 | 30 | else { |
shahidsajid | 7:a1a6bff238c1 | 31 | return 1; |
shahidsajid | 7:a1a6bff238c1 | 32 | } |
shahidsajid | 7:a1a6bff238c1 | 33 | } |
shahidsajid | 10:6c6e09023942 | 34 | Vector2D Ball::get_pos() |
shahidsajid | 10:6c6e09023942 | 35 | { |
shahidsajid | 10:6c6e09023942 | 36 | Vector2D p = {_x,_y}; |
shahidsajid | 10:6c6e09023942 | 37 | return p; |
shahidsajid | 10:6c6e09023942 | 38 | } |
shahidsajid | 10:6c6e09023942 | 39 | |
shahidsajid | 10:6c6e09023942 | 40 | void Ball::set_pos(Vector2D p) |
shahidsajid | 10:6c6e09023942 | 41 | { |
shahidsajid | 10:6c6e09023942 | 42 | _x = p.x; |
shahidsajid | 10:6c6e09023942 | 43 | _y = p.y; |
shahidsajid | 10:6c6e09023942 | 44 | } |
shahidsajid | 10:6c6e09023942 | 45 | /* |
shahidsajid | 10:6c6e09023942 | 46 | void Ball::ball_start(N5110 &lcd,int checkHit, Direction dir){ |
shahidsajid | 10:6c6e09023942 | 47 | int bowler_start_check=bowler_start(); |
shahidsajid | 10:6c6e09023942 | 48 | if (bowler_start_check==1){ |
shahidsajid | 9:a81db6a703b7 | 49 | if (_y!=36){ |
shahidsajid | 10:6c6e09023942 | 50 | _y+=1; |
shahidsajid | 10:6c6e09023942 | 51 | return 0; |
shahidsajid | 9:a81db6a703b7 | 52 | } |
shahidsajid | 10:6c6e09023942 | 53 | else{ |
shahidsajid | 7:a1a6bff238c1 | 54 | bowled=1; |
shahidsajid | 10:6c6e09023942 | 55 | return 1; |
shahidsajid | 7:a1a6bff238c1 | 56 | } |
shahidsajid | 10:6c6e09023942 | 57 | } |
shahidsajid | 10:6c6e09023942 | 58 | else{ |
shahidsajid | 10:6c6e09023942 | 59 | return 0; |
shahidsajid | 7:a1a6bff238c1 | 60 | } |
shahidsajid | 7:a1a6bff238c1 | 61 | } |
shahidsajid | 10:6c6e09023942 | 62 | */ |
shahidsajid | 7:a1a6bff238c1 | 63 | void Ball::draw(N5110 &lcd) |
shahidsajid | 7:a1a6bff238c1 | 64 | { |
shahidsajid | 7:a1a6bff238c1 | 65 | lcd.drawCircle(_x1,_y1,2,FILL_BLACK); |
shahidsajid | 10:6c6e09023942 | 66 | lcd.drawCircle(_x,_y,2,FILL_TRANSPARENT); |
shahidsajid | 7:a1a6bff238c1 | 67 | //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
shahidsajid | 7:a1a6bff238c1 | 68 | } |
shahidsajid | 10:6c6e09023942 | 69 | /* |
shahidsajid | 7:a1a6bff238c1 | 70 | void Ball::update_ball(int checkHit, Direction dir){ |
shahidsajid | 9:a81db6a703b7 | 71 | if (direction_set!=1){ |
shahidsajid | 9:a81db6a703b7 | 72 | ball_direction=dir; |
shahidsajid | 9:a81db6a703b7 | 73 | direction_set=1; |
shahidsajid | 9:a81db6a703b7 | 74 | } |
shahidsajid | 9:a81db6a703b7 | 75 | if (ballHit!=1 && ballHit!=1){ |
shahidsajid | 7:a1a6bff238c1 | 76 | ballHit=1; |
shahidsajid | 7:a1a6bff238c1 | 77 | } |
shahidsajid | 7:a1a6bff238c1 | 78 | if(bowled==1 && ballHit==1){ |
shahidsajid | 6:3e50f2cf4366 | 79 | printf("TEST\n"); |
shahidsajid | 9:a81db6a703b7 | 80 | if (ball_direction==NE){ |
shahidsajid | 6:3e50f2cf4366 | 81 | printf("x=%i y=%i \n",_x,_y); |
shahidsajid | 6:3e50f2cf4366 | 82 | if (!(_y == 2 )){ |
shahidsajid | 6:3e50f2cf4366 | 83 | _y--; |
shahidsajid | 6:3e50f2cf4366 | 84 | } |
shahidsajid | 6:3e50f2cf4366 | 85 | if (_x!=65){ |
shahidsajid | 6:3e50f2cf4366 | 86 | _x++; |
shahidsajid | 6:3e50f2cf4366 | 87 | } |
shahidsajid | 6:3e50f2cf4366 | 88 | printf("HERE"); |
shahidsajid | 6:3e50f2cf4366 | 89 | } |
shahidsajid | 6:3e50f2cf4366 | 90 | } |
shahidsajid | 7:a1a6bff238c1 | 91 | |
shahidsajid | 3:bd3465a70a5a | 92 | } |
shahidsajid | 10:6c6e09023942 | 93 | */ |
shahidsajid | 9:a81db6a703b7 | 94 | void Ball::reset(){ |
shahidsajid | 9:a81db6a703b7 | 95 | bowled=0; |
shahidsajid | 9:a81db6a703b7 | 96 | ballHit=0; |
shahidsajid | 9:a81db6a703b7 | 97 | } |
shahidsajid | 9:a81db6a703b7 | 98 | |
shahidsajid | 10:6c6e09023942 | 99 | /* |
shahidsajid | 9:a81db6a703b7 | 100 | void Ball::draw_field(N5110 &lcd){ |
shahidsajid | 9:a81db6a703b7 | 101 | lcd.drawCircle(field[0].x,field[0].y,2,FILL_BLACK); |
shahidsajid | 9:a81db6a703b7 | 102 | lcd.drawCircle(field[1].x,field[1].y,2,FILL_BLACK); |
shahidsajid | 9:a81db6a703b7 | 103 | lcd.drawCircle(field[2].x,field[2].y,2,FILL_BLACK); |
shahidsajid | 9:a81db6a703b7 | 104 | lcd.drawCircle(field[3].x,field[3].y,2,FILL_BLACK); |
shahidsajid | 9:a81db6a703b7 | 105 | } |
shahidsajid | 3:bd3465a70a5a | 106 | void Ball::set_field(N5110 &lcd){ |
shahidsajid | 4:55a0509c4874 | 107 | int d=0; |
shahidsajid | 10:6c6e09023942 | 108 | int fieldersCount=0; |
shahidsajid | 9:a81db6a703b7 | 109 | int pos=0; |
shahidsajid | 10:6c6e09023942 | 110 | int i=0; |
shahidsajid | 10:6c6e09023942 | 111 | int continueCount=0; |
shahidsajid | 5:915bcac4e9b9 | 112 | srand(time(NULL)); |
shahidsajid | 10:6c6e09023942 | 113 | while (fieldersCount!=5){ |
shahidsajid | 10:6c6e09023942 | 114 | d = 1+ rand() % 6; |
shahidsajid | 10:6c6e09023942 | 115 | printf("DIRECTION %i\n",d); |
shahidsajid | 9:a81db6a703b7 | 116 | pos=rand()%2; |
shahidsajid | 9:a81db6a703b7 | 117 | for (int j=0;j<10;j++){ |
shahidsajid | 10:6c6e09023942 | 118 | if (fieldNumbers[j]==d){ |
shahidsajid | 10:6c6e09023942 | 119 | printf("CHECK"); |
shahidsajid | 10:6c6e09023942 | 120 | continueCount=1; |
shahidsajid | 10:6c6e09023942 | 121 | } |
shahidsajid | 10:6c6e09023942 | 122 | } |
shahidsajid | 10:6c6e09023942 | 123 | if (continueCount==1){ |
shahidsajid | 10:6c6e09023942 | 124 | printf("IM HERE"); |
shahidsajid | 10:6c6e09023942 | 125 | continueCount=0; |
shahidsajid | 10:6c6e09023942 | 126 | continue; |
shahidsajid | 10:6c6e09023942 | 127 | } |
shahidsajid | 10:6c6e09023942 | 128 | printf("YESS"); |
shahidsajid | 10:6c6e09023942 | 129 | if (d==1){ //NW |
shahidsajid | 9:a81db6a703b7 | 130 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 131 | field[i].dir=NW; |
shahidsajid | 9:a81db6a703b7 | 132 | field[i].x=30; |
shahidsajid | 9:a81db6a703b7 | 133 | field[i].y=9; |
shahidsajid | 9:a81db6a703b7 | 134 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 135 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 136 | i++; |
shahidsajid | 10:6c6e09023942 | 137 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 138 | } |
shahidsajid | 9:a81db6a703b7 | 139 | else{ |
shahidsajid | 10:6c6e09023942 | 140 | field[i].dir=NW; |
shahidsajid | 9:a81db6a703b7 | 141 | field[i].x=25; |
shahidsajid | 9:a81db6a703b7 | 142 | field[i].y=2; |
shahidsajid | 9:a81db6a703b7 | 143 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 144 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 145 | i++; |
shahidsajid | 10:6c6e09023942 | 146 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 147 | } |
shahidsajid | 9:a81db6a703b7 | 148 | } |
shahidsajid | 9:a81db6a703b7 | 149 | |
shahidsajid | 9:a81db6a703b7 | 150 | |
shahidsajid | 9:a81db6a703b7 | 151 | if (d==2){ //NE |
shahidsajid | 9:a81db6a703b7 | 152 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 153 | field[i].dir=NE; |
shahidsajid | 9:a81db6a703b7 | 154 | field[i].x=55; |
shahidsajid | 9:a81db6a703b7 | 155 | field[i].y=9; |
shahidsajid | 4:55a0509c4874 | 156 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 157 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 158 | i++; |
shahidsajid | 10:6c6e09023942 | 159 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 160 | } |
shahidsajid | 9:a81db6a703b7 | 161 | else{ |
shahidsajid | 10:6c6e09023942 | 162 | field[i].dir=NE; |
shahidsajid | 9:a81db6a703b7 | 163 | field[i].x=65; |
shahidsajid | 9:a81db6a703b7 | 164 | field[i].y=2; |
shahidsajid | 9:a81db6a703b7 | 165 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 166 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 167 | i++; |
shahidsajid | 10:6c6e09023942 | 168 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 169 | } |
shahidsajid | 4:55a0509c4874 | 170 | } |
shahidsajid | 9:a81db6a703b7 | 171 | |
shahidsajid | 9:a81db6a703b7 | 172 | |
shahidsajid | 4:55a0509c4874 | 173 | if (d==3){ //SW |
shahidsajid | 9:a81db6a703b7 | 174 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 175 | field[i].dir=SW; |
shahidsajid | 9:a81db6a703b7 | 176 | field[i].x=33; |
shahidsajid | 9:a81db6a703b7 | 177 | field[i].y=40; |
shahidsajid | 9:a81db6a703b7 | 178 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 179 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 180 | i++; |
shahidsajid | 10:6c6e09023942 | 181 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 182 | } |
shahidsajid | 9:a81db6a703b7 | 183 | |
shahidsajid | 9:a81db6a703b7 | 184 | else{ |
shahidsajid | 10:6c6e09023942 | 185 | field[i].dir=SW; |
shahidsajid | 9:a81db6a703b7 | 186 | field[i].x=25; |
shahidsajid | 9:a81db6a703b7 | 187 | field[i].y=44; |
shahidsajid | 9:a81db6a703b7 | 188 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 189 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 190 | i++; |
shahidsajid | 10:6c6e09023942 | 191 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 192 | } |
shahidsajid | 4:55a0509c4874 | 193 | } |
shahidsajid | 9:a81db6a703b7 | 194 | |
shahidsajid | 5:915bcac4e9b9 | 195 | if (d==4){ //SE |
shahidsajid | 9:a81db6a703b7 | 196 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 197 | field[i].dir=SE; |
shahidsajid | 9:a81db6a703b7 | 198 | field[i].x=55; |
shahidsajid | 9:a81db6a703b7 | 199 | field[i].y=40; |
shahidsajid | 9:a81db6a703b7 | 200 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 201 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 202 | i++; |
shahidsajid | 10:6c6e09023942 | 203 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 204 | } |
shahidsajid | 9:a81db6a703b7 | 205 | else{ |
shahidsajid | 10:6c6e09023942 | 206 | field[i].dir=SE; |
shahidsajid | 9:a81db6a703b7 | 207 | field[i].x=65; |
shahidsajid | 9:a81db6a703b7 | 208 | field[i].y=44; |
shahidsajid | 9:a81db6a703b7 | 209 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 210 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 211 | i++; |
shahidsajid | 10:6c6e09023942 | 212 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 213 | } |
shahidsajid | 5:915bcac4e9b9 | 214 | } |
shahidsajid | 9:a81db6a703b7 | 215 | |
shahidsajid | 9:a81db6a703b7 | 216 | if (d==5){ //W |
shahidsajid | 9:a81db6a703b7 | 217 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 218 | field[i].dir=W; |
shahidsajid | 9:a81db6a703b7 | 219 | field[i].x=3; |
shahidsajid | 9:a81db6a703b7 | 220 | field[i].y=25; |
shahidsajid | 9:a81db6a703b7 | 221 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 222 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 223 | i++; |
shahidsajid | 10:6c6e09023942 | 224 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 225 | } |
shahidsajid | 9:a81db6a703b7 | 226 | else{ |
shahidsajid | 10:6c6e09023942 | 227 | field[i].dir=W; |
shahidsajid | 9:a81db6a703b7 | 228 | field[i].x=25; |
shahidsajid | 9:a81db6a703b7 | 229 | field[i].y=25; |
shahidsajid | 9:a81db6a703b7 | 230 | field[i].position=0; |
shahidsajid | 10:6c6e09023942 | 231 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 232 | i++; |
shahidsajid | 10:6c6e09023942 | 233 | fieldersCount++; |
shahidsajid | 9:a81db6a703b7 | 234 | } |
shahidsajid | 5:915bcac4e9b9 | 235 | } |
shahidsajid | 9:a81db6a703b7 | 236 | |
shahidsajid | 9:a81db6a703b7 | 237 | if (d==6){ //W |
shahidsajid | 10:6c6e09023942 | 238 | if (pos==1){ |
shahidsajid | 10:6c6e09023942 | 239 | field[i].dir=E; |
shahidsajid | 10:6c6e09023942 | 240 | field[i].x=80; |
shahidsajid | 10:6c6e09023942 | 241 | field[i].y=20; |
shahidsajid | 10:6c6e09023942 | 242 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 243 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 244 | i++; |
shahidsajid | 10:6c6e09023942 | 245 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 246 | } |
shahidsajid | 10:6c6e09023942 | 247 | else{ |
shahidsajid | 10:6c6e09023942 | 248 | field[i].dir=E; |
shahidsajid | 10:6c6e09023942 | 249 | field[i].x=50; |
shahidsajid | 10:6c6e09023942 | 250 | field[i].y=20; |
shahidsajid | 10:6c6e09023942 | 251 | field[i].position=1; |
shahidsajid | 10:6c6e09023942 | 252 | fieldNumbers[i]=d; |
shahidsajid | 10:6c6e09023942 | 253 | i++; |
shahidsajid | 10:6c6e09023942 | 254 | fieldersCount++; |
shahidsajid | 10:6c6e09023942 | 255 | } |
shahidsajid | 10:6c6e09023942 | 256 | |
shahidsajid | 5:915bcac4e9b9 | 257 | } |
shahidsajid | 9:a81db6a703b7 | 258 | |
shahidsajid | 4:55a0509c4874 | 259 | } |
shahidsajid | 9:a81db6a703b7 | 260 | |
shahidsajid | 4:55a0509c4874 | 261 | |
shahidsajid | 4:55a0509c4874 | 262 | } |
shahidsajid | 10:6c6e09023942 | 263 | */ |