<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #pong - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23pong</link>
      <pubDate>Sun, 08 Aug 2021 18:04:42 +0000</pubDate>
         <description>Tagged with #pong - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23pong/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>While loop only executing once?</title>
      <link>https://forum.processing.org/two/discussion/25261/while-loop-only-executing-once</link>
      <pubDate>Wed, 29 Nov 2017 00:19:21 +0000</pubDate>
      <dc:creator>louisvscharles</dc:creator>
      <guid isPermaLink="false">25261@/two/discussions</guid>
      <description><![CDATA[<p>I'm almost completely new to programming in general so excuse my naivety.</p>

<p>I'm trying to draw a dashed line for my pong game yet it only draws the first two (the first one being in void setup and the second being inside the while loop). Surely the variables 'line1' and 'line2' shouldn't be reset as the 'void setup' block of code would only be executed again when the while evaluates to false (ie when line1 &gt;= 600 and line2 &gt;= 592) in which case the 
while would loop until the dashed line reached the bottom of the window. I don't know where I'm going wrong.</p>

<pre><code>int ballX, ballY;
int ballRadius = 7;
int paddle1X, paddle1Y;
int paddle2X, paddle2Y;      
int paddle1Hor = 6, paddle1Vert = 30;    
int paddle2Hor = 6, paddle2Vert = 30;  
int i;
int line1;
int line2;


void setup()
{
  noSmooth();
  frameRate(30);
  size(800, 600);
  paddle1X = 50; 
  paddle1Y = 50; 
  paddle2X = 750;
  paddle2Y = 425;
  ballX = 398;
  ballY = 298;
  i = 0;
  line1 = 0;
  line2 = 8;
}

void draw()
{
  background(0);
  fill(255, 255, 255);
  rect(paddle1X, paddle1Y, paddle1Hor, paddle1Vert);
  rect(paddle2X, paddle2Y, paddle2Hor, paddle2Vert);
  rect(ballX, ballY, ballRadius, ballRadius);
  line(400, line1, 400, line2);
  while (line1 &gt;= 600 &amp;&amp; line2 &gt;= 592);{
    line(400, line2+15, 400, line1+15);
  }
  stroke(255);

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>in the 'Pong" game, the ball doesn't hit and come back with the paddles</title>
      <link>https://forum.processing.org/two/discussion/25197/in-the-pong-game-the-ball-doesn-t-hit-and-come-back-with-the-paddles</link>
      <pubDate>Sat, 25 Nov 2017 17:16:57 +0000</pubDate>
      <dc:creator>nevermind</dc:creator>
      <guid isPermaLink="false">25197@/two/discussions</guid>
      <description><![CDATA[<p>Hi, guys</p>

<p>I downloaded open source the game Pong two players.
I resized all the stuff in general to fit in my screen 1920*1080,
after this the ball cannot hit by the paddle and come back.
can you check this code out and let me know how can I fix this?
Thnx.</p>

<p>--------------------code----------------------------------------------</p>

<p>/* OpenProcessing Tweak of <em>@</em><a href="http://www.openprocessing.org/sketch/7254" target="_blank" rel="nofollow">http://www.openprocessing.org/sketch/7254</a><em>@</em> <em>/
/</em> !do not delete the line above, required for linking your tweak if you upload again */</p>

<p>boolean isGameStarted = false; //start variable</p>

<p>Paddle leftPaddle  = new Paddle(  30, 500 );
Paddle rightPaddle = new Paddle(1870, 500 );
Ball   ball        = new   Ball( 1000, 500 );
Player  leftPlayer = new Player( 80, 900, 1, leftPaddle );
Player rightPlayer = new Player( 280, 900, 2, rightPaddle );</p>

<p>void setup() {
  size( 1920, 1080 );
  //rectMode( CENTER );
  //ellipseMode( CENTER );
  smooth();
  strokeWeight( 2 );
  frameRate( 130 );
  PFont arial;
  arial = loadFont( "ariblk.vlw" );
  textFont( arial, 48 );
}</p>

<p>void draw() {
  processUserInput();
  //starting page 
  if ( isGameStarted == false ) {
    background(0);
    textSize(70);
    text("Pong!", 850, 400);
    textSize(30);
    text("with Arduino", 850, 450);
    textSize(30);
    text("_by Sohyun", 890, 510);
    textSize(90);
    text("Start?", 800, 650);
    stroke(255);
    noFill();
    rect(760, 550, 370, 160);
  } else {
    //real setup here
    background(100);
    stroke(0);
    textSize(48);
    fill(255);
    line(950, 0, 950, 1200);</p>

<pre><code>int leftdY = 0;
int rightdY = 0;
if ( isKeyPressed['q'] ) {        //Move the left player up.
  leftdY -= 0.5;
}
if ( isKeyPressed['a'] ) {       //Move the left player down.
  leftdY += 0.5;
}
if ( isKeyPressed['o'] ) {       //Move the right player up.
  rightdY -= 0.5;
}
if ( isKeyPressed['l'] ) {       //Move the right player down.
  rightdY += 0.5;
}

ball.move();
leftPaddle.move(  leftdY );
rightPaddle.move( rightdY );
ball.bounceOnWall();
ball.bounceOnPaddle(  leftPaddle );
ball.bounceOnPaddle( rightPaddle );

ball.display();
leftPaddle.display();
rightPaddle.display();
leftPlayer.handleGoalIfOtherPlayerScores( ball );
rightPlayer.handleGoalIfOtherPlayerScores( ball );
leftPlayer.display();
rightPlayer.display();
</code></pre>

<p>}
}</p>

<p>void drawPlayfield() {
  background(100);
  stroke(0);
  textSize(48);
  fill(255);
  line(950, 0, 950, 1200);
}</p>

<p>void resetGame() {
  leftPlayer.score = 0;
  rightPlayer.score = 0;
  leftPaddle.y = 500;
  rightPaddle.y = 500;
  ball.y = 500;
  ball.vx = -1.5;
}</p>

<p>class Ball {
    float x;
    float y;</p>

<pre><code>float vx;
float vy;

Ball( int newX, int newY ) {
    x = newX;
    y = newY;
    if ( random(1) &lt; .5 ) {
        vx = 2;
    } else {
        vx = -2;
    }
}

void display() {
    fill( 255 );
    ellipse( x, y, 50, 50 );
}

void move() {
    x = x + vx;             
    y = y + vy;
}

void reset() {   //after one lose where ball will be started
    x = 950;
    y = 500;
    vy = 0;
}

void bounceOnWall() {
    if (  y &lt; 0 || y &gt; height ) {
        vy = -vy;
    }

    bounceOnPaddle(  leftPaddle );
    bounceOnPaddle( rightPaddle );
}

void bounceOnPaddle( Paddle paddle ) {


  if (      paddle.x &lt; width / 2 &amp;&amp; x == ( paddle.x + 6 )
         || ( paddle.x &gt; width / 2 &amp;&amp; x == ( paddle.x - 6 ) ) ) {
        if ( y &gt;= paddle.y - 30 &amp;&amp; y &lt;= paddle.y + 30 ) {
            println( "bounce" + paddle );
            if ( y &lt; paddle.y - 20 ) {
                vx = -vx;
                vy -= 2.5;
            } else if ( y &gt; paddle.y + 20 ) {
                vx = -vx;
                vy += 2.5;
            } else {
                vx = -vx;
            }
            vy = constrain( vy, -1.5, 1.5 );
            move();
        }
    }
}
</code></pre>

<p>}</p>

<p>boolean[] isKeyPressed      = new boolean[256];
boolean[] isCodedKeyPressed = new boolean[256];</p>

<p>void handleKeyEvent( boolean isPressed ) {
    if ( key == CODED ) {
        isCodedKeyPressed[ keyCode ] = isPressed;
    } else {
        isKeyPressed[ key ] = isPressed;
    }
}</p>

<p>void keyPressed() {
    handleKeyEvent( true );
}</p>

<p>void keyReleased() {
    handleKeyEvent( false );
}</p>

<p>//thea area with the start button retangle
void processUserInput() {<br />
    if ( isGameStarted == false &amp;&amp; mousePressed &amp;&amp; ( mouseX &lt;= 1150 &amp;&amp; mouseX &gt;= 750 )
        &amp;&amp; ( mouseY &lt;= 650 &amp;&amp; mouseY &gt;= 500 ) ) {
        isGameStarted = true;
        resetGame();<br />
    } else {
        moveLeftPlayer();
        moveRightPlayer();
    }
}</p>

<p>//range of the paddles move<br />
void moveLeftPlayer() {<br />
    if ( isKeyPressed['q'] || isKeyPressed['Q'] ) {        //Move the left player up.
        leftPaddle.y = max(25, leftPaddle.y-2);
    }
    if ( isKeyPressed['a'] || isKeyPressed['A']) {       //Move the left player down.
        leftPaddle.y = min(950, leftPaddle.y+2);
    }
}</p>

<p>void moveRightPlayer() {
    if (isKeyPressed['o'] || isKeyPressed['O'] ) {       //Move the right player up.
        rightPaddle.y = max(25, rightPaddle.y-2);
    }
    if ( isKeyPressed['l'] || isKeyPressed['L'] ) {       //Move the right player down.
        rightPaddle.y = min(950, rightPaddle.y+2);
    }
}</p>

<p>class Paddle {
    int x;
    int y;</p>

<pre><code>Paddle( int newX, int newY ) {
    x = newX;
    y = newY;
}

void display() {
    stroke( 0 );
    fill( 80 );
    rect( x - 6, y - 25, 25, 120 );
    stroke( 255, 0, 0 );
    line( x-6, y - 25, x+6, y - 25 );
    line( x-6, y + 25, x+6, y + 25 );
}

void move( int dY ) {
     y = constrain( y + dY, 25, height - 25 );      // the range of moving paddle
}
</code></pre>

<p>}</p>

<p>class Player {
    int x;
    int y;</p>

<pre><code>int nr;
int score;
Paddle paddle;

Player( int newX, int newY, int newNr, Paddle playerPaddle ) {
    x      = newX;
    y      = newY;
    nr     = newNr;
    score  = 0;
    paddle = playerPaddle;
}
</code></pre>

<p>//how score looks like
    void display() {
        stroke( 0 );
        fill(255);
        textSize(100);
        text( score, x, y );  //place where the score
    }</p>

<pre><code>void handleGoalIfOtherPlayerScores( Ball ball ) {
    if ( ball.x &lt; 0 &amp;&amp; paddle.x &gt; width / 2 ) {
        score++;
        ball.reset();
    }
    if ( ball.x &gt; width &amp;&amp; paddle.x &lt; width / 2 ) {
        score++;
        ball.reset();
    }
    if ( score &gt;= 10 ) {
        text("player" + nr + " wins!", 50, 100 );
        textSize( 20 );
        text("&lt;click to restart&gt;", 110, 150);
        isGameStarted = false;
    }
}
</code></pre>

<p>}</p>
]]></description>
   </item>
   <item>
      <title>Help me with my pong(ish) code</title>
      <link>https://forum.processing.org/two/discussion/24001/help-me-with-my-pong-ish-code</link>
      <pubDate>Fri, 01 Sep 2017 20:29:46 +0000</pubDate>
      <dc:creator>randomdude</dc:creator>
      <guid isPermaLink="false">24001@/two/discussions</guid>
      <description><![CDATA[<p>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.</p>

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

<pre><code>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&lt;mouseX) &amp;&amp; (mouseY-100&lt;y) &amp;&amp; (mouseY+100&gt;y) &amp;&amp; (mouseX == x-10)) {
    m = m*-1;

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

}
</code></pre>

<p>Can anyone explain please? This part specifically:</p>

<pre><code>    if((pmouseX&lt;mouseX) &amp;&amp; (mouseY-100&lt;y) &amp;&amp; (mouseY+100&gt;y) &amp;&amp; (mouseX == x-10)) {
        m = m*-1
</code></pre>

<p>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. :)</p>
]]></description>
   </item>
   <item>
      <title>Bounce Ball off of Paddle</title>
      <link>https://forum.processing.org/two/discussion/23866/bounce-ball-off-of-paddle</link>
      <pubDate>Sat, 19 Aug 2017 21:35:35 +0000</pubDate>
      <dc:creator>gleister</dc:creator>
      <guid isPermaLink="false">23866@/two/discussions</guid>
      <description><![CDATA[<p>Hey, back again with another question... I got my ball to move, my paddle to display and to move! Now, I just cant seem to get the paddle to reverse the Ball's direction by having it bounce off of it. Ive tried a handful of different methods, but none have been successful. Sometimes the ball just starts bouncing up and down at the top of the screen.</p>

<p>Here is my Code:</p>

<pre><code>    //Gwendolyn Leister - Pong Game
    //Hit the ball with the paddle by moving the mouse left and right 
    //Each time you hit the paddle, your score goes up

    //Many thanks to the two forums that helped me: 
    //<a href="https://forum.processing.org/two/discussion/23862/subclass-won-t-display-or-move-help#latest" target="_blank" rel="nofollow">https://forum.processing.org/two/discussion/23862/subclass-won-t-display-or-move-help#latest</a>
    //<a href="https://forum.processing.org/two/discussion/23842/myclass-move-does-not-exist-but-it-does#latest" target="_blank" rel="nofollow">https://forum.processing.org/two/discussion/23842/myclass-move-does-not-exist-but-it-does#latest</a>


    //Global Variables
    boolean gameStart = false;

    //Declare Fonts
    PFont myFont;
    PFont myFont2;

    final GamePiece Ball = new Ball();
    final GamePiece Paddle = new Paddle();

    void setup () {
      size(800, 800);
      smooth();
      background(0);
      myFont = loadFont("AnonymousPro-30.vlw"); //smaller text font
      myFont2 = loadFont("AnonymousPro-80.vlw"); //larger title font
    } 

    void draw() {

      background (#0A141F);

      //Layout
      rectMode(CORNER);
      fill(#CBFEFF);
      noStroke();
      rect(0, 640, 800, 260);
      stroke(255);
      fill(#CBFEFF);
      strokeWeight(10);
      line(0, 640, 800, 640);
      fill(#0A141F);
      rect(200, 700, 400, 160);

      //score text 
      textFont(myFont);
      textAlign(RIGHT);
      textSize(20);
      text("Score:", 70, 25);

      //display classes
      Ball.display();
      Paddle.display();

      if (gameStart) { //if the user hits enter to start game and allow subclasses to move/bounce
        Ball.move();
        Ball.bounce();
        Paddle.move();
      } else {  //when enter is not pressed or if it is pressed twice, it will pause the game

        textAlign(CENTER);
        textFont(myFont2);
        textSize(80);
        text("P O N G", 400, 100);
        textSize(20);
        text("press enter to start", 400, 600);
        text("move mouse horizontally to control the", 400, 140);
        text("paddle and to bounce the ball off of", 400, 162);
      }
    }



    // Resource used: <a href="https://processing.org/tutorials/interactivity/" target="_blank" rel="nofollow">https://processing.org/tutorials/interactivity/</a>
    void keyPressed() { //if enter is pressed then the game starts {
      if (keyCode == ENTER) {
        gameStart = !gameStart;
      } else {
      }
    }

    abstract class GamePiece {
      //ball variables
      int rad = 15;
      float xPos=width/2;
      float yPos=height/2;
      float Speed = 6; 
      float xDirection = -2;
      float yDirection = -1;

      int score = 0;//call booleans
      boolean mouseMoved;
      boolean overPaddle = false;
      boolean locked = false;
      float dx=0;

      //paddle variables
      int px=mouseX;
      int py=550;
      int pw=100;
      int ph=30;
      int pSpeed = 5;

      int pDistance = 250; // paddle dis


      //calls
      abstract void move(); 
      abstract void display();
      abstract void bounce();
    }


    class Ball extends GamePiece {
      //Resource used: <a href="https://processing.org/examples/bounce.html" target="_blank" rel="nofollow">https://processing.org/examples/bounce.html</a>


      int rad = 15;
      float xPos=width/2;
      float yPos=height/2;
      float Speed = 5;
      float xDirection = -2;
      float yDirection = -1;

      Ball() {
        super();
      }

      void display() {
        stroke(255);
        strokeWeight(3);
        fill(#FCE1BD);
        frameRate(30);
        ellipseMode(RADIUS);
        ellipse(xPos, yPos, rad, rad);
      }
      void move() { // ball position change
        xPos=xPos+Speed*xDirection;
        yPos=yPos+Speed*yDirection;
      }
      void bounce() { //<a href="https://processing.org/examples/bounce.html" target="_blank" rel="nofollow">https://processing.org/examples/bounce.html</a>

        if (yPos - rad &lt; py + ph/2) { // bounce ball off of paddle
          yDirection = -yDirection;  // when i apply this, then the ball acts crazy

          if (xPos&gt;(width-rad)) { //bounce right side
            xPos = width-rad;
            xDirection=-xDirection;
          }
          if (xPos&lt; rad) { //bounce left side
            xPos = rad;
            xDirection=-xDirection;
          }
          if (yPos&lt;rad) { //bounce top side
            yPos=rad;
            yDirection=-yDirection;
          }

          if (yPos&gt;800) { //falls below screen and relocates ball to inside the frame
            yDirection = -1;
            xPos=width/2;
            yPos=height/2;
          }
        }
      }
    }

    class Paddle extends GamePiece {

      Paddle () {
        super();
      }

      void display() {
        rectMode(CENTER);
        fill(#CBFEFF);
        stroke(255);
        strokeWeight(5);
        rect(mouseX, py, pw, ph);  //moves paddle with mouse
      }

      void move() {
      }

      void bounce () { 
      }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Subclass won't display or move -- Help!</title>
      <link>https://forum.processing.org/two/discussion/23862/subclass-won-t-display-or-move-help</link>
      <pubDate>Sat, 19 Aug 2017 14:59:46 +0000</pubDate>
      <dc:creator>gleister</dc:creator>
      <guid isPermaLink="false">23862@/two/discussions</guid>
      <description><![CDATA[<p>Back again with another class issue, this time after three days of troubleshooting, I am still stuck on why my paddle won't display. I would appreciate any help I can get. Thanks!</p>

<p>Main Tab 
    //Gwendolyn Leister - Pong Game</p>

<pre><code>GamePiece Paddle;
GamePiece Ball;

boolean gameStart = false;
boolean restart = false;
boolean endgame = false;
//Declare Fonts
PFont myFont;
PFont myFont2;

//for Ball 
int rad = 15;
float xPos=width/2;
float yPos=height/2;
float Speed = 3;

int xDirection = 1;
int yDirection = 1;
int SPD = 5;

int score = 0;


final GamePiece ball = new Ball();

final GamePiece paddle = new Paddle();

void setup () {
  size(800, 800);
  smooth();
  background(0);
  myFont = loadFont("AnonymousPro-30.vlw");
  myFont2 = loadFont("AnonymousPro-80.vlw");
  Ball = new Ball();
}

void draw() {
  //table


  background (0);

  //Layout
  fill(#CBFEFF);
  noStroke();
  rect(0, 640, 800, 260);
  stroke(255);
  fill(#CBFEFF);
  strokeWeight(10);
  line(0, 640, 800, 640);
  fill(#EAFFFF);
  rect(200, 700, 400, 160);
  //text

  //score text
  textFont(myFont);
  textAlign(RIGHT);
  textSize(20);
  text("Score:", 70, 25);

  Ball.display();
  //Paddle.display();

  if (gameStart) {
    Ball.move();
    Ball.bounce();
    //Paddle.move();
  } else {
    textAlign(CENTER);
    textFont(myFont2);
    textSize(80);
    text("P O N G", 400, 100);
    textSize(20);
    text("press enter to start", 400, 600);
    text("move mouse horizontally to control the", 400, 140);
    text("paddle and to bounce the ball off of", 400, 162);
  }
  //if (keyPressed) {
  //  restart = !restart;
  //}
}

//if (Ball.checkScore) {
//  score = score+1;
//  textSize(20);
//  textAlign(LEFT);
//  text("

void checkScore() {
  if (yPos&gt;700) {
    score=score+1;
  } else if (yPos&gt;750) {
    score=score-1;
  }
}

void keyPressed() {
  if (keyCode == ENTER){
    gameStart = !gameStart;
  }
  else{

  }
}
</code></pre>

<p>Paddle SubClass
    class Paddle extends GamePiece {</p>

<pre><code>  boolean mouseMoved;
  boolean overPaddle = false;
  boolean locked = false;
  float xOffset = 0.0;
  float yOffset = 0.0;
  int px;
  int py;
  int pw=100;
  int ph=30;
  int pSpeed= 5;


  //float xPos = mouseX;
  //float yPos = 680;
  //int x;
  //int y;
  //int w=20;
  //int h=80;
  //Paddle (int tx,int ty) {
  //  x=tx;
  //  y=ty;
  //}



  Paddle () {
    super();
  }

  void display() {
    rectMode(CENTER);
    fill(#CBFEFF);
    stroke(255);
    strokeWeight(10);
    //rect(this.xPos, this.yPos, 50, 20);
  }
  //void mouseMove() {
  //  if (x==1) {
  //    py = py +- pSpeed;
  //  } else if (x == 0);
  //}
  void move () {

    if (mouseX &gt; px-(pw*ph) &amp;&amp; mouseX &lt; px+(pw*ph) &amp;&amp; 
      mouseY &gt; py-(pw*ph) &amp;&amp; mouseY&lt;py+(pw*ph)) {
      overPaddle=true;
      if (!locked) {
        stroke(255);
        fill(255);
      } else {
        fill(#CBFEFF);
        stroke(255);
        strokeWeight(10);
      }
      rect(px, py, pw, ph);
    }
  }
  void mousePressed() {
    if (overPaddle) {
      locked = true;
      fill (#CBFEFF);
    } else {
      locked = false;
    }
    xOffset =mouseX-px;
  }
  void bounce() {
  }
}
</code></pre>

<p>and GamePiece Superclass</p>

<pre><code>abstract class GamePiece {
  float x, y, w, h, vx, vy, dx, dy;
  abstract void move(); 
  abstract void display();
  abstract void bounce();
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Pong</title>
      <link>https://forum.processing.org/two/discussion/23725/pong</link>
      <pubDate>Sun, 06 Aug 2017 13:39:55 +0000</pubDate>
      <dc:creator>ThatOnePandaGuy</dc:creator>
      <guid isPermaLink="false">23725@/two/discussions</guid>
      <description><![CDATA[<p>Hello, im trying to make a pong game. I have my ball bouncing around but i need to get the ball to bounce off of the paddles. Im not sure how to do this iv looked at the Circle Collision example but its to much for me to understand. If you could make it easier to understand that would be great!</p>
]]></description>
   </item>
   <item>
      <title>question about an if statement in pong</title>
      <link>https://forum.processing.org/two/discussion/23489/question-about-an-if-statement-in-pong</link>
      <pubDate>Mon, 17 Jul 2017 20:49:51 +0000</pubDate>
      <dc:creator>lolnyancats</dc:creator>
      <guid isPermaLink="false">23489@/two/discussions</guid>
      <description><![CDATA[<pre><code>int p1x, p1y;
int p2x, p2y;
int ballx, bally, ballvx, ballvy;

void setup() {
  size(1000, 1000);
  p1x=25;
  p1y=height/2;
  p2x=width-25;
  p2y=height/2;
  ballx=width/2;
  bally=height/2;
  int r=(int)(random(0, 1));
  if (r==0) {
    ballvx=-1;
  } else {
    ballvx=1;
  }
  int ra=(int)(random(0, 1));
  if (ra==0) {
    ballvy=-1;
  } else {
    ballvy=1;
  }
}
void draw() {
  background(255/2+50);
  ellipse(ballx, bally, 25, 25);
  ballx+=ballvx;
  bally+=ballvy;
  if ((ballx&gt;=p1x&amp;&amp;ballx&lt;=p1x-25)||(ballx&gt;=p2x+25&amp;&amp;ballx&lt;=p2x-25)&amp;&amp;(bally&gt;=p1y+25||bally&lt;=p1y-25||bally&gt;=p1y+25||bally&lt;=p1y-25))
  {
    ballvx=-ballvx;
    println("deflected");
  }
  if (((bally&gt;=p1y+25&amp;&amp;bally&lt;=p1y-25)||(bally&gt;=p2y+25&amp;&amp;bally&lt;=p2y-25))&amp;&amp;(ballx&lt;=p1x||ballx&gt;=p2x)||(bally&gt;=height+25||bally&lt;=25))
  {
    ballvy=-ballvy;
    println("deflected");
  }
  if(ballx&lt;=0||ballx&gt;=height-25){
    textSize(50);
    fill(255,0,0);
    text("YOU LOST",width/2,height/2);
  }
  rect(p1x, p1y, 10, 50);
  rect(p2x, p2y, 10, 50);
}
void keyPressed() {
  if (keyCode=='W')
  {
    p1y+=5;
    println("paddle 1 increasing");
  }
  if (keyCode=='S') {
    p1y-=5;
    println("paddle 1 decreasing");
  }
  if (keyCode=='Y')
  {
    p2y+=5;
    println("paddle 2 increasing");
  } 
  if (keyCode=='H') {
    p2y-=5;
    println("paddle 2 decreasing");
  }
}
</code></pre>

<p>so the problem that i am encountering is that the if statement randomly deflects the ball so i added <code>(ballx&lt;=p1x||ballx&gt;=p2x)</code>
to the y section. can someone rewrite the if statements for me or just tell me how to</p>
]]></description>
   </item>
   <item>
      <title>Ping Pong Game using Arduino and Accelerometer</title>
      <link>https://forum.processing.org/two/discussion/23463/ping-pong-game-using-arduino-and-accelerometer</link>
      <pubDate>Sun, 16 Jul 2017 06:30:35 +0000</pubDate>
      <dc:creator>Aswinth_Raj</dc:creator>
      <guid isPermaLink="false">23463@/two/discussions</guid>
      <description><![CDATA[<p><img src="https://circuitdigest.com/sites/default/files/projectimage_mic/Testing-of-Ping-pong-ball-game-using-Arduino-and-accelerometer.jpg" alt="" /></p>

<p>Augmented Reality and Virtual Gaming has become a recent trend in the gaming industry. The times of using a keyboard/Joystick and a mouse to play a computer game has gone behind. Now every gaming console comes with a Virtual Controller that helps us to play the game using our body movements and gestures, this way the gaming experience has increased a lot and user feels more involved into the game.
In this project let's try to have fun as we learn through the project. Let us create a game (Yes you heard me correct we are goanna create a game) and play it using your hand’s movement. We are creating the classic Ping Pong Ball Game using Arduino and Accelerometer.</p>

<p>More details with code and video : <a rel="nofollow" href="https://circuitdigest.com/microcontroller-projects/ping-pong-game-using-arduino-accelerometer">https://circuitdigest.com/microcontroller-projects/ping-pong-game-using-arduino-accelerometer</a></p>
]]></description>
   </item>
   <item>
      <title>How to restart a game using keyPressed()</title>
      <link>https://forum.processing.org/two/discussion/23081/how-to-restart-a-game-using-keypressed</link>
      <pubDate>Thu, 15 Jun 2017 21:33:10 +0000</pubDate>
      <dc:creator>Fyzq</dc:creator>
      <guid isPermaLink="false">23081@/two/discussions</guid>
      <description><![CDATA[<p>So I am doing a project for my programming class that is a pong game. The problem I currently have is getting the game to restart after the score limit of 10 is reached by either player. I am trying something I read from another post which is to make a reset function, redraw the background, reset variables, etc. but I'm having trouble getting it to work properly even though I feel I did the right things, I've been trying different options but nothing is fixing it. So I'm wondering if any of you see any problem with my code?</p>

<p>Restart function inside Ball class (line 79):</p>

<pre><code>    class Ball {

  float x = width/2; // variables for ball class x,y locations, radius, and x/y speeds
  float y = height/2;
  int r = 15;
  float xspeed;
  float yspeed;
  float ballAngle = random(-PI/4, PI/4);

  Ball() { // ball class constructor
    reset(); // reset function
  }

  void move() { // function to move ball
    x = x + xspeed;
    y = y + yspeed;
  }

  void display() { // function to display ball
    fill(170);
    stroke(0);
    strokeWeight(1);
    ellipse(x, y, r*2, r*2);
  }

  void checkEdges() { // function to check the top and bottom so ball doesn't go off the screen // also score function
    if (y &lt; 0 + r || y &gt; height - r) {
      yspeed = yspeed *-1;
    }
    if (x + r &lt; 0) {
      cpuScore++;
      reset();
    }
    if (x - r &gt; width) {
      userScore++;
      reset();
    }
  }

  void reset() { // reset the ball when it reaches the end of the right or left side of the screen
    xspeed = 8 * cos(ballAngle);
    yspeed = 8 * sin(ballAngle);

    if (x &lt; 0 - r) { 
      x = width/2;
      y = random(0+r*2, 400-r*2);
    }
    if (x &gt; width + r) {
      x = width/2;
      y = random(0+r*2, 400-r*2);
      xspeed *= -1;
    }
  }

  void isUserPHit(Paddle p) { // detect if user paddle is hit
    if (y &lt; p.y + p.h/2 + r &amp;&amp; y &gt; p.y - p.h/2 - r &amp;&amp; x - r &lt; p.x + p.w/2) {
      if (xspeed &lt; 0) {
        //paddlehit.play();
        float diff = y - (p.y - p.h/2);
        float rad = radians(45);
        float angle = map(diff, 0, p.h, -rad, rad);
        xspeed = 8 * cos(angle);
        yspeed = 8 * sin(angle);
      }
    }
  }
  void isCPUPHit(Paddle p) { // detect if cpu paddle is hit
    if (y &lt; p.y + p.h/2 + r &amp;&amp; y &gt; p.y - p.h/2 - r &amp;&amp; x + r &gt; p.x - p.w/2) {     
      if (xspeed &gt; 0) {
        //paddlehit.play();
        float diff = y - (p.y - p.h/2);
        float angle = map(diff, 0, p.h, radians(225), radians(135));
        xspeed = 8 * cos(angle);
        yspeed = 8 * sin(angle);
      }
    }
  }

  void restart() {
    xspeed = 8 * cos(ballAngle);
    yspeed = 8 * sin(ballAngle);
    userScore = 0;
    cpuScore = 0;
    background(255); 
    // line to divide the two sides
    strokeWeight(3);
    line(width/2, 0, width/2, height);
    fill(0);
    textSize(20);
    textAlign(CENTER);
    text("User Score:", 78, 25); // text for the scores
    text("CPU Score:", 493, 25);

    text(userScore, 147, 25); // text for the numbers, currently set at “0”, but will update later
    text(cpuScore, 558, 25);


    display(); // display the ball
    move(); // move the ball
    checkEdges(); // check if the ball is reaching the top or bottom so it bounces off rather than going off the screen

    userP.display(); // display the paddles
    cpuP.display();

    userP.moveY(mouseY); // move the user’s paddle vertically with the mouse’s y position 

    isUserPHit(userP); // detect if either paddle comes in contact with the ball
    isCPUPHit(cpuP);

    cpuP.cpuAI(ball); // initiate computer AI for the computer paddle
    scorelimit(ball); // check to see if either player reaches the score limit(10)
  }
}
</code></pre>

<p>Main tab (a lot of code I know, sorry):</p>

<pre><code>int userScore = 0; // variables for both scores in order to update it as points are scored
int cpuScore = 0;
int state = 0; // state of the game
boolean startgame = false; // boolean to make it so a button works to start the game scene
boolean help = false; // boolean to make it so a button works to display the help scene
boolean back = false; // boolean to make it so a button works to go back to main scene
boolean endofgame = false;

Ball ball; // declare Ball object


Paddle userP; // declare Paddle objects
Paddle cpuP;

void setup() {
  // screen size
  size(600, 400);
  smooth();
  frameRate(60);

  //paddlehit = new SoundFile(this, "paddlehit.wav");

  ball = new Ball(); // initialize Ball object to create a new ball


  userP = new Paddle(15, 200); // initialize Paddle object to create two new paddles
  cpuP = new Paddle(585, 200);
}

void draw() { 
  if (state == 0) {
    mainScene();
  }
  if (state == 1) {
    gameScene();
  }
  if (state == 2) {
    helpScene();
  }

  if (startgame == true) {
    state = 1;
  }

  if (help == true) {
    state = 2;
  }

  if (back == true) {
    state = 0;
  }


  if (endofgame == true) {
    ball.restart();
  }

  // E - for (Ball2 b : ball2s) b.run(); // for loop to make all balls in arraylist to have functions located inside of run().
}

void scorelimit(Ball b) { // endgame for when someone reaches the score limit
  if (userScore &gt;= 1) {
    b.x = width/2;
    b.y = height/2;
    b.xspeed = 0;
    b.yspeed = 0;
    fill(0, 75, 255);
    textSize(24);
    textAlign(CENTER);
    text("You win!", width/2-140, height/2);
    textSize(20);
    fill(0);
    text("Press ENTER to restart", width/2+150, height/2-100);
  } else if (cpuScore &gt;= 1) {
    b.x = width/2;
    b.y = height/2;
    b.xspeed = 0;
    b.yspeed = 0;
    fill(255, 0, 75);
    textSize(24);
    text("CPU wins..", width/2+140, height/2);
    textSize(20);
    fill(0);
    text("Press ENTER to restart", width/2-150, height/2-100);
  }
}

void mainScene() {
  background(255);
  fill(0);  // border around main scene
  strokeWeight(3); 
  line(0, 0+1, width, 0+1); 
  line(0, height-2, width, height-2);
  line(0+1, 0, 0+1, height);
  line(width-2, 0, width-2, height);
  textSize(64); // title
  textAlign(CENTER);
  strokeWeight(1); 
  text("Pong", width/2, 70); 
  // buttons for start / how to play
  fill(175);
  stroke(0);
  rectMode(CENTER);
  rect(width/2, 130, 160, 50);
  rect(width/2, 210, 200, 50);
  // text for buttons
  fill(0);
  textSize(32);
  text("Start", width/2, 140);
  text("How to play", width/2, 220);
}

void gameScene() {
  background(255); 
  // line to divide the two sides
  strokeWeight(3);
  line(width/2, 0, width/2, height);
  fill(0);
  textSize(20);
  textAlign(CENTER);
  text("User Score:", 78, 25); // text for the scores
  text("CPU Score:", 493, 25);

  text(userScore, 147, 25); // text for the numbers, currently set at “0”, but will update later
  text(cpuScore, 558, 25);

  ball.move(); // move the ball
  ball.display(); // display the ball 
  ball.checkEdges(); // check if the ball is reaching the top or bottom so it bounces off rather than going off the screen

  userP.display(); // display the paddles
  cpuP.display();

  userP.moveY(mouseY); // move the user’s paddle vertically with the mouse’s y position 

  ball.isUserPHit(userP); // detect if either paddle comes in contact with the ball
  ball.isCPUPHit(cpuP);

  cpuP.cpuAI(ball); // initiate computer AI for the computer paddle
  scorelimit(ball); // check to see if either player reaches the score limit(10)
}

void helpScene() {
  rectMode(CENTER);
  background(255); // set background to white
  fill(175); // grey fill
  strokeWeight(1);
  stroke(0);
  rect(525, 35, 80, 30); // back button
  textSize(16);
  textAlign(CENTER);
  fill(0);
  text("BACK", 525, 40);

  fill(0); // border around edges
  strokeWeight(3); 
  line(0, 0+1, width, 0+1); 
  line(0, height-2, width, height-2);
  line(0+1, 0, 0+1, height);
  line(width-2, 0, width-2, height);

  textAlign(CENTER); // title
  textSize(48); 
  text("How to Play", width/2, 50); 
  textAlign(LEFT); 
  textSize(10); 
  // information on game and how to play
  text("- In Pong, you control one paddle and you typically play against another user or a computer controlled paddle.", 15, 90);
  text("- In this version there is a computer controlled paddle that you play against.", 15, 120);
  text("- You control your own paddle on the right side with your mouse's y position, mouse your mouse up/down to move it.", 15, 150);
  text("- Once you or the computer reach the score limit(10), it will end the game.", 15, 180);
  text("- The ball bounces off at different angles based off where it hits the paddle.", 15, 210);
  text("- Higher on paddle, bounces higher off paddle, lower on paddle, bounces lower off paddle.", 15, 240);
  text("- Make sure the ball doesn't go past your paddle, and try to get it by your opponents' paddle.", 15, 270);
  text("- P.S - as of now the computer can't lose so don't try too hard.", 15, 300);
  textSize(30);
  textAlign(CENTER);
  text("* Move the mouse to move the paddle *", width/2, 350);
  disPaddle(); // display the paddle
}

void disPaddle() { // function to display the paddle
  rectMode(CENTER);
  fill(0);
  rect(x, y, w, h);

  y = constrain(mouseY, 0 + h/2, height - h/2); // constrain the paddle so it doesn't go off the screen
}

/* E - void mousePressed() {
 ball2s.add(new Ball2(mouseX, mouseY)); // add balls to arraylist when the mouse is pressed at the mouseX/Y locations
 }
 */
void mouseClicked() { 
  if (state == 0) {
    if (mouseX &gt;= 240 &amp;&amp; mouseX &lt;= 360 &amp;&amp; mouseY &gt;= 105 &amp;&amp; mouseY &lt;= 155) { // if the mouse is pressed within the button "Start" while the state is 0, change startgame to true
      startgame = true;
    } else if (state == 0) { 
      if (mouseX &gt;= 200 &amp;&amp; mouseX &lt;= 400 &amp;&amp; mouseY &gt;= 185 &amp;&amp; mouseY &lt;= 235) { // if the mouse is pressed within the button "How to Play" while the state is 0, change help to true
        help = true;
      }
    }
  }
  if (state == 2) {
    if (mouseX &gt;= 485 &amp;&amp; mouseX &lt;= 565 &amp;&amp; mouseY &gt;= 20 &amp;&amp; mouseY &lt;= 50) { // if the mouse is clicked within the button "BACK" while the state is 2, change back to true
      back = true;
    }
  }
}

void keyPressed() {
  if (state == 1) {
    if (key == ENTER || key == RETURN) {
      endofgame = true;
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Array of objects &amp; functions</title>
      <link>https://forum.processing.org/two/discussion/22783/array-of-objects-functions</link>
      <pubDate>Sat, 27 May 2017 12:45:52 +0000</pubDate>
      <dc:creator>Alivanar</dc:creator>
      <guid isPermaLink="false">22783@/two/discussions</guid>
      <description><![CDATA[<p>Hi there, 
first of, english is not my native language so i apoligize if i' making some mistakes,
then, in a minigame that i am building w/ the p5.js library, i want to use this array :</p>

<pre lang="javascript">
 bonuses_array = [
   { name: "Paddle_size +", type: "Bonus", status: false, effect: addPSize(), duration: 30, graphic: Paddle_size_plus()},
   { name: "Paddle_speed +", type: "Bonus", status: false, effect: addPSpeed(), duration: 30, graphic: Paddle_speed_plus()},
   { name: "Ball_speed -", type: "Bonus", status: false, effect: reduceBSpeed(), duration: 30, graphic: Ball_speed_minus()},
   { name: "Paddle_size -", type: "Malus", status: false, effect: reducePSize(), duration: 30, graphic: Paddle_size_minus()},
   { name: "Paddle_speed -", type: "Malus", status: false, effect: reducePSpeed(), duration: 30,graphic: Paddle_speed_minus()},
   { name: "Ball_speed +", type: "Malus", status: false, effect: addBSpeed(), duration: 30, graphic: Ball_speed_plus()},
   { name: "Ball_size -", type: "Malus", status: false, effect: reduceBSize(), duration: 30, graphic: Ball_size_minus()},
  ]</pre>

<p>All the functions are defined somewhere else, and the thing i want to achieve is to be able to call bonuses_array[value].graphic(x, y). But it seems that the functions are not defined the right way... Can someone help me ? 
Thanks !!
Ali</p>
]]></description>
   </item>
   <item>
      <title>I'm trying to make a Pong clone, but the ball bounces off the wall anyways even if there's no pad</title>
      <link>https://forum.processing.org/two/discussion/22746/i-m-trying-to-make-a-pong-clone-but-the-ball-bounces-off-the-wall-anyways-even-if-there-s-no-pad</link>
      <pubDate>Wed, 24 May 2017 19:04:56 +0000</pubDate>
      <dc:creator>junkpot</dc:creator>
      <guid isPermaLink="false">22746@/two/discussions</guid>
      <description><![CDATA[<p><code>right rpad = new right();
ball b = new ball();
int pos;
int ballY,ballX;
void setup() {
  size(800,600);  
}
void draw() {
  background(0,0,0);
  rpad.display(pos);
  b.move(ballY); 
  if ((ballY&lt;pos+40) &amp;&amp; (ballY&gt;pos-40)) {
   b.bounce(); 
  }
class ball {
  int x = width/2;
  int y = height/2;
  int velx = 4;
  int vely = 4;
  int locy;
    void move(int locy) {
    locy = y;
    x+=velx;
    y+=vely;
    fill(255);
    ellipse(x,y,15,15);
    if (y&gt;600 || y&lt;0) {
      vely=vely*-1;      
    }
    if (x&lt;10) { 
     velx*=-1; 
    }
  }  
    void bounce() {
    if (x &gt; 790) {
      velx*=-1;
    }
}
}
class right {
int x = 790;  
int y = 300; 
  void display(int posY) {
    rectMode(mouseY);   
    rect(x,mouseY+40,x+10,mouseY-40);
    posY = mouseY;
  }
}
}</code></p>
]]></description>
   </item>
   <item>
      <title>I made a pong game, but It doesn't work!!!</title>
      <link>https://forum.processing.org/two/discussion/22031/i-made-a-pong-game-but-it-doesn-t-work</link>
      <pubDate>Sun, 16 Apr 2017 12:39:37 +0000</pubDate>
      <dc:creator>geun</dc:creator>
      <guid isPermaLink="false">22031@/two/discussions</guid>
      <description><![CDATA[<p>int playerscore=0;
int aiscore=0;
float ballspeed = 3;
float aipaddleSpeed=5;
Paddle paddle;
Paddle aipaddle;
Ball ball;
boolean keypress = false;
boolean endgame = false;
PImage background;
PImage headball;</p>

<p>void setup () {
  size(600, 400);
  smooth();
  aipaddle = new Paddle (20, 200);
  paddle = new Paddle (580, 200);
  ball = new Ball();
}</p>

<p>void draw() {
  //back ground
  background=loadImage("bg1");</p>

<p>//show score
  fill(0,0,255);
  textSize(60);
  text(playerscore,width/2+23,50);
  fill(255,0,0);
  text(aiscore,width/2-47,50);</p>

<p>//displays
  paddle.display();
  aipaddle.display();
  ball.display();
  if(endgame){
    textSize(25);
    fill(255);
    text("Press Enter Key", width/2+width/4-80, height/2+135);
    text("To Start Again", width/2+width/4-80, height/2+160);
    textSize(50);
    text("End\nof\nGame",width/2-width/4+200,height/2-80);
    whowins();
  }
  else{
    if(keypress==true){
      ball.move(ballspeed);
      paddle.moveY(mouseY);
      aipaddle.aiAi(ball);
      aipaddle.intersectRight(ball);</p>

<pre><code>  paddle.intersectLeft(ball);
  ball.checkGoal();
}
else{
  textSize(20);
  fill(255);
  if(aiscore&gt;=lastlevel || playerscore&gt;=lastlevel){
    endgame=true;
  }
  else{
     text("Press Enter Key", width/2+width/4-80, height/2+25);
     text("To Start", width/2+width/4-40, height/2+50);
  }
}
</code></pre>

<p>}
}</p>

<p>void whowins(){
  if (aiscore &gt;= lastlevel || playerscore &gt;= lastlevel) {
    textSize(30);</p>

<pre><code>if (aiscore &gt; playerscore) {
  fill(0, 255, 0, 180);
  text("AI Wins", width/4-80, 50); 
  fill(255, 0, 0, 180);
  text("You Lose", width/2+80, 50);
} 
else {
  fill(255, 0, 0, 180);
  text("AI Lose", width/4-80, 50); 
  fill(0, 255, 0, 180);
  text("You Wins", width/2+80, 50);
}
</code></pre>

<p>}
}</p>

<p>//start &amp; pause
void keyPressed() {
  ballspeed = 5;
  if ( key == ENTER) {
    if (endgame) {
      endgame = false;
      aiscore = 0;
      playerscore = 0;
    } 
    else {
      keypress = !keypress;
    }
  }
}</p>

<hr />

<p>class Ball {
  int x = width/2;
  int y = height/2;
  int r = 7;  //radius
  float speed;
  int directionX = 1;
  int directionY = 1;
  Ball() {
  }</p>

<p>void display() {
    loadImage("headball.png");</p>

<pre><code>}
</code></pre>

<p>void move(float tspeed) {
    speed = tspeed;
    x += speed * directionX;
    y += speed * directionY;
    //boundries
    if ( y &lt; r) {
      y = r;
      directionY = -directionY;
    } 
    else if ( y &gt; height-r) {
      y = height-r;
      directionY = -directionY;
    }
  }</p>

<p>void checkGoal() {
    if ( x &gt; width+r) {
      x = width/2;
      y = height/2;
      keypress = false;
      aiscore = aiscore+1;
      directionX = -directionX;
      directionY = -directionY;
      println("Score for AI!");
    } 
    else if (x &lt; -r) {
      x = width/2;
      y = height/2;
      keypress = false;
      playerscore = playerscore+1;
      directionX = -directionX;
      directionY = -directionY;
      println("Score for Player");
    }
  }</p>

<h2>}</h2>

<p>class Paddle{
  int x;
  int y;
  int w= 20;
  int h= 80;
  Paddle(int tx, int ty){
    x = tx;
    y = ty;
  }</p>

<p>void display(){
    rectMode(CENTER);
    loadImage("head.png");
  }</p>

<p>void moveY(int ty){
    y= ty;
    if(y&gt;height-h/2){
      y= height-h/2;
    }
    else if (y&lt;0+h/2){
      y=h/2;
    }
  }</p>

<p>void aiAi(Ball b) {
    if (b.x &lt;= width/2) {
      if (y &lt; p.y-p.r<em>2) {
        y +=aipaddleSpeed;
      } 
      else if (y &gt; b.y +b.r</em>2) {
        y -= aipaddleSpeed;
      }
    } 
    else {
      if (y &lt; height/2) {
        y+=aipaddleSpeed;
        ;
      } 
      if ( y &gt; height/2) {
        y-=aipaddleSpeed;
      }
    }
  }</p>

<p>void intersectLeft(ball b) {
    if (b.x &gt; x-w/2 &amp;&amp; (b.y &gt; y - h/2 &amp;&amp; b.y &lt; y + h/2)) {
      b.directionX = -p.directionX;
      ballspeed+=0.3;
      println(ballspeed);
    }
  }</p>

<p>void intersectRight(ball b) {
    if (b.x &lt; x+w/2 &amp;&amp; (b.y &gt; y - h/2 &amp;&amp; b.y &lt; y + h/2)) {
      b.directionX = -p.directionX;
      ballspeed+=0.3;
      println(ballspeed);
    }
  }
}</p>

<p>//why,,, I already insert images also in data folder.</p>
]]></description>
   </item>
   <item>
      <title>Variable recuperation through different methods</title>
      <link>https://forum.processing.org/two/discussion/21389/variable-recuperation-through-different-methods</link>
      <pubDate>Tue, 14 Mar 2017 08:36:39 +0000</pubDate>
      <dc:creator>Chipolata123</dc:creator>
      <guid isPermaLink="false">21389@/two/discussions</guid>
      <description><![CDATA[<p>Hello there,
I have a problem for my Pong game :
I have the draw() loop which contains short methods call. 
Two paddle objects and a ball object.
I can't recuperate the position (x;y) of my ball object to tell it to bounce on the paddle or 
mark a point .
The two paddle are actually coded the same way
My question is :</p>

<p>How to objects can communicate between them to get the coordinates : ( incX and incY which determinate the position of the ball in the window.)
The idea is to put</p>

<p>The code is right down :</p>

<p>````'''`void draw(){
  //Extras pour la ligne du milieu et la couleur de fond;
  background(0);
  strokeWeight(2);
  stroke(255);
  line(0, 200, 400, 200); 
  //
  ballePong();
  raquette_bas();
  raquette_haut();
}</p>

<p>void raquette_haut(){
  //Raquette du balle_haut
  pushMatrix();
  translate(r2x, 0);
  rect(170,10,60,13);
  fill(#F5310F);
  popMatrix();
  if(a == true &amp;&amp; r2x&gt;=-168){r2x -= 2;} else if(r == true &amp;&amp; r2x&lt;=168){r2x += 2; } else {r2x +=0;}
}</p>

<p>void raquette_bas(){
  pushMatrix();
  translate(r1x, 0);
  noStroke();
  rect(170, 375, 60, 13);
  fill(#1BDBD9);
  popMatrix();
  if(left==true &amp;&amp; r1x&gt;=-168){r1x -= 2;} else if(right==true &amp;&amp; r1x&lt;=168){r1x += 2;} else {r1x +=0;}
}</p>

<p>void ballePong(){
  pushMatrix();
  translate(incX, incY);
  ellipse(200,200,10,10);
  popMatrix();</p>

<p>//bounce right
  if(incX &gt;= 195){Direction=PI-Direction;}
  //bouce left
  if(incX &lt;= -195){Direction=PI-Direction;}</p>

<p>//bounce down
  if(incY &gt;= 195){Direction=-Direction;}
  //bounce up
  if(incY &lt;= -195){Direction=-Direction;}
}````'''```</p>

<p>Its a part of my source code.
I want ballePong() to know r1x and r2x which are the abcisses of my paddles...</p>

<p>Thanks again</p>
]]></description>
   </item>
   <item>
      <title>How can i make my the left sided paddle to move like the right sided paddle?</title>
      <link>https://forum.processing.org/two/discussion/21080/how-can-i-make-my-the-left-sided-paddle-to-move-like-the-right-sided-paddle</link>
      <pubDate>Wed, 01 Mar 2017 15:10:17 +0000</pubDate>
      <dc:creator>kliu6151</dc:creator>
      <guid isPermaLink="false">21080@/two/discussions</guid>
      <description><![CDATA[<p>my w and  s keys aren't moving like my up and down keys. I want the left sided paddle to move up and down and i expected to be the same as the up and down codes. I recently started processing and I'm genuinely confused. Can someone help me please?</p>

<pre><code>float pongX = 10;
float pongY = 10;
float pongR = 5;
float dX = random(1, 2);
float dY = random(1, 2);
float paddleX;
float paddleY = 200;
float paddleW = 10;
float paddleH = 30;
float paddleXA;
float paddleYA = 200;
float paddleWA = 10;
float paddleHA = 30;

void reset() {
  pongX = 10;
  pongY = 10;
  pongR = 5;
  dX = random(1, 2);
  dY = random(1, 2);
  paddleX = width - 15;
  paddleY = 10;
  paddleW = 10;
  paddleH = 30; }

void setup() {
  size(500, 500);
  paddleX = width - 15;
  paddleXA= width - 495;

}

void draw() {
  background(0);
  ellipse(pongX, pongY, 2 * pongR, 2 * pongR);

  rect(paddleX, paddleY, paddleW, paddleH);
  rect(paddleXA, paddleYA, paddleWA, paddleHA);

  if (pongRight() &gt; width) {
    fill(255);
    rect(0, 0, width, height);
    noLoop();
  }

  if (collision()) {
    dX = -dX; // if dX == 2, it becomes -2; if dX is -2, it becomes 2
  }

  if (pongLeft() &lt; 0) {
    dX = -dX; // if dX == 2, it becomes -2; if dX is -2, it becomes 2
  }

  if (pongBottom() &gt; height) {
    dY = -dY; // if dY == 2, it becomes -2; if dY is -2, it becomes 2
  }

  if (pongTop() &lt; 0) {
    dY = -dY; // if dY == 2, it becomes -2; if dY is -2, it becomes 2
  }

  pongX = pongX + dX;
  pongY = pongY + dY;
}

boolean collision() {
  boolean returnValue = false; // assume there is no collision
  if ((pongRight() &gt;= paddleX) &amp;&amp; (pongLeft() &lt;= paddleX + paddleW)) {
    if ((pongBottom() &gt;= paddleY) &amp;&amp; (pongTop() &lt;= paddleY + paddleH)) {
      returnValue = true;
    }
  }
  return returnValue;
}

float pongLeft() {
  return pongX - pongR;
}

float pongRight() {
  return pongX + pongR;
}

float pongTop() {
  return pongY - pongR;
}

float pongBottom() {
  return pongY + pongR;
}

void keyPressed() {
if(keyPressed == true) {
    if (key == 'r' || key == 'R') {
    reset();
  }
    if (key == CODED) {
      if(keyCode == UP) {
        if (paddleY &gt;= 0) {
          paddleY = paddleY - paddleH*0.2;
        }
      }
      if(keyCode == DOWN) {
        if(paddleY &lt;= height - paddleH) {
          paddleY = paddleY + paddleH*0.2;
        }
      }
        if(keyCode == 'w') {
        if (paddleYA &gt;= 0)   {
          paddleYA = paddleYA - paddleHA*0.2;
        }
      }
      if(keyCode == 's') {
        if(paddleYA &lt;= height - paddleHA) {
          paddleYA = paddleYA + paddleHA*0.2;
        }
      } 
}
}
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Detecting Collision between Rectangle and Circle</title>
      <link>https://forum.processing.org/two/discussion/20769/detecting-collision-between-rectangle-and-circle</link>
      <pubDate>Sun, 12 Feb 2017 23:21:43 +0000</pubDate>
      <dc:creator>pyan83</dc:creator>
      <guid isPermaLink="false">20769@/two/discussions</guid>
      <description><![CDATA[<p>Hi all,</p>

<p>I'm trying to make Pong using Object Orientated Programming as an exercise.</p>

<p>Right now I've gotten to the point of trying to get the ball to bounce off the paddle if they intersect.</p>

<p>I'm pretty sure my logic is correct? If the current distance of the middle of rectangle and the middle of circle is less then middle of circle + middle of rectangle, then it is colliding. I separate these into x and y because the rectangle has 2 different length sides for x and y. If either x OR y is true, true.</p>

<p>Note I've kept in some commented stuff to show some other attempts... dunno if I should be using "this.other" type stuff or should I have an update function?</p>

<p>I've tried writing that out as an if statement but my current distance number seems stagnant, when it should be constantly changing. I'm very new just started learning this year, so I might need very basic laymen's terms! :P</p>

<p>Here's my code so far, I have seperated it into sketch.js and objects.js</p>

<pre><code>var p1;
var xspeed = 3;
var yspeed = 3;

function setup() {
    createCanvas(400, 400);
    p1 = new player1();
    b = new ball();
}

function draw() {
    background(51);
    p1.show();
    p1.move();
    b.show();
    b.move();
    b.edges();
    console.log(b.currentX, b.currentY);
    if (b.hit(p1)) {
        console.log("hit");
        // xspeed = xspeed * -1;
        // yspeed = yspeed * -1;
    }
}

function keyPressed() {
    if (keyCode === 87) {
        p1.setDir(-1);
    } else if (keyCode === 83) {
        p1.setDir(1);
    }
}

function keyReleased() {
    p1.setDir(0);
}

function player1() {
    this.x = 20;
    this.y = height / 2;
    this.w = 12;
    this.h = 64;

    this.ydir = 0;

    this.show = function () {
        fill(255);
        rect(this.x, this.y, this.w, this.h);
    }

    this.setDir = function (dir) {
        this.ydir = dir;
    }

    this.move = function (dir) {
        this.y += this.ydir * 5;
    }
}

function ball() {
    this.x = 100; //Start location of ball
    this.y = 25;
    this.r = 5;
    this.currentX = p1.w / 2 + this.r; //Less than this x number = colliding
    this.currentY = p1.h / 2 + this.r; //Less than this y number = colliding
    this.distX = p1.w / 2 + this.x; //Current x dist of mid of ball and rect
    this.distY = p1.h / 2 + this.y; //Current y dist of mid of ball and rect

    this.show = function () {
        fill(255);
        ellipse(this.x, this.y, this.r * 2);
    }

    this.move = function () {
        this.x = this.x + xspeed;
        this.y = this.y + yspeed;
    }

    this.edges = function () {
        if (this.y &gt; height || this.y &lt; 0) {
            yspeed = yspeed * -1;
        }
        if (this.x &gt; height || this.x &lt; 0) {
            xspeed = xspeed * -1;
        }
    }
    // this.hit = function () {
    //  var dx = dist(this.x.pos, p1.x.pos);
    //  var dy = dist(this.y.pos, p1.y.pos);
    //  if (dx &lt; this.r + p1.x / 2 || dy &lt; this.r + p1.y / 2) {
    //      return true;
    //  } else {
    //      return false;
    //  }
    // }
    this.hit = function (other) {
        if (this.distX &gt; this.currentX || this.distY &gt; this.currentY) {
            //distX, distY is current distance of middle of circle and rect
            //If dist x is more then rect.w/2 + r = true
            //If dist y is more then rect.h/2 + r = true
            return true;
        } else {
            return false;
        }
    }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Problem with my network pong</title>
      <link>https://forum.processing.org/two/discussion/20775/problem-with-my-network-pong</link>
      <pubDate>Mon, 13 Feb 2017 14:34:07 +0000</pubDate>
      <dc:creator>corentianito</dc:creator>
      <guid isPermaLink="false">20775@/two/discussions</guid>
      <description><![CDATA[<p>Hello everyone,</p>

<p>I want to make a pong in network but I can't understand why it doesn't work ? My architecture is : I will send the position of the bot paddle and the ball by the server to the client like this the client would display everything. And the client have to send the position of the top paddle like that the server could display everything.
Sorry if my english is bad, i'm not an english native speaker.
My program works if i make few modifications to play with two players on the same keyboard.</p>

<pre><code>import processing.net.*;

Server server; 
Client client;
String input;
int data[];

boolean gameover= false, right = false, left = false;
int topscore=0;
int bottomscore=0;
int time;
float changespeed=0;
Paddle bot;
Ball pongball;
Paddle top;

void setup()
{
  server = new Server(this, 12012); // Start a simple server on a port
  frameRate(100);
  noStroke();
  pongball= new Ball();
  bot=new Paddle();
  top=new Paddle();
  top.y=0;
  size(1000, 1000);
}

void keyPressed()
{
  if (keyCode == LEFT)  left = true;
  if (keyCode == RIGHT) right = true;
}

void keyReleased()
{
  if (keyCode == LEFT)     left = false;
  if (keyCode == RIGHT)    right = false;
}

void draw()
{
  drawMyPong();
}

void drawMyPong(){
  if (gameover==false)
  {
    background(0);
    bot.showThePaddle();
    top.showThePaddle();

    if (left==true)  bot.moveLeft();
    if (right==true) bot.moveRight();

    pongball.ballSettingInMotion();
    pongball.bounce();
    pongball.showTheBall();

    if (pongball.positionY&lt;-8)
    {
      gameover=true;
      bottomscore++;
    }
    if (pongball.positionY&gt;1008)
    {
      gameover=true;
      topscore++;
    }
    //send the position of the bot paddle and the position of the ball
    server.write(bot.x + " " + pongball.positionX + " " + pongball.positionY + "\n");

    // Receive data from client, position of the top paddle
    client = server.available();
    if (client != null) {
      input = client.readString(); 
      input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
      data = int(split(input, ' '));  // Split values into an array
      //Draw the ball and the bot paddle
      top.x = data[0];

    }
  }
}

class Paddle
{
  int x, y;

  Paddle()
  {
    x=500;
    y=996;
  }

  void showThePaddle()
  {
    fill(46, 178, 253);
    rect(x, y, 120, 4);
  }

  void moveLeft()
  {
    if (x&gt;=0)  x-=5;
  }
  void moveRight()
  {
    if (x&lt;=880)  x+=5;
  }
}

class Ball
{
  int positionX, positionY;
  boolean up, right;

  Ball()
  {
    positionX=16;
    positionY=484;
    up=true;
    right=true;
  }

  void ballSettingInMotion()
  {
    if (up==true)     positionY=int(positionY-2-changespeed/2);
    else              positionY=int(positionY+2+changespeed/2); //up==false
    if (right==true)  positionX=int(positionX+1+changespeed);
    else              positionX=int(positionX-1-changespeed); //right==false
  }
  void bounce()
  {
    if (get(int(positionX)-8, int(positionY))!=color(0))          right=true;
    if (get(int(positionX)+8, int(positionY))!=color(0))          right=false;
    if (get(int(positionX), int(positionY)-8)==color(46, 178, 253))  up=false;
    if (get(int(positionX), int(positionY)+8)==color(46, 178, 253))
    {
      up=true;
      changespeed+=0.5;
    }
  }

  void showTheBall()
  {
    fill(255, 255, 255);
    ellipse(positionX, positionY, 22, 22);
  }
}
</code></pre>

<p>Here is my Client Program :</p>

<pre><code>import processing.net.*;

Server server; 
Client client;
String input;
int data[];

boolean gameover= false, right = false, left = false;
int topscore=0;
int bottomscore=0;
int time;
float changespeed=0;
Paddle bot;
Ball pongball;
Paddle top;

void setup()
{
  client = new Client(this, "localhost", 12012); // Start a simple server on a port
  frameRate(100);
  noStroke();
  pongball= new Ball();
  bot=new Paddle();
  top=new Paddle();
  top.y=0;
  size(1000, 1000);
}

void keyPressed()
{
  if (keyCode == LEFT)  left = true;
  if (keyCode == RIGHT) right = true;
}

void keyReleased()
{
  if (keyCode == LEFT)     left = false;
  if (keyCode == RIGHT)    right = false;
}

void draw()
{
  drawMyPong();
}

void drawMyPong(){
  if (gameover==false)
  {
    background(0);
    bot.showThePaddle();
    top.showThePaddle();

    if (left==true)  top.moveLeft();
    if (right==true) top.moveRight();

    pongball.ballSettingInMotion();
    pongball.bounce();
    pongball.showTheBall();

    if (pongball.positionY&lt;-8)
    {
      gameover=true;
      bottomscore++;
    }
    if (pongball.positionY&gt;1008)
    {
      gameover=true;
      topscore++;
    }
    //WITHOUT THIS PART, it goes correctly
    //send the position of the bot paddle and the position of the ball
    client.write(top.x + "\n");

    // Receive data from client, position of the top paddle
    client = server.available();
    if (client != null) {
      input = client.readString(); 
      input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
      data = int(split(input, ' '));  // Split values into an array
      //Draw the ball and the bot paddle
      bot.x = data[0];
      pongball.positionX = data[1];
      pongball.positionY = data[2];

    }
  }
}

class Paddle
{
  int x, y;

  Paddle()
  {
    x=500;
    y=996;
  }

  void showThePaddle()
  {
    fill(46, 178, 253);
    rect(x, y, 120, 4);
  }

  void moveLeft()
  {
    if (x&gt;=0)  x-=5;
  }
  void moveRight()
  {
    if (x&lt;=880)  x+=5;
  }
}

class Ball
{
  int positionX, positionY;
  boolean up, right;

  Ball()
  {
    positionX=16;
    positionY=484;
    up=true;
    right=true;
  }

  void ballSettingInMotion()
  {
    if (up==true)     positionY=int(positionY-2-changespeed/2);
    else              positionY=int(positionY+2+changespeed/2); //up==false
    if (right==true)  positionX=int(positionX+1+changespeed);
    else              positionX=int(positionX-1-changespeed); //right==false
  }
  void bounce()
  {
    if (get(int(positionX)-8, int(positionY))!=color(0))          right=true;
    if (get(int(positionX)+8, int(positionY))!=color(0))          right=false;
    if (get(int(positionX), int(positionY)-8)==color(46, 178, 253))  up=false;
    if (get(int(positionX), int(positionY)+8)==color(46, 178, 253))
    {
      up=true;
      changespeed+=0.5;
    }
  }

  void showTheBall()
  {
    fill(255, 255, 255);
    ellipse(positionX, positionY, 22, 22);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>This pong code does not  work</title>
      <link>https://forum.processing.org/two/discussion/17177/this-pong-code-does-not-work</link>
      <pubDate>Fri, 17 Jun 2016 00:23:06 +0000</pubDate>
      <dc:creator>Xela</dc:creator>
      <guid isPermaLink="false">17177@/two/discussions</guid>
      <description><![CDATA[<p>So I'm doing pong for a project we are doing in school in processing, but with my code the player's bars keep changing size when I change location! Can someone help me please? Thanks!</p>

<pre><code>//SINGLE PLAYER PONG
//Made by Alex Yang 06/16/2016

//Directions:
//Try to hit the ball back every time.
//This is a 2-player game, so you can use the UP and DOWN keys,
//or the W and S keys.
//UP or W to move your paddle up, DOWN or S to move your paddle down.

//Initialize Variables
boolean gameStart = false;
float x = 150;
float y = 150;
float speedX = random(3, 5);
float speedY = random(3, 5);
int lpaddleColor = 255;
int rpaddleColor = 255;
int radius;
int rectSize = 150;
int paddleX1 = 30;
int paddleX2 = 770;
int paddleY1 = 400;
int paddleY2 = 400;


void setup() {
  //Make an 800 by 800 pixel screen
  size(800,800);
}

void draw() {
  //Make a black background
  background(0);

  //Divide the screen up into 2 halves by adding a white line
  stroke(255);
  line(400,0,400,800);

  //Make the ball
  fill(255);
  radius = 20;
  ellipseMode(CENTER);
  ellipse(x, y, radius, radius);

  //Make the paddles
  fill(lpaddleColor);
  rect(paddleX1, mouseY-250, paddleX1-20, mouseY-175);
  fill(rpaddleColor);
  rect(paddleX2, paddleY2-325, paddleX2-760, paddleY2-250);


  if (gameStart) {
    //Ball moves diagonally when game starts.
    x = x + speedX;
    y = y + speedY;

    //If ball hits either paddle, have the X direction go the opposite way.
    //Change the color of the paddle when the ball collides with the paddle.
    if (x &gt; width-30 &amp;&amp; x &lt; width -20 &amp;&amp; y &gt; mouseY-rectSize/2 &amp;&amp; y &lt; mouseY+rectSize/2 ) {
      speedX = speedX * -1;
      x = x + speedX;
      rpaddleColor = 0;
      rectSize = rectSize-10;
      rectSize = constrain(rectSize, 10,150);     
    }

    // If ball hits either paddle, change direction of X and have the paddle color flicker gray.
    else if ((x &lt; 40) &amp;&amp; (y &lt; mouseY+75) &amp;&amp; (y &gt; mouseY-75)){
      speedX = speedX * -1.1;
      x = x + speedX;
      lpaddleColor = 128;
    }

    else {    
      lpaddleColor = 255;
      rpaddleColor = 255;
    }
    //Reset game when someone misses the ball
    if ((x &gt; width) || (x &lt; 0)) {
      gameStart = false;
      x = 150;
      y = 150;
      speedX = 4;
      speedY = 4;
      rectSize = 150;
    }


    //If ball hits up or down, change direction of Y  
    if ( y &gt; height || y &lt; 0 ) {
      speedY = speedY * -1;
      y = y + speedY;
    }

    //Control the paddles with the UP, DOWN, W, and S keys.
    if (key == CODED){
      if (keyCode == UP){
        paddleY2 = paddleY2 - 4;
      } else if (keyCode == DOWN){
        paddleY2 = paddleY2 + 4;
      }
    }

    if (key == 'w'){
      paddleY1 = paddleY1 - 4;
    } else if (key == 's'){
      paddleY1 = paddleY1 + 4;
    }
  }
}
void mousePressed() {
  //Start or stop the game whenever the mosue is pressed.
  gameStart = !gameStart;
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Array of Bouncing objects</title>
      <link>https://forum.processing.org/two/discussion/20216/array-of-bouncing-objects</link>
      <pubDate>Thu, 12 Jan 2017 16:31:19 +0000</pubDate>
      <dc:creator>wjsandbe</dc:creator>
      <guid isPermaLink="false">20216@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to make a simple version of pong. My code works when I simply draw a single ellipse and code it to bounce off of the Paddle (rectangle), but when I create an array of balls (ellipses) the balls no longer bounce off of the paddle. I'm not sure where I'm going wrong, I thought I followed the P5.js intersecting objects tutorial pretty well. Again I want any Ball from the array to bounce off of the Paddle, if the objects intersect.</p>

<pre><code>var gravity = 0.1;
var balls = [];
var n = 0;


function setup() {
  createCanvas(400, 400);
  Paddle = new Paddle(180, height / 4 * 3);
  for (n = 0; n &lt; 7; n++) {
    append(balls, new Ball(random(width), random(height)));
  }
}

function draw() {
  background(51);
  Paddle.display();
  Paddle.borders();

  for (n = 0; n &lt; balls.length; n++) {
    balls[n].display();
    balls[n].move();
  }

for (i = 0; i &lt; balls.length; i++){
 if (balls[i].intersects(Paddle)) {
   balls[n].bounce();
 } 
}

}

function mouseClicked() {
  if (n &lt; 9 &amp;&amp; mouseY &lt; height - 50) {
    append(balls, new Ball(mouseX, mouseY));
  }
}

function keyPressed() {
  if (keyCode === RIGHT_ARROW) {
    Paddle.move(1);
  } else if (keyCode === LEFT_ARROW) {
    Paddle.move(-1);
  }
}

function Paddle(x, y) {
  this.x = x;
  this.y = y;
  this.r = 25;

  this.move = function(dir) {
    this.x += dir * 15;
  }
  this.borders = function() {
    if (this.x &gt; width)
      this.x = 0;
    if (this.x &lt; 0)
      this.x = width;
  }
  this.display = function() {
    stroke(0, 255, 0);
    fill(0, 255, 0);
    rectMode(CENTER);
    rect(this.x, this.y, this.r * 4, this.r * 2)
  }
}

function Ball(x, y) {
  this.x = x;
  this.y = y;
  this.r = 25;
  this.yspeed = 0.1;
  //  this.toBounce = false;

  this.move = function() {
    this.y += this.yspeed;
    this.yspeed += gravity;

    if (this.y &gt; height) {
      this.y = height;
      this.yspeed *= -1
    }
    if (this.y &lt; 0) {
      this.y = 0;
      this.yspeed *= -1
    }
  }
    this.intersects = function(Paddle) {
    var d = dist(Ball.x, Ball.y, Paddle.x, Paddle.y);
    if (d &lt; Ball.r + Paddle.y) {
      return true;
    } else {
      return false;
    }
  }
  this.bounce = function() {
    this.y = this.y *= -1
    this.yspeed *= -1;
  }

  this.display = function() {
    stroke(255)
    fill(255, 0, 0);
    ellipse(this.x, this.y, this.r * 2, this.r * 2);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Urgent, please! I want to create a startscreen for my pong game. What am I doing wrong ?</title>
      <link>https://forum.processing.org/two/discussion/20024/urgent-please-i-want-to-create-a-startscreen-for-my-pong-game-what-am-i-doing-wrong</link>
      <pubDate>Sun, 01 Jan 2017 22:32:01 +0000</pubDate>
      <dc:creator>supsup</dc:creator>
      <guid isPermaLink="false">20024@/two/discussions</guid>
      <description><![CDATA[<p>PImage startscreen;
int startscreen;
int stage;
int base=20;
int x,y;
int hit = 0;
int miss = 0;
int changeX=-5;
int changeY=-5;
int gameOver=0;</p>

<p>void setup(){
  stage = 1;
  startscreen = LoadImage("2teniscourt.jpg");
  img(startscreen,0,0,600,600);
  size(600, 600);
  x=(int)random(width);
  y=height-base;
}
void draw()
{
  if(stage==1){
   image(startscreen,600,600);
   textAlign(CENTER);
   textSize(45);
   fill(66, 104, 244);
   text("PONG GAME",100,170);
   text("Press any key to start",100,170);
   if (keyPressed == true){
      stage = 2;
    }
}<br />
    if (stage == 2){
      background(100,200,100);
    }
  if(gameOver==0)
  {
  background(0); 
  fill(244, 66, 66);
  text("hit: " + hit,50,40);
  fill(66, 104, 244);
  rect(mouseX,height-base,150,base);
  ellipse(x,y,40,40);
  x=x+changeX;
  y=y+changeY;
  if(x&lt;0 | x&gt;width)
  {
    changeX=-changeX;
  }
  if(y&lt;0)
  {
    changeY=-changeY;
  }
  if(y&gt;height-base)
  {
    //check whether it is falling inside the rectangle or not
    if(x&gt;mouseX &amp;&amp; x&lt;mouseX+200)
    {
      changeY=-changeY; //bounce back
      hit +=1;
    }
    else
    {
      gameOverSplash();
    }
  }
  }
  else
  {
    background(244, 78, 66);
    fill(66, 104, 244);
    text("Game Over!",width/2,height/2);
    text("CLICK MOUSE TO RESTART",width/2,height/2+20);
  }
}
void gameOverSplash()<br />
{
  gameOver=1;
}
void mouseClicked()
{
  changeY=-changeY;
  hit +=1;
  gameOver=0;
}</p>
]]></description>
   </item>
   <item>
      <title>Urgent,pleas! I want to add a startscreen into my pong game. Why isn't it working? Thank you.</title>
      <link>https://forum.processing.org/two/discussion/20032/urgent-pleas-i-want-to-add-a-startscreen-into-my-pong-game-why-isn-t-it-working-thank-you</link>
      <pubDate>Mon, 02 Jan 2017 11:09:29 +0000</pubDate>
      <dc:creator>supsup</dc:creator>
      <guid isPermaLink="false">20032@/two/discussions</guid>
      <description><![CDATA[<p>PImage startscreen;
int startscreen;
int stage;
int base=20;
int x,y;
int hit = 0;
int miss = 0;
int changeX=-5;
int changeY=-5;
int gameOver=0;</p>

<p>void setup(){
  stage = 1;
  startscreen = LoadImage("2teniscourt.jpg");
  img(startscreen,0,0,600,600);
  size(600, 600);
  x=(int)random(width);
  y=height-base;
}
void draw()
{
  if(stage==1){
   image(startscreen,600,600);
   textAlign(CENTER);
   textSize(45);
   fill(66, 104, 244);
   text("PONG GAME",100,170);
   text("Press any key to start",100,170);
   if (keyPressed == true){
      stage = 2;
    }
}<br />
    if (stage == 2){
      background(100,200,100);
    }
  if(gameOver==0)
  {
  background(0); 
  fill(244, 66, 66);
  text("hit: " + hit,50,40);
  fill(66, 104, 244);
  rect(mouseX,height-base,150,base);
  ellipse(x,y,40,40);
  x=x+changeX;
  y=y+changeY;
  if(x&lt;0 | x&gt;width)
  {
    changeX=-changeX;
  }
  if(y&lt;0)
  {
    changeY=-changeY;
  }
if(y&gt;height-base)
  {
    //check if it is falling inside the screen or not
    if(x&gt;mouseX &amp;&amp; x&lt;mouseX+200)
    {
      changeY=-changeY; //bounce back
      hit +=1;
    }
    else
    {
      gameOverSplash();
    }
  }
  }
  else
  {
    background(244, 78, 66);
    fill(66, 104, 244);
    text("Game Over!",width/2,height/2);
    text("CLICK MOUSE TO RESTART",width/2,height/2+20);
  }
}
void gameOverSplash()<br />
{
  gameOver=1;
}
void mouseClicked()
{
  changeY=-changeY;
  hit +=1;
  gameOver=0;
}</p>
]]></description>
   </item>
   <item>
      <title>make the balls bounce off the paddles??</title>
      <link>https://forum.processing.org/two/discussion/16720/make-the-balls-bounce-off-the-paddles</link>
      <pubDate>Fri, 20 May 2016 12:43:22 +0000</pubDate>
      <dc:creator>abigail18</dc:creator>
      <guid isPermaLink="false">16720@/two/discussions</guid>
      <description><![CDATA[<p>`
     // variables for the ball
     int ball_width = 15, ball_height = 15;
     int ballX = width/2, ballY;
     //
     // variables for the paddles
     int paddle_width = 20, paddle_height = 200;
     int paddle1 = 60, paddle2; 
     //
     // direction variables
     int directionX = 15, directionY = 15;
     //
     // variables for the score
     int scorecounter = 0;
     //
     //game states
     boolean playing = false, gameover = false, finalscore = false, score = true;</p>

<pre><code>void setup () {

   size (1900, 1300); // the field game is going to be 1900x1300 px big
   rectMode (CENTER);
   paddle2 = width - 60;
}



void draw () {

   background (0); // black background

   playing ();
   contact ();
   gameover ();
   finalscore();

} 

//  
void playing () {

  if (keyPressed == true) {  // starts the game and makes the restart possible
  playing = true;
  gameover = false;
  ballX = width/2;
  ballY = height/2;

}

if (!playing) { // playing = false

 fill(255); 
 textSize(80); 
 textAlign(CENTER); 
 text("Press Space to Play", width/2, height/4);

 fill (255); 
 ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
 fill (255, 10, 20); 
 rect(paddle1, (height/2), paddle_width, paddle_height);  // red pong
 fill (60, 255, 0); 
 rect(paddle2, (height/2), paddle_width, paddle_height);  // green pong
}

if (playing) { // playing = true

 score(); // does what i wrote down in void score () -- keeps track of the score

 ballX = ballX + directionX;
 ballY = ballY + directionY; // gives the directions of the ball

 fill (255); 
 ellipse (ballX, ballY, ball_width, ball_height); // ball itself

 fill ( 255, 10, 20 ); 
 rect(paddle1, mouseY, paddle_width, paddle_height); // red pong

 fill ( 60, 255, 0 ); 
 rect(paddle2, mouseY, paddle_width, paddle_height);  // green pong

 if ( ballY &gt; height ) { 
   directionY = -directionY;
 } // if the ball reaches the lower wall it will bounce off

 if ( ballY &lt; 0 ) { 
   directionY = -directionY;
 } // if the ball reaches the upper wall it will bounce off

 if ( ballX &gt; width || ballX &lt; 0 ) { 
   gameover = true; } // if the ball reaches one of the bounderies it will be game over
 }
}

void contact () {

 if (ballX + ball_width/2 &lt; paddle1 + paddle_width/2 || ballX + ball_width/2 &lt; paddle2 - paddle_width/2) {
   directionX = -directionX;
 }

 if (ballY + ball_width/2 &lt; paddle1 + paddle_height/2 || ballY + ball_width/2 &lt; paddle2 + paddle_height/2) {
   directionX = -directionX;
 }

 if (ballY + ball_height/2 &gt; paddle1 + paddle_height/2 || ballY + ball_height/2 &gt; paddle2 + paddle_height/2) { 
   gameover = true;
 }

 score ();
}

void gameover () {

 if (gameover) {
   background (0);
 } // it overrides the scorecounter with a black background

 finalscore (); // gives me the final score + play again option
 score = false;

} 

void score () {

 if (playing) {

 fill(255); 
 textSize(45); 
 textAlign(CENTER); 
 text ( scorecounter, width/2, height/4); // keeps the score on the display while playing

 if (ballX + ball_width/2 &lt; paddle1 + paddle_width/2 ) { 
   scorecounter = scorecounter + 10; 
 }
 if (ballX + ball_width/2 &gt; paddle2 - paddle_width/2) { 
   scorecounter = scorecounter + 10;
 }
 // if the ball touches one paddle the score will be added by 10 points

}
}

void finalscore () {

if (gameover) {

  score = false; // the scorecounter will stop counting

  fill(255); 
  textSize(45); 
  textAlign(CENTER); 
  text("Game Over. Press a key to play again.", width/2, height/4);
  fill(255); 
  textSize(80); 
  textAlign(CENTER); 
  text("You scored " + scorecounter + " points", width/2, (height/4) * 3); // the final score will appear here
} 
}  `
</code></pre>

<p>I need the ball to bounce off only when it hits the paddle, so not as it is doing now - bouncing off when it reaches the y - axis of the paddle, no matter whether the paddle is there not.</p>
]]></description>
   </item>
   <item>
      <title>How do i get the ball to bounce off the movable walls and the game to restart after gameover!?</title>
      <link>https://forum.processing.org/two/discussion/16710/how-do-i-get-the-ball-to-bounce-off-the-movable-walls-and-the-game-to-restart-after-gameover</link>
      <pubDate>Thu, 19 May 2016 19:11:09 +0000</pubDate>
      <dc:creator>abigail18</dc:creator>
      <guid isPermaLink="false">16710@/two/discussions</guid>
      <description><![CDATA[<p>so the code does not perform two things I need him to do. 
First I need the ball to bounce off the little paddles and second I need the game to restart once I reach gameover and press a key. can someone help me with this?! please. an explanation with whatever answer you give would be nice.</p>

<p>thx abigail</p>

<pre><code>// variables for the ball
int ball_width = 15, ball_height = 15;
int ballX = width/2, ballY = height/2;
//
// variables for the paddles
int paddle_width = 20, paddle_height = 150;
int paddle1 = 60, paddle2; 
//
// direction variables
int directionX = 15, directionY = 15;
//
// variables for the score
int scorecounter = 0;
//
//game states
boolean playing = false, gameover = false, finalscore = false, score = true;

void setup () {

 size (1900, 1300); // the field game is going to be 1900x1300 px big
 rectMode (CENTER);
 paddle2 = width - 60;
 }



 void draw () {

  background (0); // black background

  playing ();
  gameover ();
  finalscore();

  }

  // 
  void playing () {

 if (keyPressed == true) { 
   playing = true;
 }

 if (!playing) { // playing = false

   fill(255); 
   textSize(80); 
   textAlign(CENTER); 
   text("Press Space to Play", width/2, height/4);

   fill (255); 
   ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
   fill (255, 10, 20); 
   rect(paddle1, (height/2), paddle_width, paddle_height);  // red pong
   fill (60, 255, 0); 
   rect(paddle2, (height/2), paddle_width, paddle_height);  // green pong
   }

   if (playing) { // playing = true

   score();

   ballX = ballX + directionX; 
   ballY = ballY + directionY;

   fill (255); 
   ellipse (ballX, ballY, ball_width, ball_height); 

   fill ( 255, 10, 20 ); 
   rect(paddle1, mouseY, paddle_width, paddle_height); // red pong
   fill ( 60, 255, 0 ); 
   rect(paddle2, mouseY, paddle_width, paddle_height);  // green pong

   if ( ballY &gt; height ) { 
     directionY = -directionY;
   } // if the ball reaches the lower wall it will bounce off
   if ( ballY &lt; 0 ) { 
     directionY = -directionY;
   }   // if the ball reaches the upper wall it will bounce off

   if ( ballX &gt; width || ballX &lt; 0 ) { 
     gameover = true; }
   }

   if (ballX == paddle1 &amp;&amp; ballY &lt;= paddle_height) { 
     directionX = -directionX;
     directionY = -directionY;
   }
   if (ballX == paddle2 &amp;&amp; ballY &lt;= paddle_height) {
     directionX = -directionX;
     directionY = -directionY;
   }
   }

   void gameover () {

   if (gameover) {
       background (0);
  }

  finalscore ();
  score = false;

  if (keyPressed) { 
  playing = true;
  }
 }

 void score () {

 if (playing) {

  fill(255); 
  textSize(45); 
  textAlign(CENTER); 
  text ( scorecounter, width/2, height/4);

  if (ballX == paddle1 &amp;&amp; ballY &lt;= paddle_height) { 
    scorecounter = scorecounter + 10;
  }
  if (ballX == paddle2 &amp;&amp; ballX &lt;= paddle_height) { 
    scorecounter = scorecounter + 10;
  }
}

if (!playing) {
  score = false;
}  
}

void finalscore () {

  if (gameover) {

    score = false;

    fill(255); 
    textSize(45); 
    textAlign(CENTER); 
    text("Game Over. Press a key to play again.", width/2, height/4);
    fill(255); 
    textSize(80); 
    textAlign(CENTER); 
    text("You scored " + scorecounter + " points", width/2, (height/4) * 3);


     if (keyPressed == true) { 
      background (0);
      playing = !playing;
     }
   }
   }   
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to take user input and reuse it later on</title>
      <link>https://forum.processing.org/two/discussion/16002/how-to-take-user-input-and-reuse-it-later-on</link>
      <pubDate>Wed, 13 Apr 2016 14:44:35 +0000</pubDate>
      <dc:creator>Garrett386</dc:creator>
      <guid isPermaLink="false">16002@/two/discussions</guid>
      <description><![CDATA[<p>I have a project for college in which we must create a 2 player version of a simple pong style game. I want to take the 2 usernames at the start-up screen and reuse them instead of player 1 and 2 below is the code i have for the game so far any help would be greatly appreciated :)</p>

<p>// width of Bat
int base=10;</p>

<p>// setting initial values to 0
int x,y,BatX,Bat2X,Player1=0,Player2=0;</p>

<p>// setting speed – rate of change in X and Y
int changeX=-6;
int changeY=-6;</p>

<p>// setting initial score to 0
int gameOver=0;</p>

<p>// setting game screen size
void setup()
{
  size(760, 640);
  BatX = width/2;
  Bat2X = width/2-100;</p>

<p>x=(int)random(width);
  y=height-base;
}</p>

<p>// start of game
void draw()
{
  if((Player1&lt;5)&amp;&amp;(Player2&lt;5))
  {
  background(0); 
  text("SCORE:"+Player1,width/2,height/2);
  text("SCORE:"+Player2,width/2,height/2.1);</p>

<p>// bat is moved by change in mouseX value – a system variable
if (keyPressed){
   if((key=='r')&amp;&amp;(BatX&lt;width-200)){
      BatX=BatX+5;
    }
    else if((key=='q')&amp;&amp;(BatX&gt;0)){
      BatX=BatX-5;
    }
}
// bat2x is moved by change in mouseX value – a system variable
 if (mousePressed &amp;&amp; (mouseButton == RIGHT)&amp;&amp;(Bat2X&lt;width-200)) {
   Bat2X=Bat2X+5; 
  } else if (mousePressed &amp;&amp; (mouseButton == LEFT)&amp;&amp;(Bat2X&gt;0)) {
    Bat2X=Bat2X-5;
  }</p>

<p>rect(BatX,height-base,200,base);
  rect(Bat2X,0,200,base);</p>

<p>ellipse(x,y,10,10);</p>

<p>// moving ball by changing X and Y values
  x=x+changeX;
  y=y+changeY;</p>

<p>// checking if ball has hit either side, or top, and changing direction if // it has
  if(x&lt;0 | x&gt;width)
  {
    changeX=-changeX;
  }</p>

<p>// checking if Ball has hit Bat i.e. is Y value of Ball at top of Bat
  if(y&gt;height-base)
  {
    //check whether it is falling inside the rectangle of Bat or not
    if(x&gt;BatX &amp;&amp; x&lt;BatX+200)
    {
      changeY=-changeY; //bounce back</p>

<pre><code>}
else
{
  Player2++;
  y=height/2;
  //delay(2000);

}
</code></pre>

<p>}
  // checking if Ball has hit Bat i.e. is Y value of Ball at top of Bat
  if(y&lt;base)
  {
    //check whether it is falling inside the rectangle of Bat or not
    if(x&gt;Bat2X &amp;&amp; x&lt;Bat2X+200)
    {
      changeY=-changeY; //bounce back</p>

<pre><code>}
else
{
  Player1++;
  y=height/2;
  //delay(2000);

}
</code></pre>

<p>}</p>

<p>}
  else
  {
    background(100,100,200);
    text("Game Over!",width/2,height/2);
    text("Press a to restart",width/2,height/2+20);
      text( "Top Player = " + Player2 , width/2, (height/2)+60);
    text( "Bottom Player = " + Player1 , width/2, (height/2)+50);</p>

<p>}
}</p>

<p>void keyPressed()
{if (key=='a'){
  changeY=-changeY;
  Player1=0;
  Player2=0;
  gameOver=0;
}
}</p>
]]></description>
   </item>
   <item>
      <title>Absolute noob, need help with simple pong game.</title>
      <link>https://forum.processing.org/two/discussion/13985/absolute-noob-need-help-with-simple-pong-game</link>
      <pubDate>Tue, 15 Dec 2015 04:29:09 +0000</pubDate>
      <dc:creator>epichvs</dc:creator>
      <guid isPermaLink="false">13985@/two/discussions</guid>
      <description><![CDATA[<p>I am an absolute beginner at coding in any way shape of form. As a 15-year-old with barely any experience thanks to a bare bones high school class, I decided to take it up as a hobby and it really rubs me the right way. It's a hell of a lot of fun and I hope to someday get as talented as the individuals on this forum. :) My question here is, how do I get the ball to bounce off the right side pedal? It's probably some tiny mistake, but I can't seem to find it. I would highly appreciate any help and possibly tips for a nooby. Please excuse my messy code.</p>

<p>Ninja Edit: To begin each round, you click anywhere. Left is controlled with W and A, while Right is using UP and DOWN. It should go up to 7 points, but there's a bug in there where you can continue playing if you click enough times. :P</p>

<pre><code>float XCoor = (325);
float YCoor = (225);
float xspeed = 0;
float yspeed = 0;
float xspeed2 = xspeed;
float yspeed2 = yspeed;
float XCoor2 = 0;
float YCoor2 = 225;
float XCoor3 = 600;
float YCoor3 = 225;
int score1 = 0;
int score2 = 0;
PFont myFont;

void setup(){
  size(650,450);
  myFont = createFont("Courier Regular", 32);
  textFont(myFont);
  textAlign(CENTER);
}

void draw(){
  background(0);
  fill(255);
  ellipse(XCoor, YCoor, 50, 50);
  fill(255);
  rect(XCoor2, YCoor2, 50, 100);
  fill(255);
  rect(XCoor3, YCoor3, 50, 100);
  XCoor += xspeed;
  YCoor += yspeed;

   if(XCoor&gt;=625){
      xspeed*=-1;
   }
   if(YCoor&gt;=425||YCoor&lt;=25){
      yspeed*=-1;
   }
if(XCoor2 + 50 &gt;= XCoor - 25){
  if(YCoor&gt;=YCoor2 &amp;&amp; YCoor &lt;= YCoor2 + 100){
    xspeed *= -1;
  }
if(YCoor2 + 100 &gt;= YCoor - 25){
  if(XCoor &lt;= XCoor2 &amp;&amp; XCoor &gt;= XCoor2 + 100){
    yspeed *= -1;
 }
}
 if(XCoor3 - 50 &gt;= XCoor - 25){
   if(YCoor&gt;=YCoor3 &amp;&amp; YCoor &lt;= YCoor3 - 100){
     xspeed *= -1;
 }
}
if(YCoor3 - 100 &gt;= YCoor - 25){
  if(XCoor&gt;=XCoor3 &amp;&amp; XCoor &lt;= XCoor3 - 100){
    yspeed *= -1;
  }
 }
}
 if(XCoor+25&gt;=650){
     score1++;
     XCoor=325;
     YCoor=225;
     xspeed=0;
     yspeed=0;
   }

   if(XCoor-25&lt;0){
     score2++;
     XCoor=325;
     YCoor=225;
     xspeed=0;
     yspeed=0;
   }
   text(score1,(width/2)-30, height/12);
   text(score2,(width/2)+30, height/12);
   if(score1==7||score2==7){
   fill(255);
   rect(0,0,650,450);
   fill(0);
   text("Game Over!",width/2,200);
   text("Reboot game to play again",width/2,250);
   }
}

void keyPressed(){
 if(key == 'w'){
     YCoor2 -= 10;
  } 
 if(key == 's'){
    YCoor2 += 10;
  }
 if (key == CODED) {
   if (keyCode == UP) {
    YCoor3 -=10;
   }
   if(keyCode ==DOWN){
    YCoor3 +=10;
  }
 }
}

void keyReleased(){
  if(key == 'w'){
    YCoor2 -= 10;
  }
  if(key == 's'){
    YCoor2 += 10;
  }
    if (key == CODED) {
  if (keyCode == UP) {
    YCoor3 -=10;
  }
  if(keyCode ==DOWN){
    YCoor3 +=10;
  }
 }
}

void mousePressed(){
  if(mousePressed){
    xspeed-=3;
  yspeed+=3.1;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Questions about the PONG GAME</title>
      <link>https://forum.processing.org/two/discussion/11428/questions-about-the-pong-game</link>
      <pubDate>Wed, 24 Jun 2015 04:03:41 +0000</pubDate>
      <dc:creator>sapphiros</dc:creator>
      <guid isPermaLink="false">11428@/two/discussions</guid>
      <description><![CDATA[<p>Just want to ask some questions about this PONG GAME.
And here is the conditions:
1.the top paddle is controlled by the mouse, and the bottom is controlled by the keyboard.
2.If the ball passes the top or bottom paddle in the y direction, a point is scored (by console), and a new ball is generated.</p>

<p>Now I was stuck in the how to generate a new ball and how to consider if the ball hit the paddle or paddles missed it.</p>

<p>Here are the questions:
1.How can I make relationship between the coordinate of the center of the paddle to the center of the ball? 
   Should I add more conditions in the if block to change the speed of ball which it hits the paddles? 
2.How can I generate a new ball when the both paddles missed the ball? Should I add a boolean named collision and assign it to true? 
   And if it was false the program will generate a new ball? 
Was my method right or wrong? Could you guys give me some hints?</p>

<pre><code>float ballX = 250;
float ballY = 250;
float ballSize = 20;
float ballSpeedX = 3;
float ballSpeedY = 8;
float ballGenSpeed = 3;

float borderTop = 25;
float borderBottom = 450;
float borderLeft = 0;
float borderRight = 450;

float paddleH = 10;
float paddleW = 100;
float paddleSpeed = 15;
float paddleX;

void setup()
{
  size(500, 500);
  background(0);
}

void draw()
{
  background(0);
  ballX += ballSpeedX;
  ballY += ballSpeedY;

  if (ballX &lt; borderLeft || ballX &gt; borderRight)
  {
    ballSpeedX *= -1;
  }
  if (ballY &lt; borderTop+(paddleH*2) || ballY &gt; borderBottom-paddleH)
  {
    ballSpeedY *= -1;
  }

  ellipse(ballX, ballY, ballSize, ballSize);

  boolean collision = true;



  if (!collision) {
    for (int i = 0; i&lt;20; i++)
    {
      float theta = i/20 * 2 * PI;
      float ballDirectX = cos(theta)*ballGenSpeed;
      float ballDirectY = sin(theta)*ballGenSpeed;
      ellipse(ballDirectX, ballDirectY, ballSize, ballSize);
    }
  }

  rect(mouseX, borderTop, paddleW, paddleH);
  rect(paddleX, borderBottom, paddleW, paddleH);
  if (key == CODED) {
    if (keyCode == LEFT &amp;&amp; keyPressed) {
      paddleX -= paddleSpeed;
    } else if (keyCode == RIGHT &amp;&amp; keyPressed) {
      paddleX += paddleSpeed;
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>2 Player Pong Multitouch Help</title>
      <link>https://forum.processing.org/two/discussion/9965/2-player-pong-multitouch-help</link>
      <pubDate>Sat, 21 Mar 2015 16:19:42 +0000</pubDate>
      <dc:creator>knowyourenemy</dc:creator>
      <guid isPermaLink="false">9965@/two/discussions</guid>
      <description><![CDATA[<p>Hello everyone, i am currently developing a two-player pong game for android. In order to enable multitouch, i used the code i found online:</p>

<pre><code> public boolean surfaceTouchEvent(MotionEvent event) {
                pointerCount = event.getPointerCount();
                if (pointerCount&gt;2){
                  pointerCount = 2;
                }
                //println("Number of pointers: "+pointerCount);
                points.clear();
                for(int i=1; i&lt;=pointerCount; i++) {

                points.add(new PVector(event.getX(i-1), event.getY(i-1)));
                }

                //if the event is a pressed gesture finishing, 
                // it means the lifting the last touch point
                if(event.getActionMasked() == MotionEvent.ACTION_UP) points.clear();

                // if you want the variables for motionX/motionY, mouseX/mouseY etc.
                // to work properly, you'll need to call super.surfaceTouchEvent().
                return super.surfaceTouchEvent(event);

              }      
</code></pre>

<p>Within the void draw(), i did this:</p>

<pre><code>if (speedY&gt;0){
        if (tempPoint.y&gt;height/2){
         //extra code here
          rect (tempPoint.x, height-lineHeight, pongRadius*2, pongHeight*2);

          }  
        if (tempPoint.y&lt;height/2){
          //extra code here
          rect (tempPoint.y, lineHeight, pongRadius*2, pongHeight*2);

        }
      }

      if (speedY&lt;0){
        if (tempPoint.y&lt;height/2){
          //extra code here
          rect (tempPoint.x, lineHeight, pongRadius*2, pongHeight*2);


        }  
        if (tempPoint.y&gt;height/2){
          //extra code here
          rect (tempPoint.x, height-lineHeight, pongRadius*2, pongHeight*2);

        }
      }

     }
</code></pre>

<p>Basically, everything is working fine, and the 2 "pongs" or paddles move fine when they are 2 fingers holding down at both ends of the screen. However, the problem arises when one person decides to let go of the screen at the second person puts 2 fingers on their side of the screen, which results in the second person getting 2 paddles and the 1st person not able to get any. This is because i have set the limit of pointerCount to 2 in the code <code>if (pointerCount&gt;2){
      pointerCount = 2;
    }</code>.</p>

<p>Is it possible for me to have a maximum of 1 paddle (1 touch) in each side of the screen(so there will still be two touches in total), and if so, how?</p>

<p>Thanks in advance.</p>
]]></description>
   </item>
   <item>
      <title>Simple Pong Using Class</title>
      <link>https://forum.processing.org/two/discussion/8663/simple-pong-using-class</link>
      <pubDate>Mon, 15 Dec 2014 00:22:02 +0000</pubDate>
      <dc:creator>winnie3269</dc:creator>
      <guid isPermaLink="false">8663@/two/discussions</guid>
      <description><![CDATA[<p>I need help getting the ball to bounce off the paddles using a get function.</p>

<pre><code>    Paddle left;
    Paddle right;
    int leftPoints=0;
    int rightPoints=0;
    int x, y;
    int Y = 0;
    int p=255;
    //variables for speed of ball
    int xspeed=7;
    int yspeed=7;
    float changeX, changeY;
    float r=0;
    float g=0;
    float b=0;
    boolean keyPressed=false;
    void setup()
    {
      size(800, 600);
      smooth();
      frameRate(60);
      bob=new Ball();
      left=new Paddle();
      right=new Paddle();
    }
    void draw()
    {
      background(0);
      bob.move();
      bob.bounce();
      bob.show();
      left.left();
      left.setY(Y); 
      right.right();
      stroke(#08ECFF);
      line(width/2, 0, width/2, height);
      textSize(40);
      text(leftPoints, 330, 100);
      text(rightPoints, 445, 100);
    }
    void keyPressed() //movement for player
    {

      if (key =='w')
      {
        Y = Y - 15;
      } else if (key=='s')
      {
        Y = Y + 15;
      } else 
      {
        Y=Y;
      }
    }
    class Paddle
    {
      int x, y;
      boolean up, down;
      void left()
      {
        fill(255);
        noStroke();
        rect(15, y, 25, 100);
      }
      void right()
      {
        fill(255);
        noStroke();
        rect(760, mouseY, 25, 100);
      }
      void setY(int newY) 
      {
        y = newY;
      }
    }
    class Ball
    {
      int x, y;
      boolean xd;//right
      boolean yd;//up
      Ball()
      {
        x=400;
        y=100;
        xd = true;
        yd = true;
      }
      void bounce()
      {
        int minY=5;
        int maxY=580;
        {
          if (y&gt;maxY) 
          { 
            yd = false;
          } else if (y&lt;minY) 
          { 
            yd = true;
          }
        }
        if (get(x+21, y)==color(255))
        {
          yspeed*=-1;
          xd=false;
        }

        if (get(x-21, y)==color(255))
        {
          yspeed*=-1;
          xd=false;
        }
      }

      void move()
      {
        if (xd == true)
        {
          x = x + 4;
        } else
        {
          x = x - 4;
        }
        if (yd == true)
        {
          y=y + 4;
        } else
        {
          y = y - 4;
        }
        if (x&gt;=780) 
        { 
          xd = false;
          leftPoints++;
          x=400;
        } 
        if (x&lt;=10) 
        { 
          xd = true;
          rightPoints++;
          x=400;
        }
      }
      void show() //the ball
      {
        stroke(0);
        fill(r, g, b);
        r=random(0, 255);
        g=random(0, 255);
        b=random(0, 255);
        rect(x, y, 20, 20);
      }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Alien Pong Help!</title>
      <link>https://forum.processing.org/two/discussion/8610/alien-pong-help</link>
      <pubDate>Thu, 11 Dec 2014 22:52:50 +0000</pubDate>
      <dc:creator>csull4</dc:creator>
      <guid isPermaLink="false">8610@/two/discussions</guid>
      <description><![CDATA[<p>I am creating a game with Processing 2.0 and I've posted my code below. What I want to have happen is for the asteroids to move independently of each other. When they bounce on the alien's head, I want the one that hit the alien to disappear and for the score to go up one point. When the asteroid disappears, I want one or more asteroids to regenerate and do the same thing. When one is missed, the user loses. I can make the asteroids into a basic shape if that makes it any easier. Can anyone help??</p>

<pre><code>PImage space, alien, asteroid, asteroid2, whirl;
PFont font;
int base = 225; //where the asteroid hits the alien
int x, y, x1, y1, gameScore = 0;
int changeX = -3;
int changeY = -3;
int gameOver = 0;


void setup() {
//background of space image
  space = loadImage("space.jpg");
  size(space.width, space.height); //Dimensions: 1131 x 707

 // whirl = loadImage("whirl.jpg");
 // size(space.width, space.height);

//variables for images  
  x = (int)random(width);
  y = height-base;
  x1 = (int)random(width);
  y1 = height-base; 

//alien image
  alien = loadImage("alien.png"); 
//asteroid images
  asteroid = loadImage("asteroid.png");
  asteroid2 = loadImage("asteroid2.png");
}


void draw() {
  if(gameOver==0) {
  background(space); 

//font and score   
  font = loadFont("Candara-Bold-48.vlw");
  textFont(font);
  fill(255);
  text("score: "+gameScore, width - 200, height - 50);

//alien moving   
  image(alien, mouseX, 520);

//asteroid bouncing  
  image(asteroid, x, y);
  image(asteroid2, x1, y1);

  x = x + changeX;
  y = y + changeY;


  if (x &lt; 0 | x &gt; width) {
    changeX = -changeX;

  }
  if (y &lt; 0) {
    changeY = -changeY;
  }

  if (y &gt; height-base) {
    //check whether it is falling inside space or not
    if (x &gt; mouseX &amp;&amp; x &lt; mouseX + 200) {
      changeY = -changeY; //bounce back
      gameScore++;
    }

    else {
      gameOverSplash();
    }
  }


  x1 = x1 + changeX;
  y1 = y1 + changeY;


  if (x1 &lt; 0 | x1 &gt; width) {
    changeX = -changeX;

  }
  if (y1 &lt; 0) {
    changeY = -changeY;
  }

  if (y1 &gt; height-base) {
    //check whether it is falling inside space or not
    if (x1 &gt; mouseX &amp;&amp; x1 &lt; mouseX + 200) {
      changeY = -changeY; //bounce back
      gameScore++;
    }

    else {
      gameOverSplash();
    }
  }
 }

//game over and restart function
  else {
    background(0);
    text("Game Over!",width/2,height/2);
    text("CLICK TO RESTART",width/2,height/2+50);
  }
}


void gameOverSplash() {
  gameOver=1;
}


void mouseClicked() { //click the black screen or double click the regular screen to restart the game
  changeY=-changeY;
  gameScore=0;
  gameOver=0;
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>how to make an object disappear upon collision</title>
      <link>https://forum.processing.org/two/discussion/8612/how-to-make-an-object-disappear-upon-collision</link>
      <pubDate>Fri, 12 Dec 2014 00:23:43 +0000</pubDate>
      <dc:creator>csull4</dc:creator>
      <guid isPermaLink="false">8612@/two/discussions</guid>
      <description><![CDATA[<p>How do I get the objects (Asteroids) to disappear when they contact the base object (alien) and reappear in another place?</p>

<p>HELP!!</p>

<pre><code>PImage space, alien, asteroid, asteroid2;
PFont font;
int base = 225; //where the asteroid hits the alien
int x, y, x1, y1, gameScore = 0;
int changeX = -3;
int changeY = -3;
int gameOver = 0;


void setup() {
//background of space image
  space = loadImage("space.jpg");
  size(space.width, space.height); //Dimensions: 1131 x 707

 // whirl = loadImage("whirl.jpg");
 // size(space.width, space.height);

//variables for images  
  x = (int)random(width);
  y = height-base;
  x1 = (int)random(width);
  y1 = height-base; 

//alien image
  alien = loadImage("alien.png"); 
//asteroid images
  asteroid = loadImage("asteroid.png");
  asteroid2 = loadImage("asteroid2.png");
}


void draw() {
  if(gameOver==0) {
  background(space); 

//font and score   
  font = loadFont("Candara-Bold-48.vlw");
  textFont(font);
  fill(255);
  text("score: "+gameScore, width - 200, height - 50);

//alien moving   
  image(alien, mouseX, 520);

//asteroid bouncing  
  image(asteroid, x, y);
  image(asteroid2, x1, y1);

  x = x + changeX;
  y = y + changeY;


  if (x &lt; 0 | x &gt; width) {
    changeX = -changeX;

  }
  if (y &lt; 0) {
    changeY = -changeY;
  }

  if (y &gt; height-base) {
    //check whether it is falling inside space or not
    if (x &gt; mouseX &amp;&amp; x &lt; mouseX + 200) {
      changeY = -changeY; //bounce back
      gameScore++;
    }

    else {
      gameOverSplash();
    }
  }


  x1 = x1 + changeX;
  y1 = y1 + changeY;


  if (x1 &lt; 0 | x1 &gt; width) {
    changeX = -changeX;

  }
  if (y1 &lt; 0) {
    changeY = -changeY;
  }

  if (y1 &gt; height-base) {
    //check whether it is falling inside space or not
    if (x1 &gt; mouseX &amp;&amp; x1 &lt; mouseX + 200) {
      changeY = -changeY; //bounce back
      gameScore++;
    }

    else {
      gameOverSplash();
    }
  }
 }

//game over and restart function
  else {
    background(0);
    text("Game Over!",width/2,height/2);
    text("CLICK TO RESTART",width/2,height/2+50);
  }
}


void gameOverSplash() {
  gameOver=1;
}


void mouseClicked() { //click the black screen or double click the regular screen to restart the game
  changeY=-changeY;
  gameScore=0;
  gameOver=0;
</code></pre>
]]></description>
   </item>
   <item>
      <title>Code Question (bouncing objects)</title>
      <link>https://forum.processing.org/two/discussion/1601/code-question-bouncing-objects</link>
      <pubDate>Tue, 26 Nov 2013 14:37:26 +0000</pubDate>
      <dc:creator>wfox</dc:creator>
      <guid isPermaLink="false">1601@/two/discussions</guid>
      <description><![CDATA[<p>What kind of code should I use to make an object bounce around the "box" ?</p>
]]></description>
   </item>
   </channel>
</rss>