已將我的代碼更改爲此。嘗試從我的Game類實例化新的Level 1。然而,現在沒有什麼顯示當我運行應用程序。在我的遊戲中添加第二個關卡
public class Game extends Application {
Pane backgroundPane;
Pane playfieldLayer;
Pane scoreLayer;
Level2 level2;
Image playerImage = new Image(getClass().getResource("warehouse.png").toExternalForm());
Image wallImage = new Image(getClass().getResource("Wall.png").toExternalForm());
Image foodImage = new Image(getClass().getResource("food.png").toExternalForm());
Image diamondImage = new Image(getClass().getResource("Chef.png").toExternalForm());
Player player;
Wall wall;
List<Player> players = new ArrayList<>();
List<Food> foods = new ArrayList<>();
List<Wall> walls = new ArrayList<>();
List<Diamonds> diamonds = new ArrayList<>();
boolean collision;
boolean wallCollision;
boolean foodWallCollision;
boolean won = false;
GridPane gameGrid;
static Scene scene;
Stage theStage;
Input input;
@Override
public void start(Stage theStage) {
Menu menu = new Menu();
Scene menuView = new Scene(menu, Settings.SCENE_HEIGHT, Settings.SCENE_WIDTH);
theStage.setScene(menuView);
// theStage.setResizable(false);
theStage.show();
Menu.start.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
runGame(theStage);
}
});
}
public void runGame(Stage theStage) {
// Input input = new Input(scene);
Group root = new Group();
GridPane gameGrid = new GamePane(14, 14);
gameGrid.setStyle("-fx-background-color: white; -fx-grid-lines-visible:true");
backgroundPane = new Pane();
backgroundPane.setId("root");
playfieldLayer = new Pane();
scoreLayer = new Pane();
playfieldLayer.getChildren().add(gameGrid);
root.getChildren().add(backgroundPane);
root.getChildren().add(playfieldLayer);
// scene = new Scene(root, (columnAmount * 40) + 66, (rowAmount * 40) +
// 66, Color.WHITE);
scene = new Scene(root, 600, 500);
backgroundPane.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm());
// theStage.setResizable(false);
theStage.setScene(scene);
theStage.show();
level2 = new Level2(playerImage, wallImage, foodImage, diamondImage, player, wall, gameGrid, theStage, input, collision, wallCollision, foodWallCollision, players, foods, walls, diamonds);
level2.startLevel2();
}
public static void main(String[] args) {
launch(args);
}
}
等級1級(我會分裂的代碼還我只是試圖讓事情到目前運行)
public abstract class Level1 {
Pane backgroundPane;
Pane playfieldLayer;
Pane scoreLayer;
Image playerImage;
Image wallImage;
Image foodImage;
Image diamondImage;
Player player;
Wall wall;
List<Player> players = new ArrayList<>();
List<Food> foods = new ArrayList<>();
List<Wall> walls = new ArrayList<>();
List<Diamonds> diamonds = new ArrayList<>();
boolean collision;
boolean wallCollision;
boolean foodWallCollision;
boolean won = false;
GridPane gameGrid;
Scene scene;
Stage theStage;
Input input;
public Level1(Image playerImage, Image wallImage,
Image foodImage, Image diamondImage, Player player, Wall wall, GridPane gameGrid, Stage theStage,
Input input, boolean collision, boolean wallCollision, boolean foodWallCollision, List<Player> players,
List<Food> foods,List<Wall> walls, List<Diamonds> diamonds)
{
this.playerImage = playerImage;
this.wallImage = wallImage;
this.foodImage = foodImage;
this.diamondImage = diamondImage;
this.player = player;
this.wall = wall;
this.gameGrid = gameGrid;
this.theStage = theStage;
this.input = input;
this.collision = collision;
this.wallCollision = wallCollision;
this.foodWallCollision = foodWallCollision;
this.players = players;
this.foods = foods;
this.walls = walls;
this.diamonds = diamonds;
//a billion getters and setters below.
}
這是我的level2的類,我想成爲第一級顯示。我的try catch語句在程序運行時被捕獲,所以這與我如何將項目產生到我的遊戲領域上有關。調用Application#launch()
(見the life cycle of a JavaFX Application)時
public class Level2 extends Level1{
public Level2(Image playerImage, Image wallImage,
Image foodImage, Image diamondImage, Player player, Wall wall, GridPane gameGrid, Stage theStage,
Input input, boolean collision, boolean wallCollision, boolean foodWallCollision, List<Player> players,
List<Food> foods,List<Wall> walls, List<Diamonds> diamonds){
super(playerImage, wallImage, foodImage,diamondImage, player,
wall, gameGrid, theStage, input, collision, wallCollision, foodWallCollision, players, foods, walls, diamonds);
// TODO Auto-generated constructor stub
}
public void startLevel2(){
try {
createPlayers();
spawnFood();
spawnWall();
spawnDiamonds();
} catch (NullPointerException e) {
System.out.println("You are missing the pictures for the spawn methods");
}
AnimationTimer gameLoop = new AnimationTimer() {
@Override
public void handle(long now) {
// player input
players.forEach(sprite -> sprite.processInput());
foods.forEach(sprite -> sprite.processInput());
// movement
players.forEach(sprite -> sprite.move());
foods.forEach(sprite -> sprite.move());
// ((Player) players).stopInput();
// check collisions
// update sprites in scene
players.forEach(sprite -> sprite.updateUI());
foods.forEach(sprite -> sprite.updateUI());
}
};
gameLoop.start();
}
private void createPlayers() {
Input input = new Input(scene);
input.addListeners();
Image image = playerImage;
double x = (Settings.SCENE_WIDTH - image.getWidth())/1.0;
double y = Settings.SCENE_HEIGHT * 0.8;
Player player = new Player(playfieldLayer, image, x, y, 0, 0, 0, input);
players.add(player);
}
private void spawnFood() {
Input input = new Input(scene);
input.addListeners();
Image image = foodImage;
double x = (Settings.SCENE_WIDTH - image.getWidth())/2.0;
double y = Settings.SCENE_HEIGHT * 0.4;
//Food food1 = new Food(playfieldLayer, image, 150, 50, 0,0);
Food food = new Food(playfieldLayer, image, x, y, 0, 0,input);
foods.add(food);
//foods.add(food1);
}
}
不知道,因爲我覺得這種代碼方式太複雜,無法理解。你的ctor只在田地裏很好,但是我們會說,這個班裏的田地數量大約是10或15個條目太長。如此多的領域意味着你在這個貧窮階層中做了太多事情。良好的面向對象的本質是將不同的職責分離到不同的類別中! – GhostCat
我很尷尬的這段代碼。把我的碰撞方法分成他們自己的碰撞類會更有意義嗎? – SteveMunroe
可能。但細節將需要看你的整個代碼庫。我的建議:閱讀關於單一責任原則。然後創建一個新的PER級責任。並尋找一些有經驗的人來審查你的代碼庫。最後:不要感到尷尬。學習編程的真正美德只需要不斷的練習。 – GhostCat