Here is the bouncing ball from Example 5-6 combined with the drawCar() function. Fill in the blanks so that you now have a bouncing car with parameter passing! (Note that the global variables are now named globalX and globalY to avoid confusion with the local variables x and y in drawCar()).
int globalX = 0;
int globalY = 100;
int speed = 1;

void setup() {
  size(200,200);
  smooth();
}

void draw() {
  background(0);
  ______________________________________________________________
  ______________________________________________________________
  ______________________________________________________________
}

void move() {
  // Change the x location by speed
  globalX = globalX + speed;
}

void bounce() {
  if ((globalX > width) || (globalX < 0)) {
    speed = speed * –1;
  }
}

void drawCar(int x, int y, int thesize, color c) {
  int offset = thesize / 4;
  rectMode(CENTER);
  stroke(200);
  fill(c);
  rect(x,y,thesize,thesize/2);
  fill(200);
  rect(x - offset,y - offset,offset,offset/2);
  rect(x + offset,y - offset,offset,offset/2);
  rect(x - offset,y + offset,offset,offset/2);
  rect(x + offset,y + offset,offset,offset/2);
}
Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 7-6: Here is the bouncing ball from Example 5-6 combined with 
// the drawCar() function. Fill in the blanks so that you now have a bouncing
// car with parameter passing! (Note that the global variables are now named 
// globalX and globalY to avoid confusion with the local variables x and y in 
// drawCar()).


int globalX = 0;
int globalY = 100;
int speed = 1;

void setup() {
  size(200,200);
  smooth();
}

void draw() {
  background(255);
  move();
  bounce();
  drawCar(globalX,globalY,24,color(100));
}

void move() {
  // Change the x location by speed
  globalX = globalX + speed;
}

void bounce() {
  if ((globalX > width) || (globalX < 0)) {
    speed = speed * -1;
  }
}

void drawCar(int x, int y, int thesize, color c) {
  int offset = thesize / 4;
  rectMode(CENTER);
  stroke(0);
  fill(c);
  rect(x,y,thesize,thesize/2);
  fill(200);
  rect(x - offset,y - offset,offset,offset/2);
  rect(x + offset,y - offset,offset,offset/2);
  rect(x - offset,y + offset,offset,offset/2);
  rect(x + offset,y + offset,offset,offset/2);
}

6 Comments

»

  1. Hi,

    I wrote the following code as an exercise, and 2 things seem weird to me:
    - how come all the instances of the ball are not aligned vertically?
    - why do the balls move slower if I remove some of them?

    Thanks in advance for your help ;)

    void setup() {
      size(200,200);
      background(0);
      noStroke();
      fill(255);
      smooth();
      frameRate(30);
    }
    
    float x = 10;
    float speed = 1;
    float diam = 10;
    
    void draw() {
      background(0);
      moveBall(30);
      moveBall(50);
      moveBall(70);
      moveBall(90);
      moveBall(110); //these balls are not aligned vertically.
      moveBall(130); //the more I add, the faster they move!
    }
    
    void moveBall(int yPos) {
      if ((x > width-(diam/2))) {
        speed = speed*(-1);
      }
      x = x+speed;
      ellipse(x,yPos,diam,diam);
    }
    

    Comment by Alex — March 10, 2010 @ 3:21 pm

  2. This is because there is only one variable ‘x’ for all of the balls. So x = x+speed gets called multiple times each cycle through draw(). Option one would be to have x1,x2,x3,etc. for all the balls, but a better option would be to use object-oriented programming and arrays (chapters 8 and 9). Or if all the balls really just stay together always, you could say:

    void setup() {
      size(200,200);
      background(0);
      noStroke();
      fill(255);
      smooth();
      frameRate(30);
    }
    
    float x = 10;
    float speed = 1;
    float diam = 10;
    
    void draw() {
      background(0);
      drawBall(30);
      drawBall(50);
      drawBall(70);
      drawBall(90);
      drawBall(110);
      drawBall(130);
    
      moveBall();
    }
    
    void drawBall(int yPos) {
      ellipse(x,yPos,diam,diam);
    }
    
    void moveBall(){
      if ((x > width-(diam/2))) {
        speed = speed*(-1);
      }
      x = x+speed;
    }
    

    Comment by Daniel Shiffman — March 11, 2010 @ 10:08 am

  3. Of course, it totally makes sense :)
    Thanks for your quick reply!

    Comment by Alex — March 12, 2010 @ 5:32 am

  4. in the exercise 7-5

    this is what i have done , but i am sure there is a simpler way to condense int total = a * b ;
    int total2 = c * d ; in one line but i don not know how ?
    Could you tell to me how

    Thanks

    P Mimran

    void multos ( int a , int b , int c , int d ) {
    int total = a * b ;
    int total2 = c * d ;
    println ( total) ;
    println (total2 ) ;

    }

    {
    multos ( 2 , 3 , 4 , 5 )

    }

    Comment by Patrick Mimran — March 12, 2010 @ 8:40 am

  5. I think what you want to do is:

    void setup() {
      int val1 = multos(2,3);
      int val2 = multos(4,5);
      println(val1);
      println(val2);
    }
    
    int multos(int a, int b) {
      return a*b;
    }
    

    Comment by Daniel Shiffman — March 12, 2010 @ 9:56 am

  6. thank you

    Patrick Mimran

    Comment by Patrick Mimran — March 12, 2010 @ 4:13 pm

Leave a comment