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