我需要更改什么才能使代码正常工作?
What do i need to change to make the code work?
我开始为 CS class 编写这款乒乓球游戏。我想让球从场地中央开始,所以我使用了:
ellipse (width/2, height/2, 15, 15);
我想在按下 space 键后让游戏开始。为此,我使用了:
if (keyPressed == true) {ellipse (ballX, ballY, 15, 15); fill (0, 255, 0);}
然而它不起作用。有人可以帮我弄清楚我的代码有什么问题吗?请考虑这不是 JavaScript 而是处理问题。
到目前为止,这是我的全部代码:
float ballX = 15, ballY = 15, dX = 15, dY = 15; // variables for the ball
float paddleX; // variables for the paddles
int mouseY; // variable to make the pong move with the mouse movement
boolean key, keyPressed;
void setup() {
size (1500,1100); // the field is going to be 1500x110px big
paddleX = width - 40;
ballX = 15; ballY = 15;
}
void draw() {
background(0); // black background
ellipse (width/2, height/2, 15, 15); // this is the starting point of the ball
if (keyPressed == true) { ellipse (ballX, ballY, 15, 15); fill (0, 255, 0); } // the game will only start when a key is pressed
if (ballX > width || ballX < 0) { dX = -dX; } // if the ball reaches the right or left wall it will switch directions
if (ballY > height || ballY < 0) { dY = -dY; }// if the ball reaches the upper or lower wall it will switch directions
ballX = ballX + dX; ballY = ballY + dY; // the ball with move with the speed set as dX and dY
rect(paddleX/58, mouseY, 20, 100); fill (255,10,20); // green pong
rect(paddleX, mouseY, 20, 100); fill (60,255,0); // red pong
}
这个问题的答案和your other question的答案是一样的:你需要把你草图的状态存储在变量中,然后你需要绘制每一帧都基于那个状态,最后你需要改变那些变量来改变你的游戏状态。
这是一个简单的例子,在你按下一个键后只画一个椭圆:
boolean playing = false;
void keyPressed() {
playing = true;
}
void draw() {
background(0);
if (playing) {
ellipse(width/2, height/2, 50, 50);
}
}
在此示例中,playing
变量是我的 state。然后,我在 keyPressed()
函数中更新该状态,并使用该状态来确定我在 draw()
函数中绘制的内容。您将不得不进行一些推断,但是将您的问题分解为一种状态、更改该状态并绘制该状态的过程是您所有问题的答案。
我开始为 CS class 编写这款乒乓球游戏。我想让球从场地中央开始,所以我使用了:
ellipse (width/2, height/2, 15, 15);
我想在按下 space 键后让游戏开始。为此,我使用了:
if (keyPressed == true) {ellipse (ballX, ballY, 15, 15); fill (0, 255, 0);}
然而它不起作用。有人可以帮我弄清楚我的代码有什么问题吗?请考虑这不是 JavaScript 而是处理问题。
到目前为止,这是我的全部代码:
float ballX = 15, ballY = 15, dX = 15, dY = 15; // variables for the ball
float paddleX; // variables for the paddles
int mouseY; // variable to make the pong move with the mouse movement
boolean key, keyPressed;
void setup() {
size (1500,1100); // the field is going to be 1500x110px big
paddleX = width - 40;
ballX = 15; ballY = 15;
}
void draw() {
background(0); // black background
ellipse (width/2, height/2, 15, 15); // this is the starting point of the ball
if (keyPressed == true) { ellipse (ballX, ballY, 15, 15); fill (0, 255, 0); } // the game will only start when a key is pressed
if (ballX > width || ballX < 0) { dX = -dX; } // if the ball reaches the right or left wall it will switch directions
if (ballY > height || ballY < 0) { dY = -dY; }// if the ball reaches the upper or lower wall it will switch directions
ballX = ballX + dX; ballY = ballY + dY; // the ball with move with the speed set as dX and dY
rect(paddleX/58, mouseY, 20, 100); fill (255,10,20); // green pong
rect(paddleX, mouseY, 20, 100); fill (60,255,0); // red pong
}
这个问题的答案和your other question的答案是一样的:你需要把你草图的状态存储在变量中,然后你需要绘制每一帧都基于那个状态,最后你需要改变那些变量来改变你的游戏状态。
这是一个简单的例子,在你按下一个键后只画一个椭圆:
boolean playing = false;
void keyPressed() {
playing = true;
}
void draw() {
background(0);
if (playing) {
ellipse(width/2, height/2, 50, 50);
}
}
在此示例中,playing
变量是我的 state。然后,我在 keyPressed()
函数中更新该状态,并使用该状态来确定我在 draw()
函数中绘制的内容。您将不得不进行一些推断,但是将您的问题分解为一种状态、更改该状态并绘制该状态的过程是您所有问题的答案。