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);
}
  • Alex

    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);
    }
    
  • http://www.learningprocessing.com Daniel Shiffman

    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;
    }
    
  • Alex

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

  • http://www.mimran.com&patrickmimran.com Patrick Mimran

    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 )

    }

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

    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;
    }
    
  • http://www.mimran.com&patrickmimran.com Patrick Mimran

    thank you

    Patrick Mimran

  • http://ww.mimran.com Patrick mimran

    Hello

    there is something i do not understand ,
    i made my function called monDessin and i try to animate monDessin it only works when my variable is global , if i put movex inside myDessin no mouvement , why ?

    Thanks you for your time

    Patrick

    like in this code :
    float movex = 50 ;
    void setup () {
    size ( 300 , 300 ) ;
    }
    void draw () {
    background ( 100 ) ;
    monDessin () ;
    }
    void monDessin () {
    fill ( random (0 , 255) , 100 , 200 ) ;
    movex = movex + 1 ;
    rect ( movex , 20 , 50 , 50 ) ;
    ellipse ( movex , 80 , 50 , 10 ) ;
    }

    but if i move , movex inside myDessin function as below it does not work , Why ?

    void setup () {
    size ( 300 , 300 ) ;
    }
    void draw () {
    background ( 100 ) ;
    monDessin () ;
    }
    void monDessin () {

    float movex = 50 ;
    fill ( random (0 , 255) , 100 , 200 ) ;
    movex = movex + 1 ;
    rect ( movex , 20 , 50 , 50 ) ;
    ellipse ( movex , 80 , 50 , 10 ) ;
    }

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

    If the variable is not global, it is simply reset each time back to 50 and therefore never has the chance to increment.