2 Replies - 1814 Views - Last Post: 08 October 2012 - 12:49 PM Rate Topic: -----

#1 Arctos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 04-November 11

Tile-Based Games!

Posted 08 October 2012 - 10:09 AM

Note: First of all, I know this is a relatively common question, but I'm struggling to find a definitive answer. I have checked a lot of sources, and I've been experimenting for a few weeks now, but I still cannot get any tiles to draw on-screen.

So, I've had an idea in my head for a while now for a small and simple-ish top-down puzzle game, using the Slick 2d library. In the game, you're trying to break out of a jail cell, but walls block your exit. These walls are attached to pressure plates, and you have to stand on them to make the walls move. I've included a basic idea below (Paint ftw):

Posted Image


As I understand it, I need a class to store my images, a class with all of the levels stored in arrays, and another class with the method with which to draw all of the tiles on the screen. I have got all of this, but at the minute I'm getting a NullPointerException ... Not sure why. My code is all here:

public class Map {
	public static int[] level1 = {1,1,1,1,1,1,1,1,1,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,0,0,0,0,0,0,0,0,1,
								  1,1,1,1,1,1,1,1,1,1};
	
	public static int[] level2 = {1,0,1,0,1,0,1,0,1,0,
								  0,1,0,1,0,1,0,1,0,1,
								  1,0,1,0,1,0,1,0,1,0,
								  0,1,0,1,0,1,0,1,0,1,
								  1,0,1,0,1,0,1,0,1,0,
								  0,1,0,1,0,1,0,1,0,1,
								  1,0,1,0,1,0,1,0,1,0,
								  0,1,0,1,0,1,0,1,0,1,
								  1,0,1,0,1,0,1,0,1,0,
								  0,1,0,1,0,1,0,1,0,1,};
}


import org.newdawn.slick.Game;
import org.newdawn.slick.Graphics;

public class Level {
	private int[] map;
	private Tiles[] tiles;
	private int x, y;
	private int mapSize;
	public static final float IMG_WIDTH = 60 * GameMain.SCALE, IMG_HEIGHT = 60 * GameMain.SCALE;
	protected float lolx, loly;

	public Level(int[] map){
		this.map = map;
		mapSize = map.length;
		tiles = new Tiles[mapSize];
	}

	private void loadMap(){
		x = 0;
		y = 0;
		for(int i = 0; i < mapSize; i++){
			tiles[i] = new Tiles(map[i], x, y);
			x += (IMG_WIDTH);

			if(x >= GameMain.WIDTH){
				x = 0;
				y += (IMG_HEIGHT);
			}
		}
	}
	public void draw(Graphics g){
		g.drawImage(Tiles.tileImg, x, y, IMG_HEIGHT, IMG_WIDTH, 0, 0, null);
	}
	public Tiles[] getTiles(){
		return tiles;
	}
}


import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Tiles{
	
	public static final int BLACK = 0, RED = 1;
	static Image tileImg;
	
	public void Tile(final int id, int x, int y) throws SlickException{
		switch(id){
		case BLACK:
			tileImg = new Image("res/cunt.png");
			
		case RED:
			tileImg = new Image("res/twat.png");
				
		}
	}

	public Tiles(int i, int x2, int y2) {
	}
}



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 World extends BasicGameState{
	private Level[] levels = new Level[2];
	public static int currentLevel;
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
		
	}
	public World(int currentLevel2){
		currentLevel = 0;
		levels[0] = new Level(Map.level1);
		levels[1] = new Level(Map.level2);
	}
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
		
	}
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{ 
		levels[currentLevel].draw(g);
	}
	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return 0;
	}
	
}



And I'm getting this error message:

java.lang.NullPointerException
	at org.newdawn.slick.Graphics.drawImage(Graphics.java:1640)
	at george.game.cells.Level.draw(Level.java:34)
	at george.game.cells.World.render(World.java:26)
	at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
	at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:681)
	at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
	at george.game.cells.GameMain.main(GameMain.java:26)
Mon Oct 08 17:56:06 BST 2012 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
	at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:684)
	at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
	at george.game.cells.GameMain.main(GameMain.java:26)


So, I need to fix this, and then I need to work out how to make the walls move when you stand on a tile ... But firstly I'd just like to be able to draw the tiles :P

Thankyou!

Is This A Good Question/Topic? 0
  • +

Replies To: Tile-Based Games!

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Tile-Based Games!

Posted 08 October 2012 - 10:39 AM

Are you sure your image path is correct? Your images should be in the folder with your .class files and /res/
Was This Post Helpful? 0
  • +
  • -

#3 Arctos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 04-November 11

Re: Tile-Based Games!

Posted 08 October 2012 - 12:49 PM

Yep, all is in order there!

Though I probably should have changed the names of the images before posting on here.

Thanks though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1