有人能找到我的矩形不跟随鼠标的原因吗?

Can someone find the reason why my rectangles do not follow my mouse?

下面是我正在编写的乒乓球游戏的完整代码,尽管我在 y 坐标中使用了 mouseY

fill ( 255, 10, 20 ); rect(x, mouseY + y, 20, 100); // red pong
fill ( 60, 255, 0 ); rect(paddleX, mouseY + y, 20, 100);  // green pong

它不起作用,我根本找不到我的错误。

我也在为 Score () 部分苦苦挣扎,因为每次我都试图把

if ( ballX = paddleX ) { score = score + 1;}

processing 告诉我无法将 int 转换为 Boolean。有人可以告诉我如何重写我的代码以便不再出现此错误。我对这部分代码的目标是,每次球击中其中一个球拍时,我希望计数器增加 1。

完整代码:

float ballX = 15, ballY = 15, dX = 15, dY = 15; // variables for the ball
float x = 40, y, score = 0;
float paddleX, paddleY = 0; // variables for the paddles
float mouseY, mouseX;
boolean playing = false, gameover = false, finalscore = false, Score = true;  // game states
boolean keyPressed, key;

void setup() {
    size (1500,1100); // the field is going to be 1500x1100px big
    paddleX = width - 40;
}

void keyPressed () { // the game will only start when a key is pressed
    playing = true; 
}

void draw() {
    background(0); // black background

    if (!playing) { // playing = false

        fill(255); textSize(80); textAlign(CENTER); text("Press Space to Play",width/2, height/4);

    fill (255); ellipse (width/2, height/2, 15, 15); // this is the starting point of the ball
    fill (255,10,20); rect(paddleX/58, (height/2)-50, 20, 100);  // red pong
    fill (60,255,0); rect(paddleX, (height/2)-50, 20, 100);  // green pong 

    }  
    if (playing) { // playing = true

        Score();

        fill ( 0, 255, 0 ); ellipse (ballX, ballY, 15, 15); 

        fill ( 255, 10, 20 ); rect(x, mouseY + y, 20, 100); // red pong
        fill ( 60, 255, 0 ); rect(paddleX, mouseY + y, 20, 100);  // green pong

        if ( ballY > height ) { dY = -dY; } // if the ball reaches the lower wall it will bounce off
        if ( ballY < 0 ) { dY = -dY; } // if the ball reaches the upper wall it will bounce off

        ballX = ballX + dX; ballY = ballY + dY;

        if (ballX > width || ballX < 0 ) { gameover = true;}
    } 

    if (gameover) { // gameover = true
        finalscore = true;

        if (keyPressed) { playing = false; }
        else if (playing) { playing = true; }

        if (finalscore) {  // score = true

            fill(255); textSize(45); textAlign(CENTER); text("Game Over. Press a letter to play again.",width/2, height/4);
            fill(255); textSize(80); textAlign(CENTER); text("You scored " + score + " points" ,width/2, (height/4) * 3);

            if (keyPressed) { playing = true;}
        } 
    } 
}

void Score() {

    fill(255); textSize(45); textAlign(CENTER); text(score,width/2, height/4);

    if (playing) {
        if (paddleX <= ballX){ score = score + 1;}
    }
}

您要在草图顶部声明自己的 mouseXmouseY 变量:

float mouseY, mouseX;

不要这样做。 处理会自动为您创建 mouseXmouseY 变量。您不会自己创建它们。去掉这一行,这样你就可以使用 Processing 的 mouseXmouseY 变量,而不是你自己的。

Also I am struggling with the Score () part because everytime I try to put

if ( ballX = paddleX ) { score = score + 1;}

看看那个 if 声明。您只使用了一个 =,这是一个赋值。您需要使用两个等于 ==,或者更好的是像 <=>= 这样的不等式,因为球击中球拍的确切像素值的机会非常低。