Repository navigation
Screen based games 0.8 release
Thomas Cashman edited this page Oct 30, 2013
·
3 revisions
It is easier to split the various parts of a game into multiple game screen, e.g. LoadingScreen, MainMenuScreen, InGameScreen, etc.
Mini2Dx provides the ScreenBasedGame class to allow for implementation of this. In the example below the developer has implemented a game with two screens; A loading screen and an in-game screen.
public class ExampleGame extends ScreenBasedGame {
@Override
public void initialise() {
this.addScreen(new LoadingScreen());
this.addScreen(new InGameScreen());
}
@Override
public int getInitialScreenId() {
return LoadingScreen.ID;
}
}public class LoadingScreen implements GameScreen {
public static int ID = 1;
private float loadingTime = 4f;
public void initialise(GameContainer gc) {}
public void update(GameContainer gc, ScreenManager screenManager, float delta) {
if(loadingTime > 0f) {
loadingTime -= delta;
if(loadingTime < 0f) {
//Fade to InGameScreen after 4 seconds
screenManager.enterGameScreen(InGameScreen.ID, new FadeOutTransition(),
new FadeInTransition());
}
}
}
public void render(GameContainer gc, Graphics g) {
g.drawString("Loading...", 32, 32);
}
public int getId() {
return ID;
}
}public class InGameScreen implements GameScreen {
public static int ID = 2;
public void initialise(GameContainer gc) {
}
public void update(GameContainer gc, ScreenManager screenManager, float delta) {
}
public void render(GameContainer gc, Graphics g) {
g.drawString("In-game!!!", 32, 32);
}
public int getId() {
return ID;
}
}