用鼠标移动桨(打砖块)
Moving Paddle with mouse (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;
}
}
你已经有了这里的逻辑:
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;
我遇到了一个小问题。我正在使用处理创建一个基本的打砖块,但我不知道如何用鼠标移动桨。对于此示例,我目前已将其设置为点击移动。
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;
}
}
你已经有了这里的逻辑:
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;