0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javafxで簡単なゲームを作ってみた① ”幸せを探そうゲーム”(未完成)

Last updated at Posted at 2020-06-25

#Javafxで簡単なゲームを作ってみた①
##”幸せを探そうゲーム”(未完成)

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);
    	  // ステージの作成
    	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("辛");
    		button[i].setPrefWidth(30);
    		button[i].setPrefHeight(30);
    		String txt=String.format("それは\"辛い\"です。");
    		button[i].setOnAction(event->status.setText(txt));
    	}

    		int number=rnd.nextInt(144);
			button[number]=new Button("幸");
    		button[number].setPrefWidth(30);
    		button[number].setPrefHeight(30);
    		txt2=String.format("\"幸\"がクリックされました。");
    		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("シーン変更完了!!!");
    	    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("次へ");
		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();
	  }

 	}



##ゲームの概要と現段階での問題点
多数の"辛"ボタンの中から、一つの"幸"ボタンを探すゲーム。
今のところ、画面推移がうまくできず苦戦中。
今後は時間制限や、カウント機能を付けていきた。

200625.gif

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?