【问题标题】:Simple Pong game in Processing处理中的简单乒乓球游戏
【发布时间】:2019-12-18 10:11:15
【问题描述】:

我正在尝试用 Java 创建一个简单的乒乓球游戏进行处理。我还没有完成,一切都很顺利,除了我不能让球从乒乓球拍上反弹。我已经成功地做到了,如果球低于桨,它会反弹回来,但由于某种原因,如果球高于桨,球就会通过。

这是我当前的代码,我当前的问题在void animatePaddles函数的paddleFunctions选项卡中:

主标签:

Ball ball;
Paddle primary, secondary;

void setup()
{
  size(1000, 500);
  background(0);
  smooth();
  frameRate(150);
  ball = new Ball();
  initializePaddles();  // enters x values into primary and secondary paddles
}
void draw()
{
  ball.animateBall();  //uses vectors to add movements to ball, also checks if hitting walls
  animatePaddles();  //adds movements to paddles
}

球类:

class Ball
{
  PVector location, direction;
  final int diameter = height/20;
  Ball()
  {
    location = new PVector(width/2, height/2);
    direction = new PVector(0.5, 0.5);
  }
  void animateBall()  //movement to balls and checking if hitting walls
  {
    background(0);
    fill(255);
    circle(location.x, location.y, diameter);
    location.add(direction);
    if (location.y+diameter/2>= height|| location.y<=diameter/2)
      direction.y*=-1;
  }
}

桨类:

class Paddle  //class for paddles
{
  PVector location;
  PVector direction = new PVector(0, 0);
  final int paddleLength = height/5;
  final int paddleWidth = width/150;
}

paddleFunctions 选项卡:

void initializePaddles()  //enters x values into paddles relative screen size
{
  primary = new Paddle();
  primary.location= new PVector(width*.987, height/2);
  secondary = new Paddle();
  secondary.location = new PVector(width*.007, height/2);
}

void animatePaddles()  //creates the paddles and adds movement
{
  fill(255);
  rect(primary.location.x, primary.location.y, primary.paddleWidth, primary.paddleLength);
  rect(secondary.location.x, secondary.location.y, secondary.paddleWidth, secondary.paddleLength);
  primary.location.add(primary.direction);
  secondary.location.add(secondary.direction);
  if (ball.location.x+ball.diameter/2==primary.location.x-primary.paddleWidth/2 && ball.location.y>=primary.location.y-primary.paddleLength/2 && ball.location.y<=primary.location.y+primary.paddleLength/2)  //THE PROBLEM
    ball.direction.x*=-1;  //^^ **PROBLEM**
}

void keyPressed()  //controls primary paddle
{
  if (key == CODED)
    if (keyCode == UP)
      primary.direction.y=-2;
    else if (keyCode == DOWN)
      primary.direction.y=2;
}

void keyReleased()
{
  primary.direction.y=0;
}

【问题讨论】:

    标签: java processing


    【解决方案1】:

    你的建议是错误的。传递给rect() 的坐标不是矩形的中心,默认是左上角。但是你可以通过rectMode()改变模式:

    rectMode(CENTER);
    rect(primary.location.x, primary.location.y, primary.paddleWidth, primary.paddleLength);
    rect(secondary.location.x, secondary.location.y, secondary.paddleWidth, secondary.paddleLength);
    

    由于您使用浮点数进行操作,因此球永远不会准确地击中球拍。您必须评估球的右侧是否大于桨的左侧:

    if (ball.location.x+ball.diameter/2 >= primary.location.x-+primary.paddleWidth/2 && 
        ball.location.y >= primary.location.y-primary.paddleLength/2 &&
        ball.location.y <= primary.location.y+primary.paddleLength/2) {
    
        ball.direction.x *= -1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多