Example

Car

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 9-9: An array of Car objects

Car[] cars = new Car[100]; // An array of 100 Car objects!

void setup() {
  size(200,200);
  smooth();
  for (int i = 0; i < cars.length; i ++ ) { // Initialize each Car using a for loop.
    cars[i] = new Car(color(i*2),0,i*2,i/20.0); 
  }
}

void draw() {
  background(255);
  for (int i = 0; i < cars.length; i ++ ) { // Run each Car using a for loop.
    cars[i].move();
    cars[i].display();
  }
}
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 9-9: An array of Car objects


// The Car class does not change whether we are making one car, 100 cars or 1,000 cars!
class Car { 
  color c;
  float xpos;
  float ypos;
  float xspeed;

  Car(color c_, float xpos_, float ypos_, float xspeed_) {
    c = c_;
    xpos = xpos_;
    ypos = ypos_;
    xspeed = xspeed_;
  }

  void display() {
    rectMode(CENTER);
    stroke(0);
    fill(c);
    rect(xpos,ypos,20,10);
  }

  void move() {
    xpos = xpos + xspeed;
    if (xpos > width) {
      xpos = 0;
    }
  }
}
  • Jwhatley

    What does “_” mean with regards to the code?

  • http://www.learningprocessing.com Daniel Shiffman

    It doesn’t mean anything. It’s just a programmer’s convention to use an underscore when a variable coming in as an argument is assigned to a variable within the class. You could just as easily write:

     Car(color tempC) {
        c = tempC;
      }
    

    or

     Car(color asdfasdfwe) {
        c = asdfasdfwe;
      }
    
  • http://www.facebook.com/piero.barbagelata Piero Barbagelata

    Why did you put the for loop in the setup method?

  • Anonymous

    That for loop is used to initialize all the car objects.  

  • Basheersubei

    Thank you for the wonderful example!

  • Maryam

    When I try and load this code, I get an error “The Constructor classEg.Car(int,int,int,float) is undefined”. in relation to this line of code–>
    for (int i = 0; i < cars.length; i ++ ) { // Initialize each Car using a for loop.
    cars[i] = new Car(color(i*2),0,i*2,i/20.0);
    … how come it won't work in my processing?

  • shiffman

    Did you retype it? Perhaps you missed something. I am not getting this error.

    http://www.learningprocessing.com/example-files/chapter09/zip/example_9_9.zip

  • marmota peluda

    señor porque?

  • Rohail Agneshius

    could you help me to find the codes for this example
    The car Race game will be very simple and must follow the following specifications:
    1. Every car race game is played with 5 cars (use of array of objects here)
    2. You will need to change the constructor of the above class in a way that every car gets a unique position on the screen (as currently the car is being displayed in the middle of the screen, if we use this code then all Five cars will be displayed one on the top of each other and then we will not be able to see all the cars). Also, they are initialized with a random speed between 50 kmph to 150 kmph. The cars will continue to move in the same speed with what they are initialized. (Do not worry of changing their speeds during the course of the Race).
    3. The cars are numbered from Car Number:1 to Car Number:5.
    4. Choose your own way to decide for the winning car. When you end the program a message should be displayed as “Car wins the Race!”. (Hint: Here you will need to decide on the finishing line and may be check which car’s xpos or ypos – depending on whether you are playing the race horizontally or vertically crosses the finishing line first.)

  • tony

    could you please help me to find the codes for this
    The car Race game will be very simple and must follow the following specifications:

    1. Every car race game is played with 5 cars (use of array of objects here)

    2. You will need to change the constructor of the above class in a way that every car gets a unique position on the screen (as currently the car is being displayed in the middle of the screen, if we use this code then all Five cars will be displayed one on the top of each other and then we will not be able to see all the cars). Also, they are initialized with a random speed between 50 kmph to 150 kmph. The cars will continue to move in the same speed with what they are initialized. (Do not worry of changing their speeds during the course of the Race).

    3. The cars are numbered from Car Number:1 to Car Number:5.

    4. Choose your own way to decide for the winning car. When you end the program a message should be displayed as “Car wins the Race!”. (Hint: Here you will need to decide on the finishing line and may be check which car’s xpos or ypos – depending on whether you are playing the race horizontally or vertically crosses the finishing line first.)