Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Pacman issue with walls

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Red face Pacman issue with walls

    Hi guys, so I've been making this java program to simulate a rather simple pacman, I've been doing fine until I reached the point where I can't figure out how to put a restriction on the pacman so he doesn't walk thru a wall. Here is my source code:


    Background.pngpacman.jpg

    import javax.swing.JFrame;
     
    public class Pac {
        public static void main(String args[]){ 
            Pacman p = new Pacman();
            JFrame f = new JFrame();
            f.add(p);
            f.setVisible(true);
            f.setSize(180, 258);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("Pacman");
        }
    }

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
     
    public class Pacman extends JPanel implements ActionListener, KeyListener {
     
        Timer t = new Timer(5, this);
        int x = 75, y = 80, velX = 0, velY = 0;
        private BufferedImage sheet;
        private BufferedImage sheet1;
        private int frame;
     
        public Pacman() {
            t.start();
            addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
        }
     
        @Override
        public void paintComponent(Graphics g) {
            try {
                sheet = ImageIO.read(Pacman.class.getResource("/images/Background.png"));
            } catch (IOException ex) {
                Logger.getLogger(Pacman.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                sheet1 = ImageIO.read(Pacman.class.getResource("/images/pacman.png"));
            } catch (IOException ex) {
                Logger.getLogger(Pacman.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (sheet != null) {
                g.drawImage(sheet, 0, 0, null);
            }
            g.drawImage(sheet1.getSubimage(629 + (frame / 3) * (15 + 2), 51, 15, 15), x, y, null);
     
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.GREEN);
            g2.fillRect(66, 98, 32, 24);
        }
     
        @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
            if (x < 7 || x > 143) {
                velX = 0;
            }
            if (y < 8 || y > 199) {
                velY = 0;
            }
     
            x += velX;
            y += velY;
        }
     
        public void up() {
            if (y > 199) {
                y = 199;
            }
            velY = -5;
            velX = 0;
        }
     
        public void down() {
            if (y < 8) {
                y = 8;
            }
            velY = 5;
            velX = 0;
        }
     
        public void left() {
            if (x > 143) {
                x = 143;
            }
            velX = -5;
            velY = 0;
        }
     
        public void right() {
            if (x < 7) {
                x = 7;
            }
            velX = 5;
            velY = 0;
        }
     
        public void walls() {
     
        }
     
        @Override
        public void keyPressed(KeyEvent e) {
     
            int code = e.getKeyCode();
            if (code == KeyEvent.VK_UP) {
                up();
            }
            if (code == KeyEvent.VK_DOWN) {
                down();
            }
            if (code == KeyEvent.VK_RIGHT) {
                right();
            }
            if (code == KeyEvent.VK_LEFT) {
                left();
            }
        }
     
        @Override
        public void keyTyped(KeyEvent e) {
        }
     
        @Override
        public void keyReleased(KeyEvent e) {
        }
    }

    I would like to get some help to figure out how to avoid the pacman from entering a wall. I have drawn a rectangle but how do I declare that the pacman's speed should change to O "velX=0 or velY=0" when it touches the wall?

    Say for the following rectangle
    g2.setColor(Color.GREEN);
    g2.fillRect(66, 98, 32, 24);
    what could I do in the method
    public void walls () {}
    Last edited by mightyking; November 25th, 2011 at 02:35 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Pacman issue with walls

    Take a glance at the methods in Component (Which JFrame extends) Especially getSize. Then look at Dimension if you do not know about it. Note that the left wall has an x of 0 and the top wall has a y of 0.

    Component (Java Platform SE 7 )
    Dimension (Java Platform SE 7 )

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pacman issue with walls

    Before you increment your pacman's position, check to see whether that position is in a wall. If it is, set the speed to zero.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pacman issue with walls

    I already have the outside wall limits completed, and it stops accordingly
    public void actionPerformed(ActionEvent e) {
            repaint();
            if (x < 7 || x > 143) {
                velX = 0;
            }
            if (y < 8 || y > 199) {
                velY = 0;
            }
     
            x += velX;
            y += velY;
        }

    however when I try to use the same type of code for a rectangle that is inside the outer walls, it doesn't work the same way.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pacman issue with walls

    Quote Originally Posted by KevinWorkman View Post
    Before you increment your pacman's position, check to see whether that position is in a wall. If it is, set the speed to zero.
    I have no idea how to go about doing that, I would guess that would go into the keyPressed method right?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pacman issue with walls

    Quote Originally Posted by mightyking View Post
    I have no idea how to go about doing that, I would guess that would go into the keyPressed method right?
    Eh, I suppose that's one way to do it. It's probably not the best way, but it's a simple start. But yeah, it would go wherever your logic for moving your pacman character is.

    I suggest you get out a piece of graph paper and draw a sample pacman maze. Write down the coordinates and sizes of each inner wall, and see if you can figure out a formula that determines whether a point is inside each wall.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pacman issue with walls

    Logic-wise im aware of what I need to do, but programing-wise I'm completely off track. Any aid?

    I've been trying and trying but none make any good progress

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pacman issue with walls

    I tried to change my actionPerformed method to this:

    @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
    // sets the limits of the outer walls and changes the pacmans velocity to 0 so it will stop.
            if (x < 7 || x > 143) {
                velX = 0;
            }
     
            if (y < 8 || y > 199) {
                velY = 0;
            }
     
            // attempt at making the center inner wall's limits so the pacmans velocity will change to 0
            for(x=66; x<98; x++){
                velX=0;
                for(y=98; y<122; y++)
                    velY=0;
            } 
     
            x += velX;
            y += velY;
        }

    however here, i don't see why it doesnt work, just makes my pacman stay still in one position, why? pacmanstuck.png
            // attempt at making the center inner wall's limits so the pacmans velocity will change to 0
            for(x=66; x<98; x++){
                velX=0;
                for(y=98; y<122; y++)
                    velY=0;
            }

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pacman issue with walls

    I am really not sure what you're expecting that for loop to do. You should check pacman's position against the walls with some if statements, not a for loop.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pacman issue with walls

    I got it working by the following method:

    @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
     
            //Pacman 
            if (x == 3 || x == 146) {
                velX = 0;
            }
            if (y == 3 || y == 202) {
                velY = 0;
            }
            //up
            if (((x >= 100 && x <= 145) || (x >= 4 && x <= 49)) && y == 76) {
                velY=0;
            }
            if (((x >= 100 && x <= 145) || (x >= 4 && x <= 49)) && y == 201) {
                velY=0;
            }
            if ((x >= 51 && x <= 98 ) && y == 76) {
                velY=0;
            }
            if ((x >= 51 && x <= 98 ) && y == 122) {
                velY=0;
            }
            if ((x >= 51 && x <= 98 ) && y == 170) {
                velY=0;
            }
            //down
            if (((x >= 100 && x <= 145) || (x >= 4 && x <= 49)) && y == 5) {
                velY=0;
            }
            if (((x >= 100 && x <= 145) || (x >= 4 && x <= 49)) && y == 131) {
                velY=0;
            }
            if ((x >= 51 && x <= 98 ) && y == 35) {
                velY = 0;
            }
            if ((x >= 51 && x <= 98 ) && y == 83) {
                velY = 0;
            }
            if ((x >= 51 && x <= 98 ) && y == 131) {
                velY = 0;
            }
            //left
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 201)) && x == 49) {
                velX = 0;
            }
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 201)) && x == 145) {
                velX = 0;
            }
            if ((y >= 35 && y <= 74 ) && x == 98) {
                velX = 0;
            }
            if ((y >= 83 && y <= 122 ) && x == 98) {
                velX = 0;
            }
            if ((y >= 131 && y <= 170 ) && x == 98) {
                velX = 0;
            }
            //right
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 200)) && x == 4) {
                velX = 0;
            }
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 200)) && x == 100) {
                velX = 0;
            }
            if ((y >= 35 && y <= 74 ) && x == 51) {
                velX = 0;
            }
            if ((y >= 83 && y <= 122 ) && x == 51) {
                velX = 0;
            }
            if ((y >= 131 && y <= 170 ) && x == 51) {
                velX = 0;
            }
     
            x += velX;
            y += velY;
        }
     
        public void up() {
            if (y == 202) {
                y = 201;
            }
            if(y == 131){
                y = 130;
            }
            if(y==35){
                y=34;
            }
            if(y==83){
                y=82;
            }
     
            if (((x >= 100 && x <= 145) || (x >= 4 && x <= 49)) && y == 76) {
                y = 77;
            }
            if (((x >= 100 && x <= 145) || (x >= 4 && x <= 49)) && y == 201) {
                y = 202;
            }
     
            if ((x >= 51 && x <= 98 ) && y == 76) {
                y = 77;
            }
            if ((x >= 51 && x <= 98 ) && y == 122) {
                y=123;
            }
            if ((x >= 51 && x <= 98 ) && y == 170) {
                y=171;
            }
     
            velY = -1;
            velX = 0;
        }
     
        public void down() {
            if (y == 3) {
                y = 4;
            }
            if(y==76){
                y=77;
            }
            if(y==122){
                y=123;
            }
            if(y==170){
                y=171;
            }
            if (((x >= 99 && x <= 144) || (x >= 3 && x <= 50)) && y == 5) {
                y = 4;
            }
            if (((x >= 99 && x <= 144) || (x >= 3 && x <= 50)) && y == 131) {
                y = 130;
            }
            if ((x >= 51 && x <= 98 ) && y == 35) {
                y = 34;
            }
            if ((x >= 51 && x <= 98 ) && y == 83) {
                y = 82;
            }
            if ((x >= 51 && x <= 98 ) && y == 131) {
                y = 130;
            }
            velY = 1;
            velX = 0;
        }
     
        public void left() {
            if (x == 146) {
                x = 145;
            }
            if(x==4){
                x=3;
            }
            if(x==51){
                x=50;
            }
            if(x==100){
                x=99;
            }
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 201)) && x == 49) {
                x = 50;
            }
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 201)) && x == 145) {
                x = 146;
            }
            if ((y >= 35 && y <= 74 ) && x == 98) {
                x = 99;
            }
            if ((y >= 83 && y <= 122 ) && x == 98) {
                x = 99;
            }
            if ((y >= 131 && y <= 170 ) && x == 98) {
                x = 99;
            }
     
            velX = -1;
            velY = 0;
        }
     
        public void right() {
            if (x == 3) {
                x = 4;
            }
            if(x==49){
                x=50;
            }
            if(x==98){
                x=99;
            }
            if(x==145){
                x=146;
            }
     
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 200)) && x == 4) {
                x = 3;
            }
            if (((y >= 5 && y <= 76) || (y >= 146 && y <= 200)) && x == 100) {
                x = 99;
            }
            if ((y >= 35 && y <= 74 ) && x == 51) {
                x = 50;
            }
            if ((y >= 83 && y <= 122 ) && x == 51) {
                x = 50;
            }
            if ((y >= 131 && y <= 170 ) && x == 51) {
                x = 50;
            }
     
            velX = 1;
            velY = 0;
        }

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Pacman issue with walls

    Gaaaaaah!

    Why not hava a collection of Wall objects. Then you loop over them checking to see if Pacman's co-ords are inside any of the walls. That way your code will not change should you change maps.
    Improving the world one idiot at a time!

  12. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pacman issue with walls

    Quote Originally Posted by Junky View Post
    Gaaaaaah!

    Why not hava a collection of Wall objects. Then you loop over them checking to see if Pacman's co-ords are inside any of the walls. That way your code will not change should you change maps.
    Yes I know this is hardly the most efficient way, but at least for now I got a working pacman

    I will however strive to work it out much better so I could, as you say change the maps to my liking without having to change the code and all the coordinates.

Similar Threads

  1. Making a pacman game having some problems with drawing
    By Matta in forum What's Wrong With My Code?
    Replies: 22
    Last Post: June 9th, 2011, 06:18 PM
  2. Possible threading issue
    By Kerr in forum Threads
    Replies: 3
    Last Post: March 6th, 2011, 05:24 PM
  3. Polymorphism issue
    By LDM91 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 03:26 PM
  4. JButton issue, please help
    By Khoatic in forum AWT / Java Swing
    Replies: 3
    Last Post: November 19th, 2010, 11:19 PM
  5. [SOLVED] Calendar Issue
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2010, 01:19 PM

Tags for this Thread