in the 'Pong" game, the ball doesn't hit and come back with the paddles

edited November 2017 in Questions about Code

Hi, guys

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.

--------------------code----------------------------------------------

/* OpenProcessing Tweak of @http://www.openprocessing.org/sketch/7254@ / / !do not delete the line above, required for linking your tweak if you upload again */

boolean isGameStarted = false; //start variable

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 );

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

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);

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();

} }

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

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

class Ball { float x; float y;

float vx;
float vy;

Ball( int newX, int newY ) {
    x = newX;
    y = newY;
    if ( random(1) < .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 < 0 || y > height ) {
        vy = -vy;
    }

    bounceOnPaddle(  leftPaddle );
    bounceOnPaddle( rightPaddle );
}

void bounceOnPaddle( Paddle paddle ) {


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

}

boolean[] isKeyPressed = new boolean[256]; boolean[] isCodedKeyPressed = new boolean[256];

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

void keyPressed() { handleKeyEvent( true ); }

void keyReleased() { handleKeyEvent( false ); }

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

//range of the paddles move
void moveLeftPlayer() {
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); } }

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

class Paddle { int x; int y;

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
}

}

class Player { int x; int y;

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

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

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

}

Tagged:

Answers

  • Edit post, highlight code, press ctrl-o to format.

  • edited November 2017

    this looks suspicious:

      if (      paddle.x < width / 2 && x == ( paddle.x + 6 )
         || ( paddle.x > width / 2 && x == ( paddle.x - 6 ) ) ) {
    

    when x is the ball I suggest x<=paddle.x + 6

    and x >= paddle.x - 6

    instead of == (in the 2nd part of both lines)

        if (  (   paddle.x < width / 2 && x <= ( paddle.x + 6 ) )
           || ( paddle.x > width / 2 && x >= ( paddle.x - 6 ) ) ) {
    
Sign In or Register to comment.