0
我開始編寫CS類的pong遊戲。我想有球在場地中央開始關閉,因此我用:我需要更改哪些代碼才能使用代碼?
ellipse (width/2, height/2, 15, 15);
我想打比賽開始後我按空格鍵。爲了做到這一點,我用:
if (keyPressed == true) {ellipse (ballX, ballY, 15, 15); fill (0, 255, 0);}
但它不起作用。有人可以幫我弄清楚我的代碼有什麼問題嗎?請考慮這不是一個JavaScript,而是一個Processing問題。
這是到目前爲止,我整個代碼:
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
}
你需要一個actionListener來做到這一點.. – ryekayo
@Ryekayo這是處理。這已經嵌入到keyPressed方法中。 – DarmaniLink
這是你寫的所有代碼嗎? – FedeWar