[ニコニコ動画] JavaFXでゲームを作りたい! part1 [YouTube]
動画
[ゆっくり解説] JavaFXでゲームをつくりたい! part 1 [JavaFX] - YouTube
[ゆっくり解説] JavaFXでゲームをつくりたい! part1 - ニコニコ動画
素材
BGM: プラスチックアドベンチャー (MusMus) Pop up the mind wings (MusMus) 潮騒アーべント (MusMus) タイルセット: Cute Platformer Sisters | OpenGameArt.org
(Master484さん) キャラチップ: ぴぽや
(ぴぽやさん)
コード
package application; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.scene.input.KeyEvent; import javafx.stage.Stage; public class Maze extends Application{ public static void main(String[] args) { launch(args); } public ImageView player1ImgView = new ImageView(); public ImageView playerImgView = new ImageView(); public Image playerMoveImg[][] = new Image[4][3]; @Override public void start(Stage stage) throws Exception{ // stage.setHeight(360); // stage.setWidth(360); stage.setTitle("TheMaze"); // マップの画像を読み込む Path mapPath = Paths.get("image/map.png"); Image mapImg = new Image(mapPath.toUri().toString()); ImageView mapImgView = new ImageView(mapImg); // プレイヤのタイルセットから歩行グラフィックを切り取る Path playerPath1 = Paths.get("image/player1.png"); Image playerImg1 = new Image(playerPath1.toUri().toString()); int width = 32; int height = 32; for(int i = 0; i < 4; i++) { for(int j = 0; j < 3; j++) { playerMoveImg[i][j] = new WritableImage(playerImg1.getPixelReader() ,width * j, height * i, width, height); } } // プレイヤの初期位置と画像を設定 player1ImgView = new ImageView(playerMoveImg[0][1]); player1ImgView.setLayoutX(32); player1ImgView.setLayoutY(32); Group group = new Group(); group.getChildren().addAll(mapImgView,player1ImgView); Scene scene = new Scene(group); int[][] mapData = getMapData(); scene.setOnKeyPressed(event -> moveEvent(event, mapData)); stage.setScene(scene); stage.show(); } //キーイベントの処理 キーボードでプレイヤを動かす public void moveEvent(KeyEvent event, int[][] mapData) { String s = event.getCode().toString(); //System.out.println(s); if(s.equals("DOWN")) { player1ImgView.setImage(playerMoveImg[0][0]); if( checkMove(mapData, 1, 0)) { player1ImgView.setLayoutY(player1ImgView.getLayoutY() + 32); } }else if(s.equals("UP")) { player1ImgView.setImage(playerMoveImg[3][0]); if( checkMove(mapData, -1, 0)) { player1ImgView.setLayoutY(player1ImgView.getLayoutY() - 32); } }else if(s.equals("LEFT")) { player1ImgView.setImage(playerMoveImg[1][0]); if( checkMove(mapData, 0, -1)) { player1ImgView.setLayoutX(player1ImgView.getLayoutX() - 32); } }else if(s.equals("RIGHT")) { player1ImgView.setImage(playerMoveImg[2][1]); if( checkMove(mapData, 0, 1)) { player1ImgView.setLayoutX(player1ImgView.getLayoutX() + 32); } } } // マップの衝突判定 public boolean checkMove(int[][] mapData, int i, int j) { int col = (int)player1ImgView.getLayoutX()/32; int row = (int)player1ImgView.getLayoutY()/32; if(mapData[row + i][col + j] == 29) { return true; }else if(mapData[row + i][col + j] == 82){ return true; }else { return false; } } // textファイルからmap情報を取得し、int型の配列を返す public int[][] getMapData() { int [][]mapData = new int[10][10]; Path path = Paths.get("maptext/mapdata1.txt"); File file = path.toFile(); try { FileReader filereader = new FileReader(file); BufferedReader br = new BufferedReader(filereader); String str; int i = 0;; while((str = br.readLine()) != null) { String[] array = str.split(","); for(int j = 0; j < array.length; j++) { mapData[i][j] = Integer.parseInt(array[j]); } i++; } br.close(); }catch(FileNotFoundException e) { System.out.println(e); }catch (IOException e) { System.out.println(e); } return mapData; } }
動画のウィンドウサイズに関するコード
package application; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.stage.Stage; public class Test0 extends Application{ public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception{ stage.setTitle("Test0"); stage.setHeight(700); stage.setWidth(700); // ウィンドウサイズをラベルで表示させる Label label1 = new Label(); label1.textProperty().bind(stage.widthProperty().asString()); label1.setFont(new Font(20)); Label wid = new Label("Width: "); wid.setFont(new Font(20)); HBox hbox1 = new HBox(wid, label1); hbox1.setLayoutX(20); hbox1.setLayoutY(40); Label label2 = new Label(); label2.textProperty().bind(stage.heightProperty().asString()); label2.setFont(new Font(20)); Label hei = new Label("Height: "); hei.setFont(new Font(20)); HBox hbox2 = new HBox(hei, label2); hbox2.setLayoutX(20); hbox2.setLayoutY(100); Rectangle rect1 = new Rectangle(0, 0, 150, 150); rect1.setStroke(Color.BLACK); rect1.setArcWidth(20.0); rect1.setArcHeight(20.0); rect1.setFill(null); Group g = new Group(hbox1, hbox2, rect1); Scene scene = new Scene(g); stage.setScene(scene); stage.show(); } }