Help me with my pong(ish) code

Hello! I'm a beginner and am still trying to fully grasp processing. I decided, armed with the scarce knowledge that I have, to nonetheless try to program a ball bouncing around the screen that can be interacted it via a board (a line on the cursor), which would change its bouncing direction, etc.

This is the code I came up with so far, but I don't understand why the mouse interaction does not work.

float x = 20;
float y = 400;
float s = 0;
float g = 0.1;
float m = 6;
float inert = 0.75;

void setup() {
  size(800,800);
  background(255);
}
void draw() {
  background(255);
  line(mouseX,mouseY-100,mouseX,mouseY+100);
  fill(0);
  println(y,m,s);
  ellipse(x,y,20,20);
  x = x + m;
  m = m*0.996;
  y = y + s;
  s = s + g;
  inert = inert*0.9999;
  if((pmouseX<mouseX) && (mouseY-100<y) && (mouseY+100>y) && (mouseX == x-10)) {
    m = m*-1;

   }
  if(x>width) {
    x = width;
    m = m*-1;
  }
  else if (x<0) {
    x = 0;
    m = m*-1;
  }
   if(y>int(height)) {
     s = s*-inert;
     }

}

Can anyone explain please? This part specifically:

    if((pmouseX<mouseX) && (mouseY-100<y) && (mouseY+100>y) && (mouseX == x-10)) {
        m = m*-1

My idea with this was that if the line touches the ball on the right side, while it's moving left-to-right, the ball would change direction. I keep rechecking the parameters I've given and it all, theoretically, sounds correct. But it does not work. Any help would be appreciated. :)

Tagged:

Answers

  • mouseX == x-10

    better use > or < I guess

  • edited September 2017

    this is a simple pong game but I understand that your idea is totally different

    //position ball
    float x = 20;
    float y = 400;
    
    //movement ball
    float xAdd = 6;
    float yAdd = 2.6;
    
    void setup() {
      size(800, 800);
      background(255);
    }
    
    void draw() {
    
      background(255);
    
      //paddle
      stroke(0);
      line(10, mouseY-100, 10, mouseY+100);
    
      //ball
      fill(255, 0, 0);
      noStroke();
      ellipse(x, y, 20, 20);
    
      //move ball  
      x = x + xAdd;
      y = y + yAdd;
    
      // reflection on paddle
      if ( x<20 && y>mouseY-100 && y < mouseY+100 ) {
        xAdd = abs(xAdd);
      }
    
      //reflection on walls X 
      if (x>width-10) {
        x = width-10;
        xAdd = xAdd*-1;
        xAdd += random(-.1, .1);
      } else if (x<10) {
        // game over 
        //x = 0;
        //xAdd = abs(xAdd);
      }
    
      //reflection on walls Y
      if (y>height-10) {
        y = height-10;
        yAdd = yAdd*-1;
        yAdd += random(-.1, .1);
      } else if (y<10) {
        y = 10;
        yAdd = abs(yAdd);
        yAdd += random(-.1, .1);
      }
    }//func
    //
    
  • edited September 2017 Answer ✓

    this is following your idea more

    float x = 20;
    float y = 400;
    float m = 6; // xadd
    float s = 0;// yadd
    
    float g = 0.1;
    
    float inert = 0.75;
    
    void setup() {
      size(800, 800);
      background(255);
    }
    
    void draw() {
    
      background(255);
    
      //paddle 
      line(mouseX, mouseY-100, mouseX, mouseY+100);
    
      //ball
      fill(0);
      println(y, m, s);
      ellipse(x, y, 20, 20);
    
      x = x + m;
      m = m*0.996;
      y = y + s;
      s = s + g;
      inert = inert*0.9999;
    
      //paddle goes right 
      if ((mouseX>pmouseX) && 
        (y>mouseY-100) && (y<mouseY+100) && 
        (mouseX > x-10) && (mouseX < x+10) ) {
        m = abs(m); // ball reflects always flying right (m always pos)
      }
    
      //paddle goes left 
      if ((mouseX<pmouseX) && 
        (y>mouseY-100) && (y<mouseY+100) && 
        (mouseX > x-10) && (mouseX < x+10) ) {
        m = -abs(m); // ball reflects always flying left (m always neg)
      }
    
      if (x>width-10) {
        x = width-10;
        m = -abs(m);
      } else if (x<10) {
        x = 10;
        m = abs(m);
      }
      if (y>int(height)-10) {
    
        if (abs(s)<.19) 
          s=0;
        else
          s = abs(s)*-inert;
      }
    }
    
Sign In or Register to comment.