sample.java
package happy_unhappy_game;
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Sample extends Application {
public static void main(String[]args) {
launch();
}
public static Scene scene1 = null;
public static Scene scene2 = null;
public static String txt=null;
public static String txt2=null;
public static Label status=new Label();
public static Button btn2 =null;
public static VBox root=new VBox();
public static TilePane pane=new TilePane();
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("main");
stage.setWidth(380);
stage.setHeight(500);
//Creating a stage
initScene2(stage);
initScene1(stage);
stage.setScene(scene1);
stage.show();
}
public static void initScene1(Stage stage){
stage.setWidth(380);
stage.setHeight(500);
stage.setTitle("1");
Random rnd=new Random();
Button button[]=new Button[144];
for(int i=0;i<144;i++) {
button[i]=new Button("Spicy");
button[i].setPrefWidth(30);
button[i].setPrefHeight(30);
String txt=String.format("that is\"Spicy\"is.");
button[i].setOnAction(event->status.setText(txt));
}
int number=rnd.nextInt(144);
button[number]=new Button("Fortunately");
button[number].setPrefWidth(30);
button[number].setPrefHeight(30);
txt2=String.format("\"Fortunately\"Was clicked.");
button[number].setOnAction(event->push(stage));
pane=new TilePane();
pane.getChildren().addAll(button);
root=new VBox();
root.getChildren().addAll(pane,status);
scene1=new Scene(root);
}
public static void initScene2(Stage stage) {
stage.setWidth(380);
stage.setHeight(500);
stage.setTitle("2");
Button btn = new Button("Scene change completed! !! !!");
btn.setPrefWidth(100);
btn.setPrefHeight(50);
btn.setOnMouseClicked(event -> setScene2(stage,scene1));
AnchorPane root = new AnchorPane();
root.getChildren().add(btn);
scene2 = new Scene(root);
}
public static void push(Stage stage) {
status.setText(txt2);
btn2 = new Button("next");
btn2.setPrefWidth(100);
btn2.setPrefHeight(50);
btn2.setOnMouseClicked(event -> setScene(stage,scene2));
root.getChildren().addAll(btn2);
}
public static void setScene(Stage stage,Scene changeScene) {
stage.setScene(changeScene);
stage.show();
}
public static void setScene2(Stage stage,Scene changeScene) {
stage.setScene(changeScene);
stage.show();
}
}
A game that searches for one "good luck" button from a large number of "spicy" buttons. At the moment, I am struggling because the screen transition is not good. In the future, we have added a time limit and a counting function.
Recommended Posts