# How to show the objects when the ball hits left and right side wall

**URL:** <https://discourse.processing.org/t/how-to-show-the-objects-when-the-ball-hits-left-and-right-side-wall/29252>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 9, 2021, 7:25pm UTC](https://discourse.processing.org/t/how-to-show-the-objects-when-the-ball-hits-left-and-right-side-wall/29252 "2021-04-09T19:25:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pou5678](https://avatars.discourse-cdn.com/v4/letter/p/3d9bf3/32.png) [@pou5678](https://discourse.processing.org/u/pou5678)\
**Post date:** [April 9, 2021, 7:25pm UTC](https://discourse.processing.org/t/how-to-show-the-objects-when-the-ball-hits-left-and-right-side-wall/29252/1 "2021-04-09T19:25:03Z")

</div>

I tried to do to fix this issue, but it didn’t work well… :/…  
You know this example is bouncing ball and I want to show the obstacle until ball hits obstacle. I want to get some help to solve. :)… (The problem is this code showed the obstacle one time, I just need to show the obstacle before ball hits the obstacle.  
Here is my code:  
/\* the speed of x and y , the position of x and y _/  
float xspeed; // x-velocity  
float yspeed; // y-velocity  
float xpos; // x-position  
float ypos; // y-position  
boolean stop=false; // Controller about changing ball’s color  
boolean obstacledraw=false;  
void setup(){  
frameRate(40);  
size(600,400); // set Canvas’ size  
noStroke(); // Disables drawing the stroke.  
colorMode(HSB,360,100,100); // changes the way Processing interprets color data.  
fill(290,100,100,255); // set ball’s initial color and screen before we push the key button  
background(0);  
noLoop();  
/_ set initial position about x and y, create initial speed of x and y \*/  
xpos = width/2; // xpos = 300  
ypos = height/2; // ypos = 200  
xspeed=random((width/150),(width/100)); // Velocity of x [4, 6]  
yspeed=random((height/100),(height/80)); // Velocity of y [4, 5]

}  
void draw(){  
int r = (width/120); // radius  
/\* Variables of obstacles _/  
float obs\_ox=-200; // obstacle x-position  
float obs\_oy=-200; // obstacle y-position  
background(0); // cover previous balls  
/_ movement of x and y about balls \*/  
xpos = xpos + xspeed;  
if (xpos+r \> width || xpos-r \< 0) {  
xspeed \*= -1;  
if(xpos \<= r && (ypos \<= 200) ){ // left top  
obs\_ox= 500;  
obs\_oy= 0;  
// fill(255);  
// rect(obs\_ox,obs\_oy,200,200);  
}  
else if(xpos \<= r && (ypos \> 200) ){ // left bottom  
obs\_ox = 500;  
obs\_oy= 300;  
// fill(255);  
// rect(obs\_ox,obs\_oy,200,200);  
}  
else if(xpos+r \> width && (ypos \<= 200) ){ // right top  
obs\_ox = 0;  
obs\_oy = 0;  
// fill(255);  
// rect(obs\_ox,obs\_oy,200,200);  
}  
else if(xpos+r \> width && (ypos \> 200) ){ // right bottom  
obs\_ox = 0;  
obs\_oy = 300;  
//fill(255);  
// rect(obs\_ox,obs\_oy,200,200);  
}  
obstacledraw=true;  
}

ypos = ypos + yspeed;  
if (ypos+r \> height || ypos-r \< 0) {  
yspeed _= -1;  
}  
/_ obstacles \*/

/\* When the ball hits the wall _/  
/_ if(xpos \<= r && (ypos \< (height/2)) ){ // hit the left top  
obs\_ox= random(400,500);  
obs\_oy=0;  
fill(255);  
rect(obs\_ox,obs\_oy,200,100);  
}  
else if(xpos \<= r && (ypos \> (height/2)) ){ // hit the left bottom  
obs\_ox=random(400,500);  
obs\_oy=300;  
fill(255);  
rect(obs\_ox,obs\_oy,200,100);  
}  
else if(xpos+r \>= width && (ypos \< (height/2)) ){ // hit the right top  
obs\_ox=0;  
obs\_oy=0;  
fill(255);  
rect(obs\_ox,obs\_oy,200,100);  
}  
else if(xpos+r \>= width && (ypos \> (height/2)) ) { // hit the right bottom  
obs\_ox=0;  
obs\_oy=random(250,300);  
fill(255);  
rect(obs\_ox,obs\_oy,100,200);  
}\*/  
//fill(255);  
//rect(obs\_ox,obs\_oy,200,100);  
if(obstacledraw == true){  
fill(255);  
rect(obs\_ox,obs\_oy,200,200);  
if(xpos-r \< obs\_ox || xpos+r \> obs\_ox){  
obstacledraw = false;  
}  
}

// draw the balls  
if(stop == false){ // change the color, if the mouse is pressed randomly.  
fill(random(255),random(255),random(255));  
}  
circle(xpos,ypos,2\*r);  
}  
void keyPressed(){  
final int k = keyCode; // k means keyCode to use s or S in if statements  
if(k == ‘s’ || k == ‘S’)  
if(looping) noLoop(); // stop if looping in the draw  
else loop(); // start  
}

void mousePressed(){  
stop = false; // control the color, if mouse is pressed, color gradually be changed  
}

void mouseReleased(){  
stop = true; // if mouse is released, changing color will be stop.  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 9, 2021, 8:32pm UTC](https://discourse.processing.org/t/how-to-show-the-objects-when-the-ball-hits-left-and-right-side-wall/29252/2 "2021-04-09T20:32:04Z")

</div>

little hard to say what you want

here something happens.

Please check how to post code

Thank you!

Chrisir

```auto

/* the speed of x and y, the position of x and y */
float xspeed; // x-velocity
float yspeed; // y-velocity

float xpos; // x-position
float ypos; // y-position

boolean stop=false; // Controller about changing ball’s color

boolean obstacledraw=true;

/* Variables of obstacles */
float obs_ox=-200; // obstacle x-position
float obs_oy=-200; // obstacle y-position

int r; // radius

void setup() {
  size(600, 400); // set Canvas’ size
  frameRate(40);

  noStroke(); // Disables drawing the stroke.
  colorMode(HSB, 360, 100, 100); // changes the way Processing interprets color data.
  fill(290, 100, 100, 255); // set ball’s initial color and screen before we push the key button
  background(0);
  // noLoop();

  // set initial position about x and y, create initial speed of x and y */
  xpos = width/2; // xpos = 300
  ypos = height/2; // ypos = 200
  xspeed=random((width/150), (width/100)); // Velocity of x [4, 6]
  yspeed=random((height/100), (height/80)); // Velocity of y [4, 5]
  r = (width/120); // radius
}

void draw() {

  background(0); // cover previous balls

  if (xpos <= r && (ypos <= 200) ) { // left top
    obs_ox= 500;
    obs_oy= 0;
    // fill(255);
    // rect(obs_ox,obs_oy,200,200);
  } else if (xpos <= r && (ypos > 200) ) { // left bottom
    obs_ox = 500;
    obs_oy= 300;
    // fill(255);
    // rect(obs_ox,obs_oy,200,200);
  } else if (xpos+r > width && (ypos <= 200) ) { // right top
    obs_ox = 0;
    obs_oy = 0;
    // fill(255);
    // rect(obs_ox,obs_oy,200,200);
  } else if (xpos+r > width && (ypos > 200) ) { // right bottom
    obs_ox = 0;
    obs_oy = 300;
    //fill(255);
    // rect(obs_ox,obs_oy,200,200);
  }
  // obstacledraw=true;

  reflectBall() ; 

  /* obstacles */

  /* When the ball hits the wall */
  if (xpos <= r && (ypos < (height/2)) ) { // hit the left top
    obs_ox= random(400, 500);
    obs_oy=0;
    //fill(255);
    //rect(obs_ox, obs_oy, 200, 100);
  } else if (xpos <= r && (ypos > (height/2)) ) { // hit the left bottom
    obs_ox=random(400, 500);
    obs_oy=300;
    //fill(255);
    //rect(obs_ox, obs_oy, 200, 100);
  } else if (xpos+r >= width && (ypos < (height/2)) ) { // hit the right top
    obs_ox=0;
    obs_oy=0;
    //fill(255);
    //rect(obs_ox, obs_oy, 200, 100);
  } else if (xpos+r >= width && (ypos > (height/2)) ) { // hit the right bottom
    obs_ox=0;
    obs_oy=random(250, 300);
    //fill(255);
    //rect(obs_ox, obs_oy, 100, 200);
  } // */

  // fill(255);
  //rect(obs_ox,obs_oy,200,100);

  if (obstacledraw) {
    fill(255);
    rect(obs_ox, obs_oy, 200, 200);
    if (xpos-r < obs_ox || xpos+r > obs_ox) {
      // obstacledraw = false;
    }
  }

  fill(255, 0, 220); 
  // draw the balls
  if (stop) {
    // change the color, if the mouse is pressed randomly.
    fill(random(255), random(255), random(255));
  }
  ellipse(xpos, ypos, 
    2*r, 2*r);
} // func draw()

// -------------------------------------------------------------------------------

void reflectBall() {
  // 
  /* movement of x and y about balls */
  xpos = xpos + xspeed;
  if (xpos+r > width || xpos-r < 0) {
    xspeed *= -1;
  }
  // 
  ypos = ypos + yspeed;
  if (ypos+r > height) {
    yspeed = -1; // neg
  }
  if (ypos-r < 0) {
    yspeed = 1; // pos
  }
}

void keyPressed() {
  final int k = keyCode; // k means keyCode to use s or S in if statements
  if (k == 's' || k == 'S')
    if (looping) noLoop(); // stop if looping in the draw
    else loop(); // start
}

void mousePressed() {
  stop = false; // control the color, if mouse is pressed, color gradually be changed
}

void mouseReleased() {
  stop = true; // if mouse is released, changing color will be stop.
}
//

```

---

<div class="post-metadata">

**Author:** ![pou5678](https://avatars.discourse-cdn.com/v4/letter/p/3d9bf3/32.png) [@pou5678](https://discourse.processing.org/u/pou5678)\
**Post date:** [April 9, 2021, 10:14pm UTC](https://discourse.processing.org/t/how-to-show-the-objects-when-the-ball-hits-left-and-right-side-wall/29252/3 "2021-04-09T22:14:27Z")

</div>

Oh, sorry :)… Also, thank you.
