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

// Example 8-2: Two Car objects

Car myCar1;
Car myCar2; // Two objects!

void setup() {
  size(200,200);
  myCar1 = new Car(color(255,0,0),0,100,2); // Parameters go inside the parentheses when the object is constructed.
  myCar2 = new Car(color(0,0,255),0,10,1);
}

void draw() {
  background(255);
  myCar1.move();
  myCar1.display();
  myCar2.move();
  myCar2.display();
}

class Car { // Even though there are multiple objects, we still only need one class. No matter how many cookies we make, only one cookie cutter is needed.Isn’t object-oriented programming swell?
  color c;
  float xpos;
  float ypos;
  float xspeed;

  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { // The Constructor is defined with arguments.
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }

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

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

    how can i create multiple objects with a for loop? multiple cars…

    thanks

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

    How can I create lines between the cars (if I create 3 cars)? (line between car 1 and car2, line between car 2 and car 3, line between car 1 and car 3)??
    thank you

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

    You can draw a line like so:

    line(myCar1.x,myCar1.y,myCar2.x,myCar2.y)

    And it might be worth building this functionality into the car class, i.e.

    myCar1.drawConnection(myCar2);

  • Leximark09

    Hey!
    I am trying to get these two to drive in a sort of circle around an oval. My window is set up so its like a race track, and there are five cars which are supposed to revolve around the track, (it is alright if they take sharp turns in a square). Any tips?

  • Anonymous

    This example by definition can only have the cars move horizontally because only the x value is adjusted. You’ll need to have both an xspeed and a yspeed. You could take a look at polar to cartesian coordinates (which is a nice way of moving objects in a circle) or vector-based motion.

    http://processing.org/learning/basics/polartocartesian.html
    http://processing.org/learning/pvector/

  • Spacelab

    like

  • Acer

    likeeed

  • Fotografie

    likeeed very much :)

  • Email

    sehr spannend..!

  • http://www.facebook.com/profile.php?id=100000562694080 Misaki-chan Hikaru Ayuzawa

    how about.. 2 roads and then opposite direction and different speed , random colors, and    
    many cars on it??

  • Brendan Murphy

     Why is it when I copy and paste the code I get an error “The constructor Car(int, int, int, int) is undefined”

  • AmirNadi

    don’t copy and paste :p Understand it

  • Brendan Murphy

    Ummm its an example so I can copy and paste then take things in and out to see what they are and how the function thereby understanding it.

    So why do I get the error

  • shiffman

    I don’t get this error unfortunately. Perhaps you missed copying part of the class with the constructor?

  • maikerling

    can someone explain me how to do several cars that drive in one line? i’m trying to create 5 cars in one row, that have the same distance between each other and go at the same speed. i know that an array would be useful but i don’t seem to use it correctly because i only get 1 car :/