Hi my name is Selvyre and in this tutorial we are going to be covering our basic game states and our game class so we can get a window up and running
OOOOOOKKKKKK so to start we need to right click our src folder in the game project and hit new package (demonstrated HERE) im gonna call mine Main (yes with a cap M please), for those who dont know a package is essentially javas version of a folder, think of them as literally just organization tools. Inside packages are class files that we create and those are in turn compiled together to make a program in our case a 2d game.[/URL]
Our First Class Files
Ok so lets begin by right clicking our package now and hitting "New Class"(dont think i need a pic) and we are gonna name it "Game" now you should see a window with some code like this
Now here is where we are going to test whether or not you guys did the first tutorial correctly, if your StateBasedGame will import. If you would be so kind as to add this simple line of code to your existing codeCode:package Main; pubic class Game { }
We now need to import our SBG(will only work if TuT1 is done correctly) (pic featured HERE) You should now see some lines added(green) and you are going to need to add a few lines yourself(red)Code:package Main; public class Game extends StateBasedGame{ }
Now at first MainMenuState and PlayState are gonna give an error because we have no created them yet but dont worry we will, essentially what those do is add two states a Main Menu where we will press play or exit and stuff like that and then a PlayState where all the action is gonna happenCode:package Main; import org.newdawn.slick.GameContainer; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; public Game(String name) { super(name); } @Override public void initStatesList(GameContainer container) throws SlickException { this.addState(new MainMenuState()); this.addState(new PlayState()); } public static void main(String[] args) { AppGameContainer container = new AppGameContainer(new Game("GAME NAME")); container.setDisplayMode(800, 640, false); container.setTargetFrameRate(60); container.start(); }
In the main(String[] args) method we created an AppGameContainer which is a class from slick2d that is essentially just a window and that window is gonna be called "GAME NAME" then we set the display mode to 800x640 and non fullscreen(thats what false is for) then we start the window. All this will be done when we first compile or run our game (because its in the main method)
Now if you would be so kind as to make 2 new class files
MainMenuStateOk the simplest way to describe each of the methods in these 2 classes is this, init is now where you are going to initialize things, not in the constructor anymore but in the init method now, this is a method made by slick2d and its linked to AppGameContainer so we are gonna use it to achieve greatness, next is render...render is gonna be anything having to do with drawing on the screen, numbers, text, images anything like that goes in render and all our movements, and other logic, and collision will go in update. The id of MainMenuState is 0 and PlayState 1 these can be accessed and changed by doing Game.enterState(0 or 1). Now if you run the game by pressing the green arrow on the top toolbar (under refactor and navigate) you should get a black window that shows 60 fps in the top right corner(or maybe 61) and says GAME NAME at the top of the window, if so good freaking job you were able to stay with me through this tutorial that is so poorly written
PlayStateCode:package Main; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; public class MainMenuState extends BasicGameState { @Override public void init(GameContainer container, StateBasedGame game) throws SlickException { } @Override public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { } @Override public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException { } @Override public int getID() { return 0; } }
Code:package Main; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; public class PlayState extends BasicGameState { @Override public void init(GameContainer container, StateBasedGame game) throws SlickException { } @Override public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { } @Override public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException { } @Override public int getID() { return 1; } }