处理3.0:加速乒乓球不正确弹跳

时间:2017-10-10 03:52:16

标签: processing bounce pong

我正在学习Processing并在处理3中制作pong游戏的修改版本。我有2个球同时而不是1个。 此外,一个球加速,而另一个球在程序运行时减速。

我的加速球工作正常,它会反弹并提高速度。但是,我的减速球不能正常工作。减速球在非常小的区域内移动,甚至不会靠近边界反弹。帮助将不胜感激。 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);
}

1 个答案:

答案 0 :(得分:0)

我认为你在isBallAtXBorderisBallAtYBorder函数中测试了减慢球的错误变量。您正在测试ballTwoXDirballTwoYDir - 您是否应该测试ballTwoXPosballTwoYPos