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