# Coding problem in my game

**URL:** https://discourse.processing.org/t/coding-problem-in-my-game/41169
**Category:** Coding Questions
**Created:** [March 6, 2023, 8:46pm UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169 "2023-03-06T20:46:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![MaxU89](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@MaxU89](https://discourse.processing.org/u/MaxU89)
#### Post date: [March 6, 2023, 8:46pm UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169/1 "2023-03-06T20:46:31Z")

</div>

Hello. I have once again stumbled across a problem which I couldn’t resolve myself. My Plank does interact with the ball, but only sometimes. And sometimes on the edge the ball just bursts through. I think the problem is somewhere in the Ball class move function. I also noticed it has something to do with the direction the ball is coming down. If you have time, I would really appreciate if someone checked the code and saw for themselves. The game is in the early stages and the code looks messy, but I will fix it later :).

```auto
color colbcg;
Plank myPlank;
Ball myBall;

void setup(){
  size(500,500);
  myPlank = new Plank();
  myBall = new Ball();
}
  

void draw(){
  background(colbcg);
  myPlank.display();
  myPlank.move();
  myBall.display();
  myBall.move();
}
class Ball{
  int xbpos;
  int ybpos;
  int bw;
  int bl;
  int xbspeed;
  int ybspeed;
  int bcol;
  
 
  Ball(){
    xbpos=width/2;
    ybpos=393;
    bw=width/50;
    bl=height/50;
    xbspeed=3;
    ybspeed=-3;
    bcol=255;
    
  }
  void display(){
    ellipseMode(CENTER);
    noStroke();
    fill(bcol);
    ellipse(xbpos,ybpos,bw,bl);
  }
  void move(){
    ybpos+=ybspeed;
    xbpos+=xbspeed;
 
 
  if(xbpos<5||xbpos>width-5){
    xbspeed*=-1;
    
  }if(ybpos<0+5||ybpos>height-5){
    ybspeed*=-1;
   
  }if(xbpos>myPlank.xpos-15 && xbpos<myPlank.xpos+15 && ybpos>myPlank.ypos-7.4){
    ybspeed*=-1;
    
    
  }
  
 }

}
class Plank{
  int xpos;
  int ypos;
  int l;
  int w;
  int col;
  
  Plank(){
   xpos = width/2;
   ypos = height-(height*1/5);
   l = width/10;
   w = height/100;
   col = 255;
  }
  
   void display() {
     rectMode(CENTER);
     fill(col);
     rect(xpos,ypos,l,w);
   }
   
   void move(){
     if(mousePressed&&mouseButton == LEFT){
       xpos = mouseX;
     }     
     if(xpos<0+l/2){
       xpos=0+l/2;
     }if(xpos>width-l/2){
       xpos=width-l/2;
     }
   }
}

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 6, 2023, 9:10pm UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169/2 "2023-03-06T21:10:55Z")

</div>

Hi

Nice game I just remove &&mouseButton == LEFT on my tablet there is no mouse button option

```auto
if(mousePressed&&mouseButton == LEFT){

```

It’s working

```auto
color colbcg;
Plank myPlank;
Ball myBall;

void setup(){
  size(500,500);
  myPlank = new Plank();
  myBall = new Ball();
}
  

void draw(){
  background(colbcg);
  myPlank.display();
  myPlank.move();
  myBall.display();
  myBall.move();
}
class Ball{
  int xbpos;
  int ybpos;
  int bw;
  int bl;
  int xbspeed;
  int ybspeed;
  int bcol;
  
 
  Ball(){
    xbpos=width/2;
    ybpos=393;
    bw=width/50;
    bl=height/50;
    xbspeed=3;
    ybspeed=-3;
    bcol=255;
    
  }
  void display(){
    ellipseMode(CENTER);
    noStroke();
    fill(bcol);
    ellipse(xbpos,ybpos,bw,bl);
  }
  void move(){
    ybpos+=ybspeed;
    xbpos+=xbspeed;
 
 
  if(xbpos<5||xbpos>width-5){
    xbspeed*=-1;
    
  }if(ybpos<0+5||ybpos>height-5){
    ybspeed*=-1;
   
  }if(xbpos>myPlank.xpos-15 && xbpos<myPlank.xpos+15 && ybpos>myPlank.ypos-7.4){
    ybspeed*=-1;
    
    
  }
  
 }

}
class Plank{
  int xpos;
  int ypos;
  int l;
  int w;
  int col;
  
  Plank(){
   xpos = width/2;
   ypos = height-(height*1/5);
   l = width/10;
   w = height/100;
   col = 255;
  }
  
   void display() {
     rectMode(CENTER);
     fill(col);
     rect(xpos,ypos,l,w);
   }
   
   void move(){
     if(mousePressed ){
       xpos = mouseX;
     }     
     if(xpos<0+l/2){
       xpos=0+l/2;
     }if(xpos>width-l/2){
       xpos=width-l/2;
     }
   }
}

```

---

<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: [March 6, 2023, 11:20pm UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169/4 "2023-03-06T23:20:44Z")

</div>

> [@MaxU89](#):
>
> `ybspeed*=-1;`

I don’t like this expression although we see it in the examples and everywhere.

The reason is this: whenever the ball stays in the reverse area so that the expression is executed twice, the ball has the previous (wrong) direction again. Bad.  
This can lead to stuttering of the ball (or leaving of the ball).

Instead I prefer the expression

ybspeed=abs(ybspeed); OR

ybspeed= - abs(ybspeed);

which is either always positive or always negative.

(Same for xbspeed)

This means even if the ball remains in the reverse  
zone the outcome stays stable.

This means that you have to separate all ifs with  
|| in it into two separate ifs.

Also at the paddle use ybspeed=-abs(ybspeed);

---

<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: [March 7, 2023, 12:22am UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169/5 "2023-03-07T00:22:01Z")

</div>

> [@MaxU89](#):
>
> `if(ybpos<0+5||ybpos>height-5){`

Here you check the lower screen border; I think you have the south reflection check at the paddle and should not be repeated here.

---

<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: [March 7, 2023, 12:40am UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169/6 "2023-03-07T00:40:24Z")

</div>

> [@jafal](#):
>
> `(xbpos>myPlank.xpos-15 && xbpos<myPlank.xpos+15`

Here you assume a width of the plank = 30,  
but it’s 50 !

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [March 7, 2023, 1:55am UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169/7 "2023-03-07T01:55:51Z")

</div>

Hello @MaxU89,

Scrutinize this code snippet:

```auto
  //if(xbpos>myPlank.xpos-15 && xbpos<myPlank.xpos+15 && ybpos>myPlank.ypos-7.4){
    //ybspeed*=-1;
  
  // +y direction
  if(ybspeed>0 &&
     xbpos>myPlank.xpos-50 && // pl = 100
     xbpos<myPlank.xpos+50 &&
     
     ybpos>myPlank.ypos-20/2 -10 && // bw = 20 pw = 20
     ybpos<myPlank.ypos)
       {
       // set start of ybpos for new direction:
       ybpos = myPlank.ypos-20/2-10; // bw = 20 pw = 20
       ybspeed*=-1;
      //My code was something like this:
      //dir = -1;
      //speed = 3; // This was already set
      //ybspeed = dir*speed;
       }

  // -y direction
  
  // Code required here...   

```

I wrote a [Pong](https://en.wikipedia.org/wiki/Pong) from scratch without the classes.  
I adapted my efforts into your code and it seemed to work.

`:)`

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 7, 2023, 2:23am UTC](https://discourse.processing.org/t/coding-problem-in-my-game/41169/8 "2023-03-07T02:23:57Z")

</div>

> [@Chrisir](#):
>
> Here you assume a width of the plank = 30,  
> but it’s 50 !

You are always the best great teacher
