A Race with Arrays


1. Write a function to draw a car, with the following input parameters:

  • x for horizontal position 
  • y for vertical position 
  • c for the car’s primary color 

2. Create a variable to store the number of cars to draw. This will be your array size. Initialize the variable, so that you’ll create at least 10 cars;

3. Since you will need multiple cars, create 3 arrays to store information about each car. 
     example: var xCars = [ ] ;   //declare an array to store x-Position of each car

  • xCars for the horizontal position of the cars 
  • yCars for the horizontal position of the cars 
  • cCars for the color of the cars 

4. Initialize each element (use a loop for each array):

  • xCars[ i ] each should initialized to 0 
  • yCars[ i ] each should be initialized to a random number between 0 and 399 
  • cCars[ i ] each should be a random color

5. Write code in the processing draw function with the following logic:

Use one for-loop to enclose logic that is repeated for each of the cars 

  • draw each car
  • Increment the car’s x position with a random number between 1 and 5 
  • Check to see if any car has passed the “finish line” (i.e. x=399) 
    if so, write the name of the winner (i.e. car 1, car 2, etc) and stop draw the draw loop from executing:   use noLoop( ); 

6. As always, save your code and submit on eLearning following the submission template and instructions.

If you have time:

  •  Add numbers to the side of the cars (will require adding a parameter) 
  •  Add a base “speed” for each car, that gets added to the x position before the random part does 
  •  Change the position of the “finish line” 
  •  Add background graphics to make it look like a racetrack and make the y position of the cars such that the cars line up on the track 

Do you need urgent help with this or a similar assignment? We got you. Simply place your order and leave the rest to our experts.

Quality Guaranteed!

Written From Scratch.

We Keep Time!

Scroll to Top