Beginning of the moveBall function
The moveBall function begins in Listing 6.
Listing 6. Beginning of the moveBall function.
void moveBall(){ //Save current location of ball. tempX = x; tempY = y; //The code in the switch statement is identical to the // code in the earlier version of the program. switch(dir){ case 0: //Direction is northwest. if((x <= radius) || (y <= radius)){ //Ball has collided with either the left wall or // the top wall. //Get a new direction. Note that if the new // direction is the same as the old one, control // will come back to here to get still another // direction the next time the function is called. dir = rand() % 4; }else{ //No collision, set new location for the ball --x; --y; }//end else break; case 1: //Direction is southwest. if(((x <= radius) || (y >= (height - radius)))){ //get a new direction dir = rand() % 4; }else{ //set new location for the ball --x; ++y; }//end else break; case 2: //Direction is northeast. if(((x >= (width - radius)) || (y <= radius))){ //get a new direction dir = rand() % 4; }else{ //set new location for the ball ++x; --y; }//end else break; case 3: //Direction is southeast if((((x >= (width - radius)) || (y >= (height - radius))))){ //get a new direction dir = rand() % 4; }else{ //set new location for the ball ++x; ++y; }//end else }//end switch |