I have made an object move across the screen from right to left in a straight line using a for loop. However now I need to use the random function to make it appear to move up and down randomly as it moves to the right of the screen, and where in the loop do I do this? any help starting this, thanks
You could generate a random number and then decide from this number whether you go up,down,left or right. Like
-
switch( rand() % 4 ) {
-
-
case 0:
-
// go up
-
cout << "up" << endl;
-
break;
-
case 1:
-
// go down
-
cout << "down" << endl;
-
break;
-
case 2:
-
// go left
-
cout << "left" << endl;
-
break;
-
default:
-
// go right
-
cout << "right" << endl;
-
break;
-
}
-
If you only need to decide about two directions, calculate rand()%2 and decide from that.
One loop iteration is one step of your object, so the above switch statement should be inside the loop.