Jump to content
Sign in to follow this  
Macecraft

Java Games Programming Tutorial #1n

Recommended Posts

It's time to learn some Slick2D!

This time we're aiming to just have a window which says "Hello World" on it! 

 

Please note the terms - 

AppGameContainer, BasicGameState, StateBasedGame, GameContainer & delta will be explained in the next tutorial

 

Let's start with some classes!

 

  1. Make a new class called 'Display', I'd suggest putting it into a package mine in a package called: 
    com.mace.teaching; 
  2. Now make another class and call this 'State1', I'd also suggest putting this into a package but with an extra subfolder
    com.mace.teaching.states; is mine.

Now let's run through the code we use in 'Display' 

package com.mace.teaching;

//Below we import the required Slick classes
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

//Importing our State1 class for use in Display
import com.mace.teaching.states.State1;

//Here we inherit all the things we need from Slick's 'StateBasedGame' class
public class Display extends StateBasedGame{

        //Variables we'll use, g_name is the window title
        //Width = window width
        //Height is the window height
	static String g_name = "Slick2D";
	static int WIDTH = 400; 
	static int HEIGHT = 400; 
        //Our constructor takes a string as a requirement
	public Display(String name) {
		super(name);
		this.addState(new State1()); //Here we add our 'State1' as a game state to be used
                //Don't worry if Eclipse readlines, we'll add the functionality for it to work soon!
	}

        //An inherited method from the StateBasedGame class
	@Override
	public void initStatesList(GameContainer gc) throws SlickException {
		// TODO Auto-generated method stub
		this.enterState(0); //Here we specify that we want state1 to be the one we enter first
	}
	public static void main(String args[]){
              //Our AppGameContainer will create the GUI window we see
		AppGameContainer agc;
		try{
                        //Here we pass in this class as the StateBasedGame
			agc = new AppGameContainer(new Display(g_name)); 
                        //Setting the display mode, requirements for this function are - 
                        //agc.setDisplayMode(int width, int height, boolean fullscrean);
			agc.setDisplayMode(WIDTH, HEIGHT, false);
                        //Here we tell it to create the window 
			agc.start();
		}catch(SlickException e){
                       //Just in case we can't create the window, we catch the exception
			e.printStackTrace();
		}
	}

}

Here's the code for the class 'State1'

package com.mace.teaching.states;

//Imports again!
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;

//Inheriting the methods from BasicGameState
public class State1 extends BasicGameState {

	//The text we will draw to our new window 
	String hi = "Hello World";

	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		  //In Slick this method is called once to initialize data and that's it

	         //Because it's called once it throws a SlickException for when we get to
                //resource loading 
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		 // This is the method which will render data to the screen
                 //It gets called a number of times per second so it can constantly update the position
                 //of objects on the screen

                 //The instruction below tells the graphics tool to take a string variable
                 //And draw text to the location of 150,150
                //The generic requirements for rendering are - g.RandomFunc(Object obj, int x, int y); 
		g.drawString(hi, 150 , 150);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		// This is the update method where the bulk of the code will go 
                // This is where things like collision detection, key presses & general game logic are
                //handled. It gets called in the game loop cycle as often as render. 
                //The calling per cycle will become important when we use delta
	}

	@Override
	public int getID() {
		// This tells our 'Display' class what number this state is
		return 0;
	}

}

If all is well, when you try run this you should see - 

teaching.png

 

If not, paste any console messages and I'll you why. The full details of what we've done will be explained in the next tutorial 'Tut 1 - Code walk through!' 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

About us

MMO-Society is the Forum former known as Lordcraft, MMO-Society offers various of Resources to help you evolve your knowledge within Emulation, In the process we make sure to form a strong community bond in-between each other and thus we are proud to can admit that MMO-Society is possibly the friendliest Community Forum to currently exist.

Useful Quick-Links

×
×
  • Create New...