Results 1 to 7 of 7
  1. #1
    tobial is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Question Making a 2d game, need help with border around the screen

    Ok, so I and a friend is making a small java game. Currently it' sonly a little man moving across whole the screen(up, down, left and right).
    I won’t to know what the best way to have the character not moving outside some borders will be? So he couldn't like walk over a house.
    Currently I check if the player is outside the screen when i update the position, what will be the easiest way to have like boxes on the screen that he couldn’t go inside?

    Thanks

    EDIT: Corrected typos.
    Last edited by tobial; 11-13-2011 at 03:43 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Making a 2d game, need help with border around the screen

    Hello and welcome to the forum.

    Please if you get a chance, edit your original post to correct typos and spelling mistakes. I've found that if posts are easier to read, more folks will read them -- which is what you want, right?

    Regarding your problem, this is a problem of your program logic. You'll need to have a non-gui representation of boundaries and obstacles and a non-GUI representation of the little man's position, and use this to determine where the man can and can't go. The specifics will depend on your program's structure.

  3. #3
    tobial is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Making a 2d game, need help with border around the screen

    Ok, fixed typos =).
    For the problem; How would the best way to save the obstacles be?
    currently for the screenborders i have:
    Java Code:
    		if(Main.Busthus.getX() + Main.Busthus.getWidth()+1 >= s.getWidth() && m.right){
    			Main.Busthus.setMoving(false);
    			m.right = false;
    		}
    		if(Main.Busthus.getX()-1 <= 0 && m.left){
    			Main.Busthus.setMoving(false);
    			m.left = false;	
    		}																		
    		if(Main.Busthus.getY()-1 <= 0 && m.up){									
    			Main.Busthus.setMoving(false);									
    			m.up = false;														
    		}																	
    		if(Main.Busthus.getY() + Main.Busthus.getHeight()+1 >= s.getHeight() && m.down){
    			Main.Busthus.setMoving(false);										
    			m.down = false;														
    		}
    s is my screen, Busthus is the little man,m and Main is my mainclass.
    Last edited by tobial; 11-13-2011 at 04:00 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Making a 2d game, need help with border around the screen

    Quote Originally Posted by tobial View Post
    Ok, fixed typos =).
    For the problem; How would the best way to save the obstacles be?
    I don't know of any "best way", but when I've done something like this before, I've used a simple text file with letters to represent the grid such as g for grass and h for house, and then have the program read this into an array. Then when moving the man, check his position relative to my data array to see if he's trying to go somewhere there's an obstacle, and if so, not allow it.

  5. #5
    tobial is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Making a 2d game, need help with border around the screen

    aha, so putting every pixle of the obstacle in a array? And checking if the mans position is in the array?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Making a 2d game, need help with border around the screen

    Quote Originally Posted by tobial View Post
    aha, so putting every pixle of the obstacle in a array? And checking if the mans position is in the array?
    Again, this is just one way. You also could have an ArrayList of Obstacle type, a class that describes where an obstacle is located and that has a method boolean contains(Point p), or something similar, and then simply iterate through this list when moving your man. There are many ways to skin this cat.

  7. #7
    tobial is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Making a 2d game, need help with border around the screen

    Ok, i'll try that

    I will try to have a class for House. and in the class iwill have a arraylist with points, but it it any way to automaticly fintd the points between 10:10 and let say 30:45?
    Last edited by tobial; 11-13-2011 at 04:05 PM.

Similar Threads

  1. Making a screen to draw on
    By sgthale in forum New To Java
    Replies: 9
    Last Post: 09-29-2011, 03:51 AM
  2. Loading Game Screen.
    By Alerhau in forum New To Java
    Replies: 3
    Last Post: 09-04-2011, 03:02 PM
  3. Chess game selection screen
    By gruffyddd in forum AWT / Swing
    Replies: 1
    Last Post: 03-19-2011, 03:27 AM
  4. Making more than one reports to screen with list
    By aborgeld in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2011, 11:22 PM
  5. Game making help
    By Mrkantaloupe in forum New To Java
    Replies: 2
    Last Post: 07-13-2010, 10:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •