乒乓运动不顺畅

时间:2012-05-24 20:23:47

标签: processing pong

我想在处理中制作乒乓球游戏。但球的运动不是很顺利。我尝试改变帧速率并降低球速,但随后移动速度似乎不恒定。这可能是性能问题吗?我做错了什么,或者处理可能不是正确的事情,即使是简单的游戏? (我的目标是尝试使用processing.js并制作一个没有插件的游戏)。这是我的代码:

// Colorset
color scrBG;
color bastards;

// Border for the Paddle
int border;

// The Paddle
int padWid;
int padHei;
float padX;
float padY;

// The Ball
float ballX;
float ballY;
int ballRad;

int speed;
float velX;
float velY;

boolean playing = false;



void setup()
{
  border = 5;

  scrBG = #EEEEEE;
  bastards = #FFF0EB;

  padWid = 80;
  padHei = 10;
  ballRad = 15;
  speed = 5;
  velX = velY = speed;

  size(800, 600);
  background(scrBG);
  noStroke();
  noCursor();
  //frameRate(100);
  smooth();
}

void draw()
{

  background(bastards);drawPaddle();
  drawBall();
}

void drawBall()
{

  if(!playing)
  {
    ballX = padX+padWid/2;
    ballY = height-border-padHei-ballRad/2-2;
  }
  else
  {
if(ballY > height)
{
  ballOut();
}

if(ballY > padY && ballX < padX + padWid && ballX > padX)
{
  velY = -velY;
  ballY = padY-ballRad-1;
}
if(ballX < ballRad)
{
  velX = -velX;
  ballX = ballRad+1;
}
if(ballX > width-ballRad)
{
  velX = -velX;
  ballX = width-ballRad-1;
}
if(ballY < ballRad)
{
  velY = -velY;
  ballY = ballRad+1;
}


ballY += velY;
ballX += velX;   
  }

  fill(0);
  ellipse(ballX, ballY, ballRad*2, ballRad*2);

}

void drawPaddle()
{
  padX = constrain(mouseX, border, width-padWid-border);
  padY = height-padHei-border;

  fill(0);
  rect(padX, padY, padWid, padHei);
}

void startLevel()
{
  playing = true;
}

void resetLevel()
{
  playing = false;
}

void ballOut()
{
  playing = false;
}

void mouseClicked()
{
  if(playing)
  {
    resetLevel();
  }
  else
  {
    startLevel();
  }
}

1 个答案:

答案 0 :(得分:1)

你的frameRate似乎对我很高兴。对于像Processing和Flash这样的平滑动画,30帧通常是一个很好的速度。我还将我的所有内容更改为浮点数,因为浮点数更适合平滑动画。

接下来我要考虑的是添加一些缓和,这可能会使其平滑。请在processing.org上查看此信息:

www.processing.org/learning/basics/easing.html

并且有一个游戏。