ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Committer:
shahidsajid
Date:
Wed May 08 13:30:12 2019 +0000
Revision:
33:9d34ef219fff
Parent:
31:eefa1d23a843
Parent:
8:7b7e1a5b8200
Child:
34:d9099874bbc3
Fixed Documentation Errors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shahidsajid 3:bd3465a70a5a 1 #include "Ball.h"
shahidsajid 3:bd3465a70a5a 2
shahidsajid 31:eefa1d23a843 3 #define ORIGIN_BALL_X 42
shahidsajid 31:eefa1d23a843 4 #define ORIGIN_BALL_Y 15
shahidsajid 4:55a0509c4874 5
shahidsajid 31:eefa1d23a843 6 #define ORIGIN_BOWLER_X 42
shahidsajid 31:eefa1d23a843 7 #define ORIGIN_BOWLER_Y 3
shahidsajid 31:eefa1d23a843 8
shahidsajid 31:eefa1d23a843 9 #define TARGET_BOWLER_Y 15
shahidsajid 31:eefa1d23a843 10 #define TARGET_BALL_Y 36
shahidsajid 31:eefa1d23a843 11
shahidsajid 31:eefa1d23a843 12
shahidsajid 31:eefa1d23a843 13
shahidsajid 31:eefa1d23a843 14 //Constructor for the Ball Object
shahidsajid 3:bd3465a70a5a 15 Ball::Ball()
shahidsajid 3:bd3465a70a5a 16 {
shahidsajid 3:bd3465a70a5a 17
shahidsajid 3:bd3465a70a5a 18 }
shahidsajid 3:bd3465a70a5a 19
shahidsajid 31:eefa1d23a843 20 //Deconstructor for the Ball Object
shahidsajid 3:bd3465a70a5a 21 Ball::~Ball()
shahidsajid 3:bd3465a70a5a 22 {
shahidsajid 3:bd3465a70a5a 23
shahidsajid 3:bd3465a70a5a 24 }
shahidsajid 31:eefa1d23a843 25 //Initialises the Ball Object
shahidsajid 31:eefa1d23a843 26 void Ball::init(int size)
shahidsajid 3:bd3465a70a5a 27 {
shahidsajid 31:eefa1d23a843 28 /*Sets the ball size and ball's starting x & y co-ordinate*/
shahidsajid 3:bd3465a70a5a 29 _size = size;
shahidsajid 31:eefa1d23a843 30 _ball_x = ORIGIN_BALL_X;
shahidsajid 31:eefa1d23a843 31 _ball_y = ORIGIN_BALL_Y;
shahidsajid 31:eefa1d23a843 32
shahidsajid 31:eefa1d23a843 33 /*_bowler_ball_x and _bowler_ball_y are the bowler's starting co-ordinate*/
shahidsajid 31:eefa1d23a843 34 _bowler_x=ORIGIN_BOWLER_X;
shahidsajid 31:eefa1d23a843 35 _bowler_y=ORIGIN_BOWLER_Y;
shahidsajid 31:eefa1d23a843 36
shahidsajid 31:eefa1d23a843 37 /*Initially sets the Ball class variables to 0 */
shahidsajid 18:a260ce8db9e7 38 _ball_count=0;
shahidsajid 26:6427f09cf8d3 39 _set_tone=0;
shahidsajid 18:a260ce8db9e7 40 _ball_count=0;
shahidsajid 3:bd3465a70a5a 41
shahidsajid 3:bd3465a70a5a 42 }
shahidsajid 3:bd3465a70a5a 43
shahidsajid 31:eefa1d23a843 44 /*Increments the ball count for each round*/
shahidsajid 18:a260ce8db9e7 45 void Ball::increment_ball_count(){
shahidsajid 18:a260ce8db9e7 46 _ball_count++;
shahidsajid 18:a260ce8db9e7 47 }
shahidsajid 31:eefa1d23a843 48
shahidsajid 31:eefa1d23a843 49 /*getter method: Returns the integer ball Coumt*/
shahidsajid 18:a260ce8db9e7 50 int Ball::get_ball_count(){
shahidsajid 18:a260ce8db9e7 51 return _ball_count;
shahidsajid 3:bd3465a70a5a 52 }
shahidsajid 31:eefa1d23a843 53
shahidsajid 31:eefa1d23a843 54 /* The bowler is marked with a white circle on top of the pitch and runs in
shahidsajid 31:eefa1d23a843 55 to bowl at the start of every round, the y cordinate is incremented for every call
shahidsajid 31:eefa1d23a843 56 to the function till the target co-ordinate of 15 is reached, when the target
shahidsajid 31:eefa1d23a843 57 co-ordinate is reached, the method returns 1
shahidsajid 31:eefa1d23a843 58
shahidsajid 31:eefa1d23a843 59 The method is called in ball_start()
shahidsajid 31:eefa1d23a843 60 */
shahidsajid 15:81a3aaf52647 61 int Ball::bowler_start(Gamepad &pad){
shahidsajid 31:eefa1d23a843 62 // wait function implemented to provide time in between each round
shahidsajid 31:eefa1d23a843 63 if (_bowler_y==3){ //the starting co-ordinate for the bowler is 3
shahidsajid 31:eefa1d23a843 64 wait(0.5);
shahidsajid 31:eefa1d23a843 65 }
shahidsajid 31:eefa1d23a843 66 //y-ordinate is incremented if the target co-ordinate is no reached
shahidsajid 31:eefa1d23a843 67 if(_bowler_y!=TARGET_BOWLER_Y){
shahidsajid 31:eefa1d23a843 68 _bowler_y+=1;
shahidsajid 7:a1a6bff238c1 69 return 0;
shahidsajid 7:a1a6bff238c1 70 }
shahidsajid 7:a1a6bff238c1 71 else {
shahidsajid 31:eefa1d23a843 72 /*
shahidsajid 31:eefa1d23a843 73 set_tone used as a flag to indicate whether to play tone or not
shahidsajid 31:eefa1d23a843 74 The tone of frequency 750Hz every time the bowler reaches the target co-ordinate to
shahidsajid 31:eefa1d23a843 75 inform the player that the ball will now be delivered */
shahidsajid 26:6427f09cf8d3 76 if (_set_tone==0){
shahidsajid 31:eefa1d23a843 77 pad.tone(750.0,0.3);
shahidsajid 26:6427f09cf8d3 78 _set_tone=1;
shahidsajid 31:eefa1d23a843 79 }
shahidsajid 7:a1a6bff238c1 80 return 1;
shahidsajid 7:a1a6bff238c1 81 }
shahidsajid 7:a1a6bff238c1 82 }
shahidsajid 31:eefa1d23a843 83 /* ball_start follows a similar logic to bowler_start wherein there is a target y co-ordinate for the ball
shahidsajid 31:eefa1d23a843 84 the y co-ordinate is incremented untill the target co-ordinate is reached returning 1 */
shahidsajid 31:eefa1d23a843 85 int Ball::ball_start(Gamepad &pad){
shahidsajid 31:eefa1d23a843 86 //Method only starts running after bowler_start() returns 1
shahidsajid 15:81a3aaf52647 87 int bowler_start_check=bowler_start(pad);
shahidsajid 31:eefa1d23a843 88
shahidsajid 10:6c6e09023942 89 if (bowler_start_check==1){
shahidsajid 31:eefa1d23a843 90 if (_ball_y!=TARGET_BALL_Y){ //target_cordinate is 36
shahidsajid 31:eefa1d23a843 91 _ball_y+=1;
shahidsajid 10:6c6e09023942 92 return 0;
shahidsajid 7:a1a6bff238c1 93 }
shahidsajid 10:6c6e09023942 94 else{
shahidsajid 31:eefa1d23a843 95 //sets _bowled flag to 1
shahidsajid 10:6c6e09023942 96 return 1;
shahidsajid 7:a1a6bff238c1 97 }
shahidsajid 31:eefa1d23a843 98 }
shahidsajid 31:eefa1d23a843 99 //returns 0 if the bowler_start hasn't returned 1
shahidsajid 31:eefa1d23a843 100 return 0;
shahidsajid 7:a1a6bff238c1 101 }
shahidsajid 31:eefa1d23a843 102 // Method to draw the ball and ball count on to the LCD
shahidsajid 7:a1a6bff238c1 103 void Ball::draw(N5110 &lcd)
shahidsajid 7:a1a6bff238c1 104 {
shahidsajid 31:eefa1d23a843 105 //draws the ball cont to the top left of the LCD
shahidsajid 18:a260ce8db9e7 106 char buffer[5];
shahidsajid 18:a260ce8db9e7 107 int c=get_ball_count();
shahidsajid 18:a260ce8db9e7 108 int length=sprintf(buffer,"B%i",c);
shahidsajid 31:eefa1d23a843 109 lcd.printString(buffer,70,0);
shahidsajid 31:eefa1d23a843 110 //draws the ball and the bowler to the LCD
shahidsajid 31:eefa1d23a843 111 lcd.drawCircle(_bowler_x,_bowler_y,2,FILL_TRANSPARENT);
shahidsajid 31:eefa1d23a843 112 lcd.drawCircle(_bowler_x,_bowler_y,1,FILL_BLACK);
shahidsajid 31:eefa1d23a843 113 lcd.drawCircle(_ball_x,_ball_y,2,FILL_BLACK);
shahidsajid 31:eefa1d23a843 114 //lcd.drawRect(_ball_x,_ball_y,_size,_size,FILL_BLACK);
shahidsajid 3:bd3465a70a5a 115 }
shahidsajid 31:eefa1d23a843 116 /*The method takes as paramets the expected x and y co-ordinates for the ball to
shahidsajid 31:eefa1d23a843 117 travel towards. Accorodingly the x & y co-orinate are incremente untill the exepcted
shahidsajid 31:eefa1d23a843 118 co-ordinates are reached and then returns 1
shahidsajid 31:eefa1d23a843 119 */
shahidsajid 31:eefa1d23a843 120 int Ball::update_ball(int expected_ball_x,int expected_ball_y){
shahidsajid 31:eefa1d23a843 121 /*Checking if the y co-ordinate of the ball is equal to the expected y_co-ordinate
shahidsajid 31:eefa1d23a843 122 if true, a simple if condition is conduced to check if the expected cordinate
shahidsajid 31:eefa1d23a843 123 is greater than or less than the current ball co-ridnate the current y_ordinate of
shahidsajid 31:eefa1d23a843 124 the ball is incremented decremented accordingly.
shahidsajid 31:eefa1d23a843 125 */
shahidsajid 31:eefa1d23a843 126 if (_ball_y!=expected_ball_y){
shahidsajid 31:eefa1d23a843 127 if (_ball_y>=expected_ball_y){
shahidsajid 31:eefa1d23a843 128 _ball_y--;
shahidsajid 9:a81db6a703b7 129 }
shahidsajid 12:954da4f4e565 130 else{
shahidsajid 31:eefa1d23a843 131 _ball_y++;
shahidsajid 14:122eaa3b7a50 132 }
shahidsajid 31:eefa1d23a843 133 /*Checking if the x co-ordinate of the ball is equal to the expected x_co-ordinate
shahidsajid 31:eefa1d23a843 134 if true, a simple if condition is conduced to check if the expected cordinate
shahidsajid 31:eefa1d23a843 135 is greater than or less than the current ball co-ridnate the current x_ordinate of
shahidsajid 31:eefa1d23a843 136 the ball is incremented or decremented accordingly
shahidsajid 31:eefa1d23a843 137 */
shahidsajid 6:3e50f2cf4366 138 }
shahidsajid 31:eefa1d23a843 139 if (_ball_x!=expected_ball_x){
shahidsajid 31:eefa1d23a843 140 if (_ball_x>expected_ball_x)
shahidsajid 31:eefa1d23a843 141 _ball_x--;
shahidsajid 12:954da4f4e565 142 else{
shahidsajid 31:eefa1d23a843 143 _ball_x++;
shahidsajid 12:954da4f4e565 144 }
shahidsajid 15:81a3aaf52647 145 }
shahidsajid 31:eefa1d23a843 146 printf(" %i %i %i %i \n",_ball_x,expected_ball_x,_ball_y,expected_ball_y);//used for debugging
shahidsajid 31:eefa1d23a843 147
shahidsajid 31:eefa1d23a843 148 // if x& y cordinates are equal to the target co-ordinate, method returns 1
shahidsajid 31:eefa1d23a843 149 if (_ball_x==expected_ball_x && _ball_y==expected_ball_y){
shahidsajid 14:122eaa3b7a50 150 return 1;
shahidsajid 12:954da4f4e565 151 }
shahidsajid 14:122eaa3b7a50 152 else{
shahidsajid 14:122eaa3b7a50 153 return 0;
shahidsajid 14:122eaa3b7a50 154 }
shahidsajid 14:122eaa3b7a50 155 }
shahidsajid 31:eefa1d23a843 156 //resets the ball count
shahidsajid 18:a260ce8db9e7 157 void Ball::reset_ball_count(){
shahidsajid 18:a260ce8db9e7 158 _ball_count=0;
shahidsajid 18:a260ce8db9e7 159 }
shahidsajid 31:eefa1d23a843 160 //resets the ball variables and the ball and bolwer cordinates to the origin
shahidsajid 9:a81db6a703b7 161 void Ball::reset(){
shahidsajid 31:eefa1d23a843 162 _ball_x = ORIGIN_BALL_X;
shahidsajid 31:eefa1d23a843 163 _ball_y = ORIGIN_BALL_Y;
shahidsajid 31:eefa1d23a843 164
shahidsajid 31:eefa1d23a843 165
shahidsajid 31:eefa1d23a843 166 _bowler_x=ORIGIN_BOWLER_X;
shahidsajid 31:eefa1d23a843 167 _bowler_y=ORIGIN_BOWLER_Y;
shahidsajid 26:6427f09cf8d3 168 _set_tone=0;
shahidsajid 30:43aace0fdbdf 169 }