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@8:7b7e1a5b8200, 2019-04-20 (annotated)
- Committer:
- shahidsajid
- Date:
- Sat Apr 20 20:32:56 2019 +0000
- Revision:
- 8:7b7e1a5b8200
- Parent:
- 7:a1a6bff238c1
- Child:
- 33:9d34ef219fff
Updated the set_field function to include more fielders;
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 | 3:bd3465a70a5a | 22 | |
shahidsajid | 3:bd3465a70a5a | 23 | srand(time(NULL)); |
shahidsajid | 3:bd3465a70a5a | 24 | int direction = rand() % 4; // randomise initial direction. |
shahidsajid | 3:bd3465a70a5a | 25 | |
shahidsajid | 3:bd3465a70a5a | 26 | // 4 possibilities. Get random modulo and set velocities accordingly |
shahidsajid | 3:bd3465a70a5a | 27 | if (direction == 0) { |
shahidsajid | 3:bd3465a70a5a | 28 | _velocity.x = speed; |
shahidsajid | 3:bd3465a70a5a | 29 | _velocity.y = speed; |
shahidsajid | 3:bd3465a70a5a | 30 | } else if (direction == 1) { |
shahidsajid | 3:bd3465a70a5a | 31 | _velocity.x = speed; |
shahidsajid | 3:bd3465a70a5a | 32 | _velocity.y = -speed; |
shahidsajid | 3:bd3465a70a5a | 33 | } else if (direction == 2) { |
shahidsajid | 3:bd3465a70a5a | 34 | _velocity.x = speed; |
shahidsajid | 3:bd3465a70a5a | 35 | _velocity.y = speed; |
shahidsajid | 3:bd3465a70a5a | 36 | } else { |
shahidsajid | 3:bd3465a70a5a | 37 | _velocity.x = -speed; |
shahidsajid | 3:bd3465a70a5a | 38 | _velocity.y = -speed; |
shahidsajid | 3:bd3465a70a5a | 39 | } |
shahidsajid | 3:bd3465a70a5a | 40 | } |
shahidsajid | 7:a1a6bff238c1 | 41 | int Ball::bowler_start(){ |
shahidsajid | 7:a1a6bff238c1 | 42 | if(_y1!=15){ |
shahidsajid | 7:a1a6bff238c1 | 43 | _y1++; |
shahidsajid | 7:a1a6bff238c1 | 44 | return 0; |
shahidsajid | 7:a1a6bff238c1 | 45 | } |
shahidsajid | 7:a1a6bff238c1 | 46 | else { |
shahidsajid | 7:a1a6bff238c1 | 47 | return 1; |
shahidsajid | 7:a1a6bff238c1 | 48 | } |
shahidsajid | 7:a1a6bff238c1 | 49 | } |
shahidsajid | 7:a1a6bff238c1 | 50 | void Ball::start(N5110 &lcd,int checkHit, Direction dir){ |
shahidsajid | 7:a1a6bff238c1 | 51 | int ball_start_check=bowler_start(); |
shahidsajid | 7:a1a6bff238c1 | 52 | if (ball_start_check==1){ |
shahidsajid | 7:a1a6bff238c1 | 53 | printf("HELLO\n"); |
shahidsajid | 7:a1a6bff238c1 | 54 | if (bowled==1){ |
shahidsajid | 7:a1a6bff238c1 | 55 | update_ball(checkHit, dir); |
shahidsajid | 7:a1a6bff238c1 | 56 | } |
shahidsajid | 7:a1a6bff238c1 | 57 | else if (_y!=36){ |
shahidsajid | 7:a1a6bff238c1 | 58 | _y+=1; |
shahidsajid | 7:a1a6bff238c1 | 59 | } |
shahidsajid | 7:a1a6bff238c1 | 60 | else { |
shahidsajid | 7:a1a6bff238c1 | 61 | bowled=1; |
shahidsajid | 7:a1a6bff238c1 | 62 | } |
shahidsajid | 7:a1a6bff238c1 | 63 | } |
shahidsajid | 7:a1a6bff238c1 | 64 | } |
shahidsajid | 7:a1a6bff238c1 | 65 | void Ball::draw(N5110 &lcd) |
shahidsajid | 7:a1a6bff238c1 | 66 | { |
shahidsajid | 7:a1a6bff238c1 | 67 | lcd.drawCircle(_x1,_y1,2,FILL_BLACK); |
shahidsajid | 7:a1a6bff238c1 | 68 | lcd.drawCircle(_x,_y,1,FILL_BLACK); |
shahidsajid | 7:a1a6bff238c1 | 69 | //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
shahidsajid | 7:a1a6bff238c1 | 70 | } |
shahidsajid | 7:a1a6bff238c1 | 71 | void Ball::update_ball(int checkHit, Direction dir){ |
shahidsajid | 7:a1a6bff238c1 | 72 | if (ballHit!=1 && checkHit==1){ |
shahidsajid | 7:a1a6bff238c1 | 73 | ballHit=1; |
shahidsajid | 7:a1a6bff238c1 | 74 | } |
shahidsajid | 7:a1a6bff238c1 | 75 | if(bowled==1 && ballHit==1){ |
shahidsajid | 6:3e50f2cf4366 | 76 | printf("TEST\n"); |
shahidsajid | 6:3e50f2cf4366 | 77 | if (dir==NE){ |
shahidsajid | 6:3e50f2cf4366 | 78 | printf("x=%i y=%i \n",_x,_y); |
shahidsajid | 6:3e50f2cf4366 | 79 | if (!(_y == 2 )){ |
shahidsajid | 6:3e50f2cf4366 | 80 | _y--; |
shahidsajid | 6:3e50f2cf4366 | 81 | } |
shahidsajid | 6:3e50f2cf4366 | 82 | if (_x!=65){ |
shahidsajid | 6:3e50f2cf4366 | 83 | _x++; |
shahidsajid | 6:3e50f2cf4366 | 84 | } |
shahidsajid | 6:3e50f2cf4366 | 85 | printf("HERE"); |
shahidsajid | 6:3e50f2cf4366 | 86 | } |
shahidsajid | 6:3e50f2cf4366 | 87 | |
shahidsajid | 6:3e50f2cf4366 | 88 | } |
shahidsajid | 7:a1a6bff238c1 | 89 | |
shahidsajid | 3:bd3465a70a5a | 90 | } |
shahidsajid | 3:bd3465a70a5a | 91 | |
shahidsajid | 3:bd3465a70a5a | 92 | |
shahidsajid | 3:bd3465a70a5a | 93 | Vector2D Ball::get_velocity() |
shahidsajid | 3:bd3465a70a5a | 94 | { |
shahidsajid | 3:bd3465a70a5a | 95 | Vector2D v = {_velocity.x,_velocity.y}; |
shahidsajid | 3:bd3465a70a5a | 96 | return v; |
shahidsajid | 3:bd3465a70a5a | 97 | } |
shahidsajid | 3:bd3465a70a5a | 98 | |
shahidsajid | 8:7b7e1a5b8200 | 99 | |
shahidsajid | 8:7b7e1a5b8200 | 100 | void Ball::get_direction(Gamepad &pad){ |
shahidsajid | 8:7b7e1a5b8200 | 101 | _d = pad.get_direction(); |
shahidsajid | 3:bd3465a70a5a | 102 | } |
shahidsajid | 3:bd3465a70a5a | 103 | |
shahidsajid | 8:7b7e1a5b8200 | 104 | void Ball::draw_field(N5110 &lcd){ |
shahidsajid | 8:7b7e1a5b8200 | 105 | lcd.drawCircle(field[0].x,field[0].y,2,FILL_BLACK); |
shahidsajid | 8:7b7e1a5b8200 | 106 | lcd.drawCircle(field[1].x,field[1].y,2,FILL_BLACK); |
shahidsajid | 8:7b7e1a5b8200 | 107 | lcd.drawCircle(field[2].x,field[2].y,2,FILL_BLACK); |
shahidsajid | 8:7b7e1a5b8200 | 108 | lcd.drawCircle(field[3].x,field[3].y,2,FILL_BLACK); |
shahidsajid | 3:bd3465a70a5a | 109 | } |
shahidsajid | 3:bd3465a70a5a | 110 | void Ball::set_field(N5110 &lcd){ |
shahidsajid | 4:55a0509c4874 | 111 | lcd.drawLine(40,11,40,14,1); |
shahidsajid | 4:55a0509c4874 | 112 | lcd.drawLine(42,11,42,14,1); |
shahidsajid | 4:55a0509c4874 | 113 | lcd.drawLine(44,11,44,14,1); |
shahidsajid | 4:55a0509c4874 | 114 | lcd.drawLine(40,14,44,14,2); |
shahidsajid | 4:55a0509c4874 | 115 | /* |
shahidsajid | 3:bd3465a70a5a | 116 | lcd.drawCircle(25,2,2,FILL_BLACK); //NE |
shahidsajid | 3:bd3465a70a5a | 117 | lcd.drawCircle(65,2,2,FILL_BLACK); //NW |
shahidsajid | 3:bd3465a70a5a | 118 | lcd.drawCircle(65,40,2,FILL_BLACK); //SE |
shahidsajid | 3:bd3465a70a5a | 119 | lcd.drawCircle(25,40,2,FILL_BLACK); //SW |
shahidsajid | 3:bd3465a70a5a | 120 | lcd.drawCircle(3,20,2,FILL_BLACK); //E |
shahidsajid | 4:55a0509c4874 | 121 | lcd.drawCircle(80,20,2,FILL_BLACK); |
shahidsajid | 4:55a0509c4874 | 122 | */ |
shahidsajid | 4:55a0509c4874 | 123 | int d=0; |
shahidsajid | 8:7b7e1a5b8200 | 124 | int pos=0; |
shahidsajid | 5:915bcac4e9b9 | 125 | srand(time(NULL)); |
shahidsajid | 4:55a0509c4874 | 126 | for(int i=0;i<3;i++){ |
shahidsajid | 5:915bcac4e9b9 | 127 | d = 1+ rand() % 6; |
shahidsajid | 8:7b7e1a5b8200 | 128 | pos=rand()%2; |
shahidsajid | 5:915bcac4e9b9 | 129 | //printf("Direction %i \n",d); |
shahidsajid | 4:55a0509c4874 | 130 | //while (d==N||d==S){ |
shahidsajid | 4:55a0509c4874 | 131 | //get_direction(pad); |
shahidsajid | 4:55a0509c4874 | 132 | //} |
shahidsajid | 8:7b7e1a5b8200 | 133 | /* |
shahidsajid | 8:7b7e1a5b8200 | 134 | if (d==1){ //NW |
shahidsajid | 8:7b7e1a5b8200 | 135 | if (pos==1){ |
shahidsajid | 8:7b7e1a5b8200 | 136 | field[i].x=30; |
shahidsajid | 8:7b7e1a5b8200 | 137 | field[i].y=9; |
shahidsajid | 8:7b7e1a5b8200 | 138 | field[i].position=1; |
shahidsajid | 8:7b7e1a5b8200 | 139 | } |
shahidsajid | 8:7b7e1a5b8200 | 140 | else{ |
shahidsajid | 8:7b7e1a5b8200 | 141 | field[i].x=25; |
shahidsajid | 8:7b7e1a5b8200 | 142 | field[i].y=2; |
shahidsajid | 8:7b7e1a5b8200 | 143 | field[i].position=0; |
shahidsajid | 8:7b7e1a5b8200 | 144 | } |
shahidsajid | 3:bd3465a70a5a | 145 | } |
shahidsajid | 8:7b7e1a5b8200 | 146 | |
shahidsajid | 8:7b7e1a5b8200 | 147 | |
shahidsajid | 8:7b7e1a5b8200 | 148 | if (d==2){ //NE |
shahidsajid | 8:7b7e1a5b8200 | 149 | if (pos==1){ |
shahidsajid | 8:7b7e1a5b8200 | 150 | field[i].x=55; |
shahidsajid | 8:7b7e1a5b8200 | 151 | field[i].y=9; |
shahidsajid | 4:55a0509c4874 | 152 | field[i].position=1; |
shahidsajid | 8:7b7e1a5b8200 | 153 | } |
shahidsajid | 8:7b7e1a5b8200 | 154 | else{ |
shahidsajid | 8:7b7e1a5b8200 | 155 | field[i].x=65; |
shahidsajid | 8:7b7e1a5b8200 | 156 | field[i].y=2; |
shahidsajid | 8:7b7e1a5b8200 | 157 | field[i].position=0; |
shahidsajid | 8:7b7e1a5b8200 | 158 | } |
shahidsajid | 4:55a0509c4874 | 159 | } |
shahidsajid | 8:7b7e1a5b8200 | 160 | |
shahidsajid | 8:7b7e1a5b8200 | 161 | |
shahidsajid | 4:55a0509c4874 | 162 | if (d==3){ //SW |
shahidsajid | 8:7b7e1a5b8200 | 163 | if (pos==1){ |
shahidsajid | 8:7b7e1a5b8200 | 164 | field[i].x=33; |
shahidsajid | 8:7b7e1a5b8200 | 165 | field[i].y=40; |
shahidsajid | 8:7b7e1a5b8200 | 166 | field[i].position=1; |
shahidsajid | 8:7b7e1a5b8200 | 167 | } |
shahidsajid | 8:7b7e1a5b8200 | 168 | |
shahidsajid | 8:7b7e1a5b8200 | 169 | else{ |
shahidsajid | 8:7b7e1a5b8200 | 170 | field[i].x=25; |
shahidsajid | 8:7b7e1a5b8200 | 171 | field[i].y=44; |
shahidsajid | 8:7b7e1a5b8200 | 172 | field[i].position=0; |
shahidsajid | 8:7b7e1a5b8200 | 173 | } |
shahidsajid | 4:55a0509c4874 | 174 | } |
shahidsajid | 8:7b7e1a5b8200 | 175 | |
shahidsajid | 5:915bcac4e9b9 | 176 | if (d==4){ //SE |
shahidsajid | 8:7b7e1a5b8200 | 177 | if (pos==1){ |
shahidsajid | 8:7b7e1a5b8200 | 178 | field[i].x=55; |
shahidsajid | 8:7b7e1a5b8200 | 179 | field[i].y=40; |
shahidsajid | 8:7b7e1a5b8200 | 180 | field[i].position=1; |
shahidsajid | 8:7b7e1a5b8200 | 181 | } |
shahidsajid | 8:7b7e1a5b8200 | 182 | else{ |
shahidsajid | 8:7b7e1a5b8200 | 183 | field[i].x=65; |
shahidsajid | 8:7b7e1a5b8200 | 184 | field[i].y=44; |
shahidsajid | 8:7b7e1a5b8200 | 185 | field[i].position=0; |
shahidsajid | 8:7b7e1a5b8200 | 186 | } |
shahidsajid | 5:915bcac4e9b9 | 187 | } |
shahidsajid | 8:7b7e1a5b8200 | 188 | */ |
shahidsajid | 8:7b7e1a5b8200 | 189 | if (d==5){ //W |
shahidsajid | 8:7b7e1a5b8200 | 190 | if (pos==1){ |
shahidsajid | 8:7b7e1a5b8200 | 191 | field[i].x=3; |
shahidsajid | 8:7b7e1a5b8200 | 192 | field[i].y=25; |
shahidsajid | 8:7b7e1a5b8200 | 193 | field[i].position=1; |
shahidsajid | 8:7b7e1a5b8200 | 194 | } |
shahidsajid | 8:7b7e1a5b8200 | 195 | else{ |
shahidsajid | 8:7b7e1a5b8200 | 196 | field[i].x=25; |
shahidsajid | 8:7b7e1a5b8200 | 197 | field[i].y=25; |
shahidsajid | 8:7b7e1a5b8200 | 198 | field[i].position=0; |
shahidsajid | 8:7b7e1a5b8200 | 199 | } |
shahidsajid | 5:915bcac4e9b9 | 200 | } |
shahidsajid | 8:7b7e1a5b8200 | 201 | /* |
shahidsajid | 8:7b7e1a5b8200 | 202 | if (d==6){ //W |
shahidsajid | 5:915bcac4e9b9 | 203 | field[i].x=80; |
shahidsajid | 5:915bcac4e9b9 | 204 | field[i].y=20; |
shahidsajid | 5:915bcac4e9b9 | 205 | field[i].position=1; |
shahidsajid | 5:915bcac4e9b9 | 206 | } |
shahidsajid | 8:7b7e1a5b8200 | 207 | */ |
shahidsajid | 4:55a0509c4874 | 208 | } |
shahidsajid | 4:55a0509c4874 | 209 | |
shahidsajid | 4:55a0509c4874 | 210 | } |