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
- Committer:
- shahidsajid
- Date:
- 2019-05-08
- Revision:
- 31:eefa1d23a843
- Parent:
- 30:43aace0fdbdf
- Child:
- 33:9d34ef219fff
File content as of revision 31:eefa1d23a843:
#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() { } //Initialises the Ball Object void Ball::init(int size) { /*Sets the ball size and ball's starting x & y co-ordinate*/ _size = size; _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; _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){ // 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); _set_tone=1; } return 1; } } /* 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 (_ball_y!=TARGET_BALL_Y){ //target_cordinate is 36 _ball_y+=1; return 0; } else{ //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,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); } /*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{ _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 (_ball_x!=expected_ball_x){ if (_ball_x>expected_ball_x) _ball_x--; else{ _ball_x++; } } 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(){ _ball_x = ORIGIN_BALL_X; _ball_y = ORIGIN_BALL_Y; _bowler_x=ORIGIN_BOWLER_X; _bowler_y=ORIGIN_BOWLER_Y; _set_tone=0; }