因此,我已经开发了一款游戏,到目前为止,我只有一个级别,我想添加一个具有不同板条箱位置,菱形位置和墙位置的第二级,但是我不确定采用哪种最佳方法。
您会建议我让我的第二个(或更多)继承我的第一个级别并更改游戏代码,还是以稍微不同的方式来做?就像我可以再制作一个称为“ level2”的类,并在当前的游戏类中添加一些代码,使玩家在完成第一级时跳至第二级吗?
public class Game extends Application {
Pane backgroundPane;
Pane playfieldLayer;
Pane scoreLayer;
Image playerImage;
Image wallImage;
Image foodImage;
Image diamondImage;
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;
@Override
public void start(Stage theStage) {
// theStage.setWidth(Settings.SCENE_WIDTH);
// theStage.setHeight(Settings.SCENE_HEIGHT);
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();
loadGame();
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
checkCollisions();
// GamePane.checkWallCollisions();
checkWallCollisions();
checkfoodwallCollisions();
checkDiamondCollisions();
checkWallPlayerCollisions();
checkWin();
// update sprites in scene
players.forEach(sprite -> sprite.updateUI());
foods.forEach(sprite -> sprite.updateUI());
}
};
gameLoop.start();
}
private void loadGame() {
playerImage = new Image(getClass().getResource("warehouse.png").toExternalForm());
wallImage = new Image(getClass().getResource("Wall.png").toExternalForm());
foodImage = new Image(getClass().getResource("food.png").toExternalForm());
diamondImage = new Image(getClass().getResource("Chef.png").toExternalForm());
}
private void createPlayers() {
Input input = new Input(scene);
input.addListeners(scene);
Image image = playerImage;
double x = (Settings.SCENE_WIDTH - image.getWidth()) / 1.5;
double y = Settings.SCENE_HEIGHT * 0.2;
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(scene);
Image image = foodImage;
double x = (Settings.SCENE_WIDTH - image.getWidth()) / 2.0;
double y = Settings.SCENE_HEIGHT * 0.4;
Food food = new Food(playfieldLayer, image, x, y, 0, 0, input);
foods.add(food);
}
private void spawnWall() {
Input input = new Input(scene);
input.addListeners(scene);
Image image = wallImage;
double x = Settings.SCENE_WIDTH * 0.01;
double y = Settings.SCENE_HEIGHT * 0.01;
Wall wall = new Wall(playfieldLayer, image, x, 100, 0, 0, input);
Wall wall2 = new Wall(playfieldLayer, image, 570, 150, 0, 0, input);
walls.add(wall);
walls.add(wall2);
for (int i = 0; i < 9; i++) {
y = 200;
x = x + 50;
Wall wall1 = new Wall(playfieldLayer, image, x, y, 0, 0, input);
walls.add(wall1);
}
for (int i = 0; i < 2; i++) {
y = y -40;
x = x - 50;
Wall wall4 = new Wall(playfieldLayer, image, x, y, 0, 0, input);
walls.add(wall4);
}
for (int i = 0; i < 3; i++) {
y = y + 150;
x = x -100;
Wall wall5 = new Wall(playfieldLayer, image, x, y, 0, 0, input);
walls.add(wall5);
}
}
private void spawnDiamonds() {
Input input = new Input(scene);
input.addListeners(scene);
Image image = diamondImage;
double x = (Settings.SCENE_WIDTH - image.getWidth()) / 3.0;
double y = Settings.SCENE_HEIGHT * 0.1;
Diamonds diamond = new Diamonds(playfieldLayer, image, x, y, 0, 0, input);
diamonds.add(diamond);
}
private boolean checkCollisions() {
collision = false;
for (Player player : players) {
for (Food food : foods) {
if (player.collidesWith(food)) {
player.setDy(-player.getDy());
player.setDx(-player.getDx());
Food.collision = true;
}
}
}
return collision;
}
//other collisions exist but not added due to simplicity
public void checkWin() {
if (won == true) {
System.out.println("won");
}
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
尽管我很久以前从事过一些Java项目,但我并不是真正的Java开发人员。但是,以下建议适用于任何面向对象的项目,因此可能会有帮助。
类定义对象的类型。如果两个对象属于同一类型,则它们应从同一类派生。
有时,您想修改类型。您可以通过扩展类来完成。
扩展类通常具有其他属性或方法;也就是说它将具有其他数据或功能。
具有类的全部目的是使多个对象具有相同的属性和方法。属性值可能会更改,但是对象仍然是同一类型。
考虑到您问题的性质,我倾向于建议:
附加级别可能只是具有不同属性值的附加对象。我不认为您实际上需要一门新课。
在运行时,您一次可能只处理一个级别,因此您甚至可能使用相同的对象,并用不同的属性级别对其进行了初始化。
编辑
同样,我不在这里,所以术语我错了。
如果我有多个级别,则可能会创建一个描述符类,该描述符类具有所有变量元素的属性。然后,我将创建一个此类对象的数组,表示每个对象的属性值。
最后,我将使用构造函数中的特定数组元素来证实或重新初始化一个级别。像这样:
level2 = new Level(levels[2]); // new object
// or
level = level.init(levels[2]); // existing object
关于java - 如何使用Java进行第二级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42743288/