所以下面我有我的整个代码用于我正在编程的乒乓球游戏,虽然我在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;}
处理告诉我无法将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;}
}
}
答案 0 :(得分:0)
您在草图顶部声明了自己的mouseX
和mouseY
变量:
float mouseY, mouseX;
不要这样做。处理会自动为您创建mouseX
和mouseY
个变量。你不自己创建它们。摆脱这一行,以便您使用Processing的mouseX
和mouseY
变量,而不是您自己的变量。
此外,我正在努力获得Score()部分,因为每次我尝试放
if ( ballX = paddleX ) { score = score + 1;}
查看if
语句。您只使用一个=
,这是一项任务。您需要使用两个等于==
,或者更好的是<=
或>=
之类的不等式,因为球击中球拍的精确像素值的几率非常低。