2017-10-10 66 views
0

我學習處理和想提出的乒乓球比賽的修改後的版本在處理3.我有2個球同時而不是僅僅1 此外,一個球加速,而其他球減慢隨着程序運行而下降。處理3.0:加快球的乒乓球不能正確彈跳

我的加速球工作正常,它反彈並提高速度。但是,我放慢球速度並不正確。減速球在非常小的範圍內移動,甚至不會靠近邊界反彈。幫助將不勝感激。 Thanks.`

float ballXPosition; 
float ballYPosition; 
float ballTwoXPos; 
float ballTwoYPos; 
float xDirection; 
float ballTwoXDir; 
float yDirection; 
float ballTwoYDir; 
float radius = 12; 
float xSpeed; 
float ySpeed; 
float ballTwoXSpeed; 
float ballTwoYSpeed; 
float MAX_SPEED = 15; 
float MIN_SPEED = 0.2; 

void setup() 
{ 
    size(600,600); 
    stroke(3); 
    background(255,255,255); 

    ballXPosition = width/2 + random(60); 
    ballTwoXPos= width/2 + random(60); 

    ballYPosition = height/2 + random(60); 
    ballTwoYPos = height/2 + random(60); 

    xDirection = random(1,3); 
    ballTwoXDir = random(1,3); 

    yDirection = random(1,3); 
    ballTwoYDir = random(1,3); 

    xSpeed = MIN_SPEED; 
    ySpeed = MIN_SPEED; 

    ballTwoXSpeed = MAX_SPEED; 
    ballTwoYSpeed = MAX_SPEED; 
} 

void createAcceleratingBall(float xpos, float ypos, float xstretch, float ystretch) 
{ 
    fill(255,0,0); 
    ellipse(xpos, ypos, xstretch, ystretch); 
} 

void createSlowingBall(float xpos, float ypos, float xstretch, float ystretch) 
{ 
    fill(0,0,255); 
    ellipse(xpos, ypos, xstretch, ystretch); 
} 

boolean isSpeedMax(float speed) 
{ 
    return speed > MAX_SPEED; 
} 

boolean isSpeedMin(float speed) 
{ 
    return speed < MIN_SPEED; 
} 

boolean isBallAtXBorder(float xpos) 
{ 
    if(xpos < radius || xpos > width - radius) 
    return true; 
    else 
    return false; 
} 

boolean isBallAtYBorder(float ypos) 
{ 
    if(ypos < radius || ypos > height - radius) 
    return true; 
    else 
    return false; 
} 

void draw() 
{ 
    background(255); 

    ballXPosition = ballXPosition + (xDirection * xSpeed); 
    ballTwoXPos = ballTwoXPos + (ballTwoXDir * ballTwoXSpeed); 

    ballYPosition = ballYPosition + (yDirection * ySpeed); 
    ballTwoYPos = ballTwoYPos + (ballTwoYDir * ballTwoYSpeed); 

    if(!isSpeedMax(xSpeed)) 
    xSpeed *= 1.005; 

    if(!isSpeedMax(ySpeed)) 
    ySpeed *= 1.003; 

    if(!isSpeedMin(ballTwoXSpeed)) 
    ballTwoXSpeed = ballTwoXSpeed/1.005; 

    if(!isSpeedMin(ballTwoYSpeed)) 
    ballTwoYSpeed = ballTwoYSpeed/1.003; 

    if(isBallAtXBorder(ballXPosition)) 
    xDirection *= -1; 

    if(isBallAtYBorder(ballYPosition)) 
    yDirection *= -1; 

    if(isBallAtXBorder(ballTwoXDir)) 
    ballTwoXDir *= -1; 

    if(isBallAtYBorder(ballTwoYDir)) 
    ballTwoYDir *= -1; 

    createAcceleratingBall(ballXPosition, ballYPosition, 2*radius, 2*radius); 
    createSlowingBall(ballTwoXPos, ballTwoYPos, 2.5*radius, 2.5*radius); 
} 

回答

0

我覺得你有被測試在isBallAtXBorderisBallAtYBorder功能放緩球錯變量。您正在測試ballTwoXDirballTwoYDir - 你應該不會被測試ballTwoXPosballTwoYPos