用鼠标移动桨(打砖块)

时间:2016-04-10 23:57:15

标签: processing

我有一个小问题。我正在使用处理创建一个基本的Arkanoid,但我无法弄清楚如何用我的鼠标移动桨。我现在设置为在此示例中单击一下。

void setup() {

size(400, 400);
rectMode(CENTER);
ellipseMode(CENTER);
}

void draw() {
  background(#EFF8FB);

  batMain();
  ballMain();
}

float ballX = 200, ballY = 200;
float direction = PI * .3f;
byte  speed = 4;

final static byte  ballSize  = 20;
final static color ballColor = #2EFE2E;

void ballMain() {
  // Draw the ball
  fill(ballColor);
  ellipse(ballX, ballY, ballSize, ballSize);


  ballX += speed * cos(direction);
  ballY += speed * sin(direction);
  walls();

  if (direction > TWO_PI)        direction -= TWO_PI;
  else if (direction < -TWO_PI)  direction += TWO_PI;
}

short a = 200;

final static color batColor = #5858FA;

void batMain() {
  // Draw the bat
  fill(batColor);
  rect(a, 380, 60, 10);
  noStroke();

  if (mousePressed) {

    // Right side of the screen
    if (mouseX > width>>1)    a += 4;
    // Other side of the screen
    else                      a -= 4;
  }


  if (ballY + 10 > 375 && ballY + 10 < 385 && ballX + 10 > a - 30 && ballX -   10    < a + 30) {

    if (ballX + 10 > a - 30 && ballX < a - 15) { // Left part
      direction = TWO_PI - direction;
      ++speed;
    }

    else if (ballX - 10 < a + 30 && ballX < a + 15) {
      direction = TWO_PI - direction;
      ++speed;
    } 
     else {
      direction = TWO_PI - direction;
      speed = 4;
    }
  }
}

void walls() {

   if (ballX < 10) {
    ballX = 11;
    direction = PI - direction;
  }
  if (ballX > 390) {
    ballX = 389;
    direction = PI - direction;
  }
  if (ballY < 10) {
    ballY = 11;
    direction = TWO_PI - direction;
  }
  if (ballY > 390) {
    ballY = 389;
    direction = TWO_PI - direction;
  }
}

1 个答案:

答案 0 :(得分:0)

你已经有了逻辑:

if (mousePressed) {

    // Right side of the screen
    if (mouseX > width>>1)    a += 4;
    // Other side of the screen
    else                      a -= 4;
  }

如果您希望即使您没有按下它,也可以使用鼠标跟随鼠标,只需删除那个外部的if语句:

    // Right side of the screen
    if (mouseX > width>>1){    
       a += 4;
    }
    // Other side of the screen
    else{
       a -= 4;
    }

或者如果您希望桨位于鼠标位置,只需直接使用:

a = mouseX;