Results 1 to 7 of 7
  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default How do you go about setting boundaries in a game?

    I am new to java games and so far I have created a nice game that has an image in the background and a character that you can move using the arrow keys. My problem is he goes right through the window and off the frame. I need to set "boundaries" at the edge of the frame so he can't leave. I don't expect you to code it all for me but at least give me some instruction on what to do?

    Here is my character class

    Java Code:
    package Game;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    
    public class Dude {
            int x, dx, y, dy;
            Image still;
    
            public Dude() {
                    ImageIcon i = new ImageIcon("C:/Users/dude.png");
                    still = i.getImage();
                    x = 10;
                    y = 172;
            }
    
            public void move() {
                    x = x + dx;
                    y = y + dy;
            }
    
            public int getX() {
                    return x;
            }
    
            public int getY() {
                    return y;
            }
    
            public Image getImage() {
                    return still;
            }
    
            public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_LEFT)
                        dx = -2;
    
                    if (key == KeyEvent.VK_RIGHT)
                        dx = 2;
    
                    if (key == KeyEvent.VK_UP)
                        dy = -2;
    
                    if (key == KeyEvent.VK_DOWN)
                        dy = 2;
            }
    
            public void keyReleased(KeyEvent e) {
                    int key = e.getKeyCode();
    
                    if (key == KeyEvent.VK_LEFT)
                        dx = 0;
    
                    if (key == KeyEvent.VK_RIGHT)
                        dx = 0;
    
                    if (key == KeyEvent.VK_UP)
                        dy = 2;
    
                    if (key == KeyEvent.VK_DOWN)
                        dy = 0;
    
                    if (key == KeyEvent.VK_S)
                        dy = 0;
            }
    }
    And the code for the board: painting it, displaying the background image and displaying the gui window.

    Java Code:
    package Game;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Board extends JPanel implements ActionListener {
            Dude p;
            public Image img;
            Timer time;
    
            public Board() {
                    p = new Dude();
                    addKeyListener(new AL());
                    setFocusable(true);
                    ImageIcon i = new ImageIcon("C:/Users/still.jpg");
                    img = i.getImage();
                    time = new Timer(5, this);
                    time.start();
            }
    
            public void actionPerformed(ActionEvent e) {
                    p.move();
                    repaint();
            }
    
            public void paint(Graphics g) {
                    super.paint(g);
                    Graphics2D g2d = (Graphics2D) g;
    
                    g2d.drawImage(img, 0, 0, null);
                    g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
            }
    
            private class AL extends KeyAdapter {
                    public void keyReleased(KeyEvent e) {
                            p.keyReleased(e);
                    }
    
                    public void keyPressed(KeyEvent e) {
                            p.keyPressed(e);
                    }
            }
    
            public static void main (String args[]) {
                JFrame frame = new JFrame();
                    frame.add(new Board());
                    frame.setTitle("2-D Game");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(1200,365);
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
            }
    }
    Oh and btw my background image is 1200x365 (same as my gui window).
    Last edited by robertbob; 05-10-2010 at 02:58 AM.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    A helper method for determining an out of bounds character would be the most appropriate solution to this problem:
    Java Code:
    public void move() {
         x = x + dx;
         y = y + dy;
         checkOOB();
    }
    
    private void checkOOB() {
      //check the x and y coordinates, if they are greater than 1200 or 365
      //(or less if you have a frame around the game area) or less than 0,
      //set them accordingly
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Hmm I tried:

    Java Code:
    package Game;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    
    public class Dude {
            int x, dx, y, dy;
            Image still;
    
            public Dude() {
                    ImageIcon i = new ImageIcon("C:/Users/dude.png");
                    still = i.getImage();
                    x = 10;
                    y = 172;
            }
    
            public void move() {
                    checkOOB();
            }
    
            public int getX() {
                    return x;
            }
    
            public int getY() {
                    return y;
            }
    
            public Image getImage() {
                    return still;
            }
    
            public void checkOOB() {
                x = x + dx;
                y = y + dy;
                if(dx <= 1200 || dx >= 0) {
                    move();
                } else if (dy <= 365 || dy >= 0) {
                    move();
                }
            }
    
            public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_LEFT)
                        dx = -2;
    
                    if (key == KeyEvent.VK_RIGHT)
                        dx = 2;
    
                    if (key == KeyEvent.VK_UP)
                        dy = -2;
    
                    if (key == KeyEvent.VK_DOWN)
                        dy = 2;
            }
    
            public void keyReleased(KeyEvent e) {
                    int key = e.getKeyCode();
    
                    if (key == KeyEvent.VK_LEFT)
                        dx = 0;
    
                    if (key == KeyEvent.VK_RIGHT)
                        dx = 0;
    
                    if (key == KeyEvent.VK_UP)
                        dy = 2;
    
                    if (key == KeyEvent.VK_DOWN)
                        dy = 0;
    
                    if (key == KeyEvent.VK_S)
                        dy = 0;
            }
    }
    But now he won't move and I get an error:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
            at Game.Dude.move(Dude.java:20)
            at Game.Dude.checkOOB(Dude.java:39)

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Your move() method just calles the checkOOB() method, and the checkOOB() method calles move(). Infinite loop of method calls. And also, you don't check the dx and dy values, but x and y. And checkOOB() should only check and if neccesary change the values of x and y, not call move.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Thanks I got it work now! +rep :)

    However my problem now is my guy will get to the edge of the image and stop, but as I hold down the arrow keys he will move very slowly and can go right through the boundary.

    new code:

    Java Code:
    package Game;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    
    public class Dude {
            int x, dx, y, dy;
            Image still;
    
            public Dude() {
                    ImageIcon i = new ImageIcon("C:/Users/dude.png");
                    still = i.getImage();
                    x = 1000;
                    y = 147;
            }
    
            public void move() {
                    checkOOB();
            }
    
            public int getX() {
                    return x;
            }
    
            public int getY() {
                    return y;
            }
    
            public Image getImage() {
                    return still;
            }
    
            public void checkOOB() {
                x = x + dx;
                y = y + dy;
                if (x >= 1110 || x <= 0) {
                    dx = 0;
                } else if (y <= 0 || y >= 147 ) {
                    dy = 0;
                } 
            }
    
            public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_LEFT)
                        dx = -1;
    
                    if (key == KeyEvent.VK_RIGHT)
                        dx = 1;
    
                    if (key == KeyEvent.VK_UP)
                        dy = -1;
    
                    if (key == KeyEvent.VK_DOWN)
                        dy = 1;
            }
    
            public void keyReleased(KeyEvent e) {
                    int key = e.getKeyCode();
    
                    if (key == KeyEvent.VK_LEFT)
                        dx = 0;
    
                    if (key == KeyEvent.VK_RIGHT)
                        dx = 0;
    
                    if (key == KeyEvent.VK_UP)
                        dy = 1;
    
                    if (key == KeyEvent.VK_DOWN)
                        dy = 0;
    
                    if (key == KeyEvent.VK_S)
                        dy = 0;
            }
    }

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Java Code:
    public void move() {
      x += dx;
      y += dy;
      checkOOB();
    }
    
    private void checkOOB() {
      if(x < 0) x = 0;
      if(x > 1200) x = 1200;
      if(y < 0) y = 0;
      if(y > 365) y = 365;
    }
    You were modifying the dx and dy values, if your character reached the edge, you'd set them to 0. The problem is, you incremented the values, and then didn't account for erraneous coordinates, as the dx and dy values change every time a KeyEvent fires. What I've written above is simple, just increment the values, and if they go over a limit, pull them back. Don't make life hard for yourself :D.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 09:06 AM
  3. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM
  4. Setting the DSN
    By tim in forum JDBC
    Replies: 1
    Last Post: 02-14-2008, 10:55 PM
  5. Help with setting up please
    By BlitzA in forum New To Java
    Replies: 6
    Last Post: 12-29-2007, 01:54 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
  •