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

// Example 8-2: Two Car objects

Car myCar1;
Car myCar2; // Two objects!

void setup() {
  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() {

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() {

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

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


  • 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:


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


  • Leximark09

    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.


  • Spacelab


  • Acer


  • 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 :/