How to set boundaries/invisible walls in Slick 2D

I am creating my very first game project, but I have no idea how to actually start the code for the boundaries. The game is pretty much an ordinary kid maze game which you are suppose to find your way from point A or point B. Right now I have 2 ideas on how to set the boundaries:
1: (If Java is smart enough), where ever there is black (the boundaries), the person cannot go past it. EASY WAY
2: Figure out the coordinates for all of the black lines and do not allow the person to go past any of it. HARD WAY

OR are none of these valid ways?

Here is my code right now for the play state


package gameOne;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class PlayFirstGame extends BasicGameState{

	Image maze;
	Image player;
	float x = 50;
	float y = 50;
	float speed = .25f;
	
	public PlayFirstGame(int state){
		
	}
	
	// load all fonts, graphics, sounds, etc
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
		maze = new Image("res1/maze3.jpg");
		player = new Image("res1/normalFace.png");
	}
	
	//draw stuff on the screen
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
		
		g.drawImage(maze, 82, 15);
		g.drawImage(player, x, y);
	}
	
	//animation and game logic(AI, user input, etc)
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
		
		//movement
		Input input = gc.getInput();

		//up
		if(input.isKeyDown(Input.KEY_UP)){
			y = y - speed * delta;//delta makes the speed run the same on ANY computer
		}
		//down
		if(input.isKeyDown(Input.KEY_DOWN)){
			y = y + speed * delta;
		}
		//left
		if(input.isKeyDown(Input.KEY_LEFT)){
			x = x - speed * delta;
		}
		//right
		if(input.isKeyDown(Input.KEY_RIGHT)){
			x = x + speed * delta;
		}
		
		// add foot prints as they move 
		
		//make foot prints disappear after X amount of steps
		
		//if they reach a dead end, failFace 
		
		//if they reach the end, winFace
	}
	
	public int getID(){
		return 1;//play is 1
	}
}


Pretty much this code is just the background, the player, and the ability to move the player around.

Can someone please tell my why my code wont display? I put it between the qr thing…

“[ code ] CODE GOES HERE [ /code ]”

REMOVE Spaces inside of brackets
REMOVE quotes on outside

int test = 5;

If your walls are rectangles (or squares) then you could use AABB (Axis-Aligned Bounding Box) to implement collision detection. An easy way to do this is to have a [icode]Rectangle[/icode] follow the player around. This will be the player’s hit box. Then, you can use other rectangles where your walls are and call [icode]playerHitBox.intersects(wallHitBox);[/icode] (both playerHitBox and wallHitBox are rectangles) to determine whether the player is inside the wall or not. Then, it’s a matter of not allowing your player to move in that direction any more.

For maze games, you might want to force the character to follow a very direct path instead of using the walls to determine boundaries. There are plenty of ways to do it, but some of the easiest ways I saw this implemented was a tile based system in where each tile has 4 walls representing North, South, East, and West. By having it tile based, even if the movement is crude, you can avoid having to worry about collision detection altogether by having each movement controlled by the position of the character, rather than the position off the objects.

Other than that, AABB collision methods are usually the way to go when it comes to pixel perfect movements like the above poster pointed out.

Ok, thanks :slight_smile:

How can I adjust the size of the boxes?
Also, could you give me an example of this code? I tried looking it up but all I found is this site
http://useflashpunk.net/basic/handling-collision.html
but I dont think its applicable for me.
EDIT Would this be sufficient enough?
http://www.java-gaming.org/index.php?topic=28059.0

Hmmm… it depends on how much you know (and which method you are using) but…

AABB Collision Detection

You can change the size of the boxes by changing the numbers of the bounding boxes. :wink:

EDIT: Yeah, you found the right page :slight_smile: