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
Diff: Ball/Ball.cpp
- Revision:
- 31:eefa1d23a843
- Parent:
- 30:43aace0fdbdf
- Child:
- 33:9d34ef219fff
--- a/Ball/Ball.cpp Wed May 08 01:20:06 2019 +0000 +++ b/Ball/Ball.cpp Wed May 08 12:13:28 2019 +0000 @@ -1,106 +1,169 @@ #include "Ball.h" + +#define ORIGIN_BALL_X 42 +#define ORIGIN_BALL_Y 15 + +#define ORIGIN_BOWLER_X 42 +#define ORIGIN_BOWLER_Y 3 + +#define TARGET_BOWLER_Y 15 +#define TARGET_BALL_Y 36 + + + +//Constructor for the Ball Object Ball::Ball() { } +//Deconstructor for the Ball Object Ball::~Ball() { } - -void Ball::init(int size,int speed) +//Initialises the Ball Object +void Ball::init(int size) { + /*Sets the ball size and ball's starting x & y co-ordinate*/ _size = size; - _x1=42; - _y1=3; - _x = 42; - _y = 15; + _ball_x = ORIGIN_BALL_X; + _ball_y = ORIGIN_BALL_Y; + + /*_bowler_ball_x and _bowler_ball_y are the bowler's starting co-ordinate*/ + _bowler_x=ORIGIN_BOWLER_X; + _bowler_y=ORIGIN_BOWLER_Y; + + /*Initially sets the Ball class variables to 0 */ _ball_count=0; _set_tone=0; - _bowled=0; _ball_count=0; } + +/*Increments the ball count for each round*/ void Ball::increment_ball_count(){ _ball_count++; } + +/*getter method: Returns the integer ball Coumt*/ int Ball::get_ball_count(){ return _ball_count; } + +/* The bowler is marked with a white circle on top of the pitch and runs in + to bowl at the start of every round, the y cordinate is incremented for every call + to the function till the target co-ordinate of 15 is reached, when the target + co-ordinate is reached, the method returns 1 + + The method is called in ball_start() +*/ int Ball::bowler_start(Gamepad &pad){ - if(_y1!=15){ - _y1+=1; + // wait function implemented to provide time in between each round + if (_bowler_y==3){ //the starting co-ordinate for the bowler is 3 + wait(0.5); + } + //y-ordinate is incremented if the target co-ordinate is no reached + if(_bowler_y!=TARGET_BOWLER_Y){ + _bowler_y+=1; return 0; } else { + /* + set_tone used as a flag to indicate whether to play tone or not + The tone of frequency 750Hz every time the bowler reaches the target co-ordinate to + inform the player that the ball will now be delivered */ if (_set_tone==0){ - //pad.tone(750.0,0.3); + pad.tone(750.0,0.3); _set_tone=1; - } + } return 1; } } -int Ball::ball_start(Gamepad &pad){ - +/* ball_start follows a similar logic to bowler_start wherein there is a target y co-ordinate for the ball + the y co-ordinate is incremented untill the target co-ordinate is reached returning 1 */ +int Ball::ball_start(Gamepad &pad){ + //Method only starts running after bowler_start() returns 1 int bowler_start_check=bowler_start(pad); + if (bowler_start_check==1){ - if (_y!=36){ - _y+=1; + if (_ball_y!=TARGET_BALL_Y){ //target_cordinate is 36 + _ball_y+=1; return 0; } else{ - _bowled=1; + //sets _bowled flag to 1 return 1; } - } + } + //returns 0 if the bowler_start hasn't returned 1 + return 0; } - +// Method to draw the ball and ball count on to the LCD void Ball::draw(N5110 &lcd) { + //draws the ball cont to the top left of the LCD char buffer[5]; int c=get_ball_count(); int length=sprintf(buffer,"B%i",c); - lcd.printString(buffer,0,0); - lcd.drawCircle(_x1,_y1,2,FILL_TRANSPARENT); - lcd.drawCircle(_x,_y,2,FILL_BLACK); - //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); + lcd.printString(buffer,70,0); + //draws the ball and the bowler to the LCD + lcd.drawCircle(_bowler_x,_bowler_y,2,FILL_TRANSPARENT); + lcd.drawCircle(_bowler_x,_bowler_y,1,FILL_BLACK); + lcd.drawCircle(_ball_x,_ball_y,2,FILL_BLACK); + //lcd.drawRect(_ball_x,_ball_y,_size,_size,FILL_BLACK); } - -int Ball::update_ball(int expected_x,int expected_y){ - if (_y!=expected_y){ - if (_y>=expected_y){ - _y--; +/*The method takes as paramets the expected x and y co-ordinates for the ball to + travel towards. Accorodingly the x & y co-orinate are incremente untill the exepcted + co-ordinates are reached and then returns 1 +*/ +int Ball::update_ball(int expected_ball_x,int expected_ball_y){ + /*Checking if the y co-ordinate of the ball is equal to the expected y_co-ordinate + if true, a simple if condition is conduced to check if the expected cordinate + is greater than or less than the current ball co-ridnate the current y_ordinate of + the ball is incremented decremented accordingly. + */ + if (_ball_y!=expected_ball_y){ + if (_ball_y>=expected_ball_y){ + _ball_y--; } else{ - _y++; + _ball_y++; } + /*Checking if the x co-ordinate of the ball is equal to the expected x_co-ordinate + if true, a simple if condition is conduced to check if the expected cordinate + is greater than or less than the current ball co-ridnate the current x_ordinate of + the ball is incremented or decremented accordingly + */ } - if (_x!=expected_x){ - if (_x>expected_x) - _x--; + if (_ball_x!=expected_ball_x){ + if (_ball_x>expected_ball_x) + _ball_x--; else{ - _x++; + _ball_x++; } } -printf(" %i %i %i %i \n",_x,expected_x,_y,expected_y); -if (_x==expected_x && _y==expected_y){ +printf(" %i %i %i %i \n",_ball_x,expected_ball_x,_ball_y,expected_ball_y);//used for debugging + +// if x& y cordinates are equal to the target co-ordinate, method returns 1 +if (_ball_x==expected_ball_x && _ball_y==expected_ball_y){ return 1; } else{ return 0; } } - +//resets the ball count void Ball::reset_ball_count(){ _ball_count=0; } +//resets the ball variables and the ball and bolwer cordinates to the origin void Ball::reset(){ - _bowled=0; - _x1=42; - _y1=3; - _x = 42; - _y = 15; + _ball_x = ORIGIN_BALL_X; + _ball_y = ORIGIN_BALL_Y; + + + _bowler_x=ORIGIN_BOWLER_X; + _bowler_y=ORIGIN_BOWLER_Y; _set_tone=0; - //_ball_count=0; } \ No newline at end of file