2017-10-10 61 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