Hallo Zusammen,
ich habe mal ne kleine persönliche Challenge gemacht und innerhalb von ner Stunde nen kleines Reaktionszeitspiel
mit JavaFX geschrieben.
Ich poste das einfach mal und vielleicht findet das jemand ja cool genug für irgendwas
Falls Ihr optimierungen oder sonstige sachen verbessern wollt könnt ihr das natürlich gerne tun.
Würde mich freuen wenn Ihr eure Sachen dann ebenso hier posten könnt
Habt Spaß und bleibt gesund
ich habe mal ne kleine persönliche Challenge gemacht und innerhalb von ner Stunde nen kleines Reaktionszeitspiel
mit JavaFX geschrieben.
Ich poste das einfach mal und vielleicht findet das jemand ja cool genug für irgendwas
Falls Ihr optimierungen oder sonstige sachen verbessern wollt könnt ihr das natürlich gerne tun.
Würde mich freuen wenn Ihr eure Sachen dann ebenso hier posten könnt
Java:
public static void main(String[] args) {
launch(args);
}
VBox slider = new VBox();
abstract static class Field extends HBox {
int id;
}
static class FailureField extends Field {
public FailureField() {
id = -1;
setStyle("-fx-background-color: linear-gradient(#FF2525 40%, #FF6925 100%);");
}
}
static class SuccessField extends Field {
public SuccessField() {
id = 1;
setStyle("-fx-background-color: linear-gradient(#009900 50%, #77FF77 100%);");
}
}
static class BonusField extends Field {
public BonusField() {
id = 2;
setStyle("-fx-background-color: linear-gradient(#77FF77 20%, #0090FF 80%);");
}
}
static class FieldShape {
Class<? extends Field> fieldClass;
double width;
public FieldShape(Class<? extends Field> fieldClass, double width) {
this.fieldClass = fieldClass;
this.width = width;
}
public Class<? extends Field> getFieldClass() {
return fieldClass;
}
public double getWidth() {
return width;
}
}
static class FieldShapeMatrix {
List<FieldShape> fieldShapes = new LinkedList<>();
public void addFieldShapes(FieldShape ...shapes) {
fieldShapes.addAll(Arrays.asList(shapes));
}
public List<FieldShape> getFieldShapes() {
return fieldShapes;
}
}
static class ExampleFieldMatrix {
FieldShapeMatrix matrix = new FieldShapeMatrix();
public ExampleFieldMatrix() {
matrix.addFieldShapes(
new FieldShape(FailureField.class, 300),
new FieldShape(BonusField.class, 10),
new FieldShape(SuccessField.class, 50),
new FieldShape(BonusField.class, 10),
new FieldShape(FailureField.class, 300));
}
public FieldShapeMatrix getMatrix() {
return matrix;
}
}
private boolean doRun = true;
private double speed = 2;
private long timeToWait = 3;
private Thread createRunningThread() {
Thread t = new Thread(() -> {
double x = 0;
boolean forward = true;
while (doRun) {
try {
if (forward) {
slider.setTranslateX(x);
x += speed;
} else {
slider.setTranslateX(x);
x -= speed;
}
if (forward && x >= Main.this.background.getWidth()/2) {
forward = false;
} else if (x <= (Main.this.background.getWidth()/2) * -1) {
forward = true;
}
Thread.sleep(timeToWait);
Platform.runLater(() -> slider.requestFocus());
} catch (Exception e) {
e.printStackTrace();
}
}
});
return t;
}
HBox background = new HBox();
@Override
public void start(Stage mainStage) {
mainStage.setMinHeight(50);
mainStage.setResizable(false);
mainStage.setFullScreen(false);
ExampleFieldMatrix exampleFieldMatrix = new ExampleFieldMatrix();
StackPane stack = new StackPane();
stack.setOnMouseClicked(event -> {
if (doRun) {
doRun = false;
double xPos = slider.getTranslateX();
double pos = (Main.this.background.getWidth()/2) * -1;
Class<? extends Field> resultField = null;
for (FieldShape fieldShape : exampleFieldMatrix.getMatrix().getFieldShapes()) {
pos += fieldShape.getWidth();
if (pos >= xPos) {
resultField = fieldShape.getFieldClass();
break;
}
}
if (resultField.isAssignableFrom(BonusField.class)) {
new Alert(Alert.AlertType.INFORMATION, "BONUS WIN!").showAndWait();
} else if (resultField.isAssignableFrom(SuccessField.class)) {
new Alert(Alert.AlertType.INFORMATION, "SUCCESS").showAndWait();
} else {
new Alert(Alert.AlertType.INFORMATION, "FAILURE").showAndWait();
}
} else {
Platform.runLater(() -> {
doRun = true;
createRunningThread().start();
});
}
});
int maxWidth = 0;
for (FieldShape fieldShape : exampleFieldMatrix.getMatrix().getFieldShapes()) {
try {
Field field = fieldShape.getFieldClass().newInstance();
double width = fieldShape.getWidth();
maxWidth += width;
field.setMinWidth(width);
field.setPrefWidth(width);
field.setMaxWidth(width);
background.getChildren().add(field);
} catch (Exception e) {
e.printStackTrace();
}
}
background.setMinWidth(maxWidth);
background.setPrefWidth(maxWidth);
background.setMaxWidth(maxWidth);
stack.setMinWidth(maxWidth);
stack.setPrefWidth(maxWidth);
stack.setMaxWidth(maxWidth);
slider.setMinWidth(2);
slider.setPrefWidth(2);
slider.setMaxWidth(2);
slider.prefHeightProperty().bind(stack.heightProperty());
slider.setStyle("-fx-background-color: black;");
slider.setTranslateX(0);
stack.getChildren().addAll(background, slider);
Scene scene = new Scene(stack);
mainStage.setScene(scene);
mainStage.sizeToScene();
mainStage.addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, event -> System.exit(0));
mainStage.show();
createRunningThread().start();
}
Habt Spaß und bleibt gesund