Write a program that creates several racing cars and drives them across the screen. Each car should be the same size, have the same max speed, and start at the same x coordinate. It might be useful to create constants for these values. Create an array of GRects that you will use as the bodies of the cars. Because you're going to create a lot of cars, it's a good idea to do all of this in a loop. As you go through this loop, calculate the starting y coordinate for each car to make sure that we can see them.

Now you need to animate the cars! Create a loop that moves each car across the screen. As cars go along they have to speed up and slow down according to the track. To mimic this, generate a random integer less than the max speed and move the car only in the x direction. When the winning car has crossed the finish line (reached the edge of the screen), leave the loop.

Remember to use constants that you define up top rather than manually writing ("hard coding") the number every time. That way, if you want to make more cars, all you have to do is change one number!

/*The width of a car:*/
private final int CAR_WIDTH=75;

/*The top speed of a car:*/
private final int MAX_VELOCITY=5;

/*The amount of time the program should pause on each loop:*/
private final int PAUSE_TIME=20;

/*Where the cars start driving from:*/
private final int X_OFFSET=50;

Solution

/**
 * Class: RacingCars
 * -----------------
 * Move cars across the screen
 */

public class RacingCars extends GraphicsProgram {
	private final int CAR_WIDTH = 75;
	private final int MAX_VELOCITY = 5;
	private final int PAUSE_TIME = 30;
	private final int X_OFFSET = 50;
	private RandomGenerator rgen = RandomGenerator.getInstance();

	public void run(){
		/* Creation and placement of the cars */
		int numCars = 10;
		double carHeight = getHeight()/(2*numCars);
		double yOffset = carHeight/2.0;
		GRect[] cars = new GRect[numCars];
		for(int i = 0; i < numCars; i++){
			cars[i] = new GRect(CAR_WIDTH, carHeight);
			cars[i].setColor(rgen.nextColor());
			cars[i].setFilled(true);
			double carYOffset = yOffset + 2*i*carHeight;
			add(cars[i], X_OFFSET, carYOffset);
		}
		pause(1000);
		
		/* Animation */
		boolean finished = false;
		while(!finished) {
			// move each car one by one
			for(int i = 0; i < cars.length; i++) {
				cars[i].move(rgen.nextInt(MAX_VELOCITY), 0);
				if(cars[i].getX() + CAR_WIDTH >= getWidth()) {
					// let everyone move to the next animation frame.
					finished = true;
				}
			}
			pause(PAUSE_TIME);
		}
	}
}