Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to set boundaries/invisible walls in Slick 2D  (Read 371 times)
0 Members and 1 Guest are viewing this topic.
Offline streek405

Senior Newbie





« Posted 2014-01-16 02:22:36 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
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.
Offline streek405

Senior Newbie





« Reply #1 - Posted 2014-01-16 02:23:50 »

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

Junior Member


Medals: 1



« Reply #2 - Posted 2014-01-16 02:27:44 »

"[ code ] CODE GOES HERE [ /code ]"

**REMOVE Spaces inside of brackets**
**REMOVE quotes on outside**

1  
int test = 5;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Longarmx
« Reply #3 - Posted 2014-01-16 03:03:24 »

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
Rectangle
follow the player around. This will be the player's hit box. Then, you can use other rectangles where your walls are and call
playerHitBox.intersects(wallHitBox);
(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.

Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #4 - Posted 2014-01-16 04:51:05 »

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.

Offline streek405

Senior Newbie





« Reply #5 - Posted 2014-01-16 05:36:26 »

Ok, thanks Smiley
Offline streek405

Senior Newbie





« Reply #6 - Posted 2014-01-16 05:38:23 »

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
Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #7 - Posted 2014-01-16 05:53:13 »

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 Smiley

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (48 views)
2014-04-15 18:08:23

BurntPizza (44 views)
2014-04-15 03:46:01

UprightPath (60 views)
2014-04-14 17:39:50

UprightPath (42 views)
2014-04-14 17:35:47

Porlus (58 views)
2014-04-14 15:48:38

tom_mai78101 (81 views)
2014-04-10 04:04:31

BurntPizza (140 views)
2014-04-08 23:06:04

tom_mai78101 (240 views)
2014-04-05 13:34:39

trollwarrior1 (200 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!