Results 1 to 15 of 15
  1. #1
    Mondos is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default Making PacMan game, need help!

    I am attempting to create pacman from scratch in java. I am using a GUI to do so. My program feels like a mess however, and I still need to work on some major components.

    I am making this thread because I will most likely be having problems during the process of making the game and I can use this to periodically ask questions:o.

    My current problem: getting the keylistener to work. I am running the keylistener methods in PacRunner. I have the key bindings set to the arrow keys, so the arrow key corresponds to the appropriate pacman move. As of now nothing happens.


    Here's my code:
    Grid Class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.ArrayList;
    
    public class Grid{
    	private static ArrayList<Ghost> enemies;
    	private static Pacman Player;
    	private static ArrayList<Dot> food;
    	private static Container pane;
    	private static int index;
    	private JFrame game;
    	public Grid()
    		{
    			enemies = new ArrayList<Ghost>();
    			game = new JFrame();
    			game.setFocusable(true);
    			game.setResizable(false);
    			game.setTitle("Pac-Man 2.0");
    			game.setPreferredSize(new Dimension(1000, 450));
    			game.setBackground(Color.BLACK);
    			food = new ArrayList<Dot>();
    			Player = new Pacman();
    			int row = 10, col = 25;
    			index = 0;
    			game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    			pane = game.getContentPane();
    			pane.setBackground(Color.white);
                pane.setLayout(new GridLayout(row, col, 0, 0));
                for(int i = 0; i < row; i++)
                {
                	for(int a = 0; a < col; a++)
                	{
                	if(getGrid()[i][a] == 0){
                	   //Adding Dot Images to Grid
                	   Dot d = new Dot();
                	   d.setLocation(new Location(i, a));
                	   food.add(d);
                       pane.add(new JLabel(d.getImage()));
                	}
                	
                	         	
                   	else
                   		//Adding Boundery Images to Grid
                		pane.add(new JLabel(new ImageIcon("Wall.gif")));
                	            	
                   	}
                	
                }
                game.pack(); 
                
            }
            
            private static int[][] getGrid(){
            	//Creating Layout of grid
            	int[][] gr = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    						  {1,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
    				 		  {1,0,1,1,1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1},
    				 		  {1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,1},
    				 		  {1,0,1,1,1,0,1,1,0,0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1},
    				 		  {1,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1},
    				 		  {1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,1,1,1,1,0,1,0,1},
    				 		  {1,0,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1},
    						  {1,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
    				 		  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
            	return gr;
            }
            
            public ArrayList<Ghost> getActors(){
            	//Returns array of ghosts
            	return enemies;
            }
            
            public Pacman getPac()
            {
            	//Returns pacman
            	return Player;
            }
            
            public static void move(int direction)
            {
            	//Move method for pacman
            	Location nw = new Location(getLocationInDirection(direction, Player.getLocation()));
            	Location old = new Location(Player.getLocation());
            	if(isValid(nw))
            	{
            		replace(old);
            		movePac(nw);
            		Player.setLocation(nw);
            	}
            		
            }
            
            public static void move(int direction, int g)
            {
            	//Move method for Ghosts
            	Location nw = new Location(getLocationInDirection(direction, enemies.get(g).getLocation()));
            	Location old = new Location(enemies.get(g).getLocation());
            	if(isValid(nw))
            	{
            		int num = (old.getRow() * 25) + old.getCol();
            		boolean found = false;
            		for(int i = 0; i < food.size(); i++){
            			if(food.get(i).getLocation().isEqual(old)){
            				pane.remove(num);
            				pane.add(new JLabel(new ImageIcon("dot.gif")), num);
            				found = true;
            			}
                 			
            		}
            		if(!found) replace(old);
            			
            		moveTo(nw, g);
            		enemies.get(g).setLocation(nw);
            	}
                	
            }
            
            private static boolean isValid(Location loc)
            {
            	//Checks if location is a wall
            	int r = loc.getRow(), c = loc.getCol();
            	if(getGrid()[r][c] == 0) return true;
    
            	return false;
            }
            
            public static void movePac(Location x)
            {
            	//moves pacman to location
            	for(int i = 0; i < food.size(); i++)
            		if(food.get(i).getLocation().isEqual(x))
            			food.remove(i);
            	if(isValid(x))
            	{
           		int num = (x.getRow() * 25) + x.getCol();
            	pane.remove(num);
            	pane.add(new JLabel(Player.getImage()), num);
            	Player.setLocation(x);
    			}
    
            }
            
            public static void moveTo(Location x, int value)
            {
            	//Moves ghost to location
            	if(isValid(x))
            	{
           		int num = (x.getRow() * 25) + x.getCol();
           		pane.remove(num);
            	pane.add(new JLabel(enemies.get(value).getImage()), num);
            	enemies.get(value).setLocation(x);
    			}
            }
            
            public static void addGhost(Location x)
            {
            	//Adds Ghost into game
            	if(isValid(x)){
            	Ghost g = new Ghost();
            	int num = (x.getRow() * 25) + x.getCol();
           		pane.remove(num);
            	pane.add(new JLabel(g.getImage()), num);
            	g.setLocation(x);
            	g.setIndex(index);
            	enemies.add(g);
            	index++;
            	}
            	
            }
            
            public static void replace(Location loc)
            {
            	//Replaces spot on grid with floor piece
            	int num = (loc.getRow() * 25) + loc.getCol();
            	pane.remove(num);
            	pane.add(new JLabel(new ImageIcon("Floor.gif")), num);
            }
            
            public void show()
            {
            	//Displays Grid
            	game.setVisible(true); 
            }
            
            public static Location getLocationInDirection(int d, Location b)
            {
            	if(d == Location.RIGHT)
            		return new Location(b.getRow(), b.getCol() + 1);
            	else if(d == Location.LEFT)
            		return new Location(b.getRow(), b.getCol() - 1);
            	else if(d == Location.UP)
            		return new Location(b.getRow() - 1, b.getCol());
            	else if(d == Location.DOWN)
            		return new Location(b.getRow() + 1, b.getCol());
            	else return null;	
            }
            
    
    
    
            
    }
    PacRunner
    Java Code:
    import java.awt.event.*;
    import java.awt.event.KeyEvent;
    
    public class PacRunner implements KeyListener
    {
    		private static Grid gr;
    		public static void main (String[] args)
    		{	
    		gr = new Grid();
    		gr.addGhost(new Location(1,1));
    		gr.movePac(new Location(4,1));
    		gr.show();
    		}
    	
    		public void keyPressed(KeyEvent e)
        	{
            	if(e.getKeyCode() == 38) gr.move(Location.UP);     		
            	else if(e.getKeyCode() == 40) gr.move(Location.DOWN);
            	else if(e.getKeyCode() == 37) gr.move(Location.LEFT);
            	else if(e.getKeyCode() == 39) gr.move(Location.RIGHT);
            	
        	}
    
        
            public void keyReleased(KeyEvent e)
            {
            	
            }
            
            public void keyTyped(KeyEvent e)
            {
            
            }
    }
    Players
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Players{
    	private Location loc;
    	public Players(){
    	
    	}
    	
    	public Location getLocation(){
    		return loc;
    	}
    	
    	public void setLocation(Location k)
    	{
    		loc.set(k.getRow(), k.getCol());
    	}
    		
    }
    Pacman
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Pacman extends Players
    {
    	private Location loc;
    	private int life;
    	public Pacman()
    	{
    		life = 0;
    		loc = new Location(-1,-1);
    	}
    	
    	public ImageIcon getImage()
    	{
    		return new ImageIcon("pac.png");
    	}
    	
    
    	public Location getLocation(){
    		return loc;
    	}
    	
    	public void setLocation(Location k)
    	{
    		loc.set(k.getRow(), k.getCol());
    	}
    		
    }
    Location
    Java Code:
    public class Location{
    	private int row, col;
    	public static final int RIGHT = 0, LEFT = 180, UP = 90, DOWN = 270;
    	
    	public Location(int r, int c){
    		row = r;
    		col = c;
    	}
    	
    	public Location (Location loc) {
        	row = loc.getRow();
        	col = loc.getCol();
    	}
    	
    	public boolean isEqual(Location c)
    	{
    		if(row == c.getRow() && col == c.getCol())
    			return true;
    		return false;
    	}
    	public int getRow(){
    		return row;
    	}
    	public int getCol(){
    		return col;
    	}
    	
    	public void set(int i, int x){
    		row = i;
    		col = x;
    	}
    
    }
    Ghost
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    
    public class Ghost extends Players
    {
    	private Location loc;
    	public int index;
    	public Ghost()
    	{
    		loc = new Location(-1,-1);
    	}
    	
    	public void setLocation(Location k)
    	{
    		loc.set(k.getRow(), k.getCol());
    	}
    	
    	public void setIndex(int i)
    	{
    		index = i;
    	}	
    	
    	public Location getLocation()
    	{
    		return loc;
    	}
    	
    	public ImageIcon getImage()
    	{
    		return new ImageIcon("Ghost.png");
    	}
    }
    Dot
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Dot extends Players
    {
    	private Location loc;
    	public Dot(){
    		
    	}
    	
    	public void setLocation(Location k)
    	{
    		loc = new Location(k);
    	}
    		
    	
    	public Location getLocation()
    	{
    		return loc;
    	}
    	
    	public ImageIcon getImage()
    	{
    		return new ImageIcon("dot.gif");
    	}
    }
    I could also use tips on improving my already existing code. Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    You haven't set the KeyListener for the frame. See the addKeyListener method. There is no reason for the JVM to call the keyPressed method.
    You need some work on redoing the layout after removing an image. See the validate method.

  3. #3
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default I have two things to say.

    First off I would recommend putting this instead.

    Java Code:
    if(e,getKeyCode() == KeyEvent.VK_LEFT)
    Second the original PacMan did not have all the ghosts follow the player. Instead only the red one follows the player. The blue ghost tries to get to a point that's exactly thirty-two pixels in front of the player and the green ghost tries to go to a similar spot as the blue. The yellow ghost just bounces around randomly and never tries to go to a set point like the rest.
    <Daniel>P.</Berry>

  4. #4
    Mondos is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Wow thanks it's working now! ;)

  5. #5
    Mondos is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    New problem: How can I make a timer so it will make a variable true until the timer goes on for a period of time, then the variable would be set false. I have tried the method I used for delaying the ghosts movement but it just pauses the entire game.

    This is for making the ghosts eatable and blue, like in pacman.

    Thanks

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    Can you make a small simple program to demonstrate your problem using the Timer class.
    Not sure how using a Timer would pause the entire game.

    a guess would be: Is your problem with the scope of the boolean?

  7. #7
    Mondos is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Well, I just said timer. I am really using this weird Thread.sleep() typed deal. I have no clue how to use a timer and google hasn't helped much.

    :D

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    Look in the Java tutorial for examples:
    The Really Big Index

  9. #9
    Mondos is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the quick response.

    Java Code:
    //Method called when supermode is set to true
    timer = new Timer(7000, this);
    timer.start();
    supermode = false;
    So would this wait 7 seconds and then execute the code following it in the method?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    No, the last statement would be executed immediately.
    Go back and read the tutorial again. The timer calls an ActionListener when it expires.

  11. #11
    rdjava's Avatar
    rdjava is offline Member
    Join Date
    Jan 2011
    Posts
    38
    Rep Power
    0

    Default

    I never really liked using timers. You can also create your own Thread that runs parallel to the main thread, kinda like multi-tasking. But Thread.sleep(1000) is what i use since thats pretty much what the timer is doing really.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    For some, very limited, definitions of 'pretty much'.

    A Swing Timer encapsulates more functionality, primarily in that its actionPerformed is invoked on the EDT, and that all Swing Timers use a common background Thread for their timing. Then there's the facility to setRepeats and setCoalesce -- the latter is particularly significant.

    db

  13. #13
    Mondos is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Does anyone know a simple way to add sound effects. All the tutorials I have looked up are so complicated and don't seem to work for me. Isn't there a simple playsound(wav file) class?

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    See the API for the static method in Applet that creates an AudioClip.

    db

  15. #15
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default Sounds

    Heres a simple code that will play two types of sound files. .wav and .au

    Java Code:
                           try {
                                    String fileLocation = "";
    				File bounceFile = new File(fileLocation);
    				URL bounceURL = bounceFile.toURI().toURL();
    				AudioClip clip = Applet.newAudioClip(bounceURL);
    				clip.play();
                          }catch(Exception e) {}

    Plug this into your code and place the file path in the fileLocation variable. Whenever the code runs it will play the audio file. Remember only .au and .wav can be used.

    clip.play() can be replaced by clip.loop(). This will loop the sound file over and over until the clip.stop() method is run.
    <Daniel>P.</Berry>

Similar Threads

  1. Making a game restart? :/
    By AndroidAppNewbie in forum New To Java
    Replies: 2
    Last Post: 02-19-2011, 11:11 AM
  2. Making a puzzle game, need help!
    By Atriamax in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 01:55 AM
  3. Game making help
    By Mrkantaloupe in forum New To Java
    Replies: 2
    Last Post: 07-13-2010, 11:15 PM
  4. Huge problem. PacMan game.
    By Bullfrog in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 03:39 PM
  5. need help with pacman game!!
    By Newbie_Javaer in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 03:44 PM

Tags for this Thread

Posting Permissions

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