Boa noite,
Estou com um problema, alguém pode-me dizer qual é o problema com o código, não percebo o porque de não estar a correr.
Será que podem dar-me uma mãozinha.
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ZenPong extends Application {
private DoubleProperty centerX = new SimpleDoubleProperty();
private DoubleProperty centerY = new SimpleDoubleProperty();
private DoubleProperty leftPaddleY = new SimpleDoubleProperty();
private DoubleProperty rightPaddleY = new SimpleDoubleProperty();
private double leftPaddleDragAnchorY;
private double rightPaddleDragAnchorY;
private double initLeftPaddleTranslateY;
private double initRightPaddleTranslateY;
private Circle ball = new Circle();
private Rectangle leftPaddle = new Rectangle();
private Rectangle rightPaddle = new Rectangle();
private Rectangle topWall = new Rectangle();
private Rectangle bottomWall = new Rectangle();
private Rectangle leftWall = new Rectangle();
private Rectangle rightWall = new Rectangle();
private Group pongConponents = new Group();
private Button startButton = new Button();
private BooleanProperty startVisible = new SimpleBooleanProperty(true);
private boolean movingRight = true;
private boolean movingDown = true;
private Timeline pongAnimation = new Timeline();
private void createAnimation() {
pongAnimation.setCycleCount(Timeline.INDEFINITE);
KeyFrame f1 = new KeyFrame(new Duration(10.0), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
checkForCollision();
int horzPixels = movingRight ? 1 : -1;
int vertPixels = movingDown ? 1 : -1;
centerX.setValue(centerX.getValue() + horzPixels);
centerY.setValue(centerY.getValue() + vertPixels);
}
});
pongAnimation.getKeyFrames().add(f1);
}
void initialize() {
centerX.setValue(250);
centerY.setValue(250);
leftPaddleY.setValue(235);
rightPaddleY.setValue(235);
startVisible.setValue(true);
pongConponents.requestFocus();
}
private void checkForCollision() {
if (ball.intersects(rightWall.getBoundsInLocal()) || ball.intersects(leftWall.getBoundsInLocal())) {
pongAnimation.stop();
initialize();
} else if (ball.intersects(bottomWall.getBoundsInLocal()) || ball.intersects(topWall.getBoundsInLocal())) {
movingDown = !movingDown;
} else if (ball.intersects(leftPaddle.getBoundsInParent()) && !movingRight) {
movingRight = !movingRight;
} else if (ball.intersects(rightPaddle.getBoundsInParent()) && movingRight) {
movingRight = !movingRight;
}
}
@Override
public void start(Stage primaryStage) throws Exception {
createAnimation();
ball.setRadius(5.0);
ball.setFill(Color.WHITE);
topWall.setX(0);
topWall.setY(0);
topWall.setWidth(500);
topWall.setHeight(1);
leftWall.setX(0);
leftWall.setY(0);
leftWall.setWidth(1);
leftWall.setHeight(500);
rightWall.setX(500);
rightWall.setY(0);
rightWall.setWidth(1);
rightWall.setHeight(500);
bottomWall.setX(0);
bottomWall.setY(500);
bottomWall.setWidth(500);
bottomWall.setHeight(1);
leftPaddle.setX(20);
leftPaddle.setWidth(10);
leftPaddle.setHeight(30);
leftPaddle.setFill(Color.BLUE);
leftPaddle.setCursor(Cursor.HAND);
leftPaddle.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
initLeftPaddleTranslateY = leftPaddle.getTranslateY();
leftPaddleDragAnchorY = t.getSceneY();
}
});
leftPaddle.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
double dragY = t.getSceneY() - leftPaddleDragAnchorY;
leftPaddleY.setValue(initLeftPaddleTranslateY + dragY);
}
});
rightPaddle.setX(470);
rightPaddle.setWidth(10);
rightPaddle.setHeight(30);
rightPaddle.setFill(Color.RED);
rightPaddle.setCursor(Cursor.HAND);
rightPaddle.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
initRightPaddleTranslateY = rightPaddle.getTranslateY();
rightPaddleDragAnchorY = t.getSceneY();
}
});
rightPaddle.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
double dragY = t.getSceneY() - rightPaddleDragAnchorY;
rightPaddleY.setValue(initRightPaddleTranslateY + dragY);
}
});
startButton.setLayoutX(225);
startButton.setLayoutY(470);
startButton.setText("Start!");
startButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
startVisible.set(false);
pongAnimation.playFromStart();
pongConponents.requestFocus();
}
});
pongConponents.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.SPACE && pongAnimation.statusProperty().equals(Animation.Status.STOPPED)) {
rightPaddleY.setValue(rightPaddleY.getValue() - 6);
} else if (t.getCode() == KeyCode.L
&& !rightPaddle.getBoundsInParent().intersects(topWall.getBoundsInLocal())) {
rightPaddleY.setValue(rightPaddleY.getValue() - 6);
} else if (t.getCode() == KeyCode.COMMA
&& !rightPaddle.getBoundsInParent().intersects(bottomWall.getBoundsInLocal())) {
rightPaddleY.setValue(rightPaddleY.getValue() + 6);
} else if (t.getCode() == KeyCode.A
&& !leftPaddle.getBoundsInParent().intersects(topWall.getBoundsInLocal())) {
leftPaddleY.setValue(leftPaddleY.getValue() - 6);
} else if (t.getCode() == KeyCode.Z
&& !leftPaddle.getBoundsInParent().intersects(bottomWall.getBoundsInLocal())) {
}
}
});
pongConponents.getChildren()
.addAll(ball, topWall, leftWall, rightWall, bottomWall, leftPaddle, rightPaddle, startButton);
Scene scene = new Scene(pongConponents, 500, 500, Color.BLACK);
primaryStage.setScene(scene);
initialize();
primaryStage.setTitle("Zen Pong");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Obrigado