Ich begann dieses Pong-Spiel für die CS-Klasse zu programmieren. Ich will den Ball haben, in der Mitte des Feldes Anfahren so verwendet I:Was muss ich ändern, damit der Code funktioniert?
ellipse (width/2, height/2, 15, 15);
Ich will das Spiel machen beginnen, sobald ich die Leertaste drücken. Um das zu tun, benutzte ich:
if (keyPressed == true) {ellipse (ballX, ballY, 15, 15); fill (0, 255, 0);}
Es funktioniert jedoch nicht. Kann mir bitte jemand helfen, herauszufinden, was mit meinem Code nicht stimmt? Bitte beachten Sie, dass dies keine JavaScript-, sondern eine Verarbeitungsfrage ist.
Hier ist meine gesamte Code so weit:
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
}
Sie brauchen einen actionListener, um das zu tun. – ryekayo
@Ryekayo Dies ist Verarbeitung. Das ist bereits in der keyPressed-Methode eingebaut. – DarmaniLink
Ist das alles der Code, den Sie geschrieben haben? – FedeWar