PacMan Game Help Needed

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

PacMan Game Help Needed

Postposted on Fri May 13, 2011 4:41 pm

I am trying to make PacMan from strach in Java. I am going to have a lot of questions throughout so I am using this thread to do so :) .

My first question is: How do I set the "=" assignment for a class I made(Location class). So basically I can say
Code: Select all
Location loc = loc2 // loc2 is also a Location
. I want loc2 to contain the same x,y coordinates of loc. What method do I override to change how "=" reacts?

I could also use tips on improving my code.
Here is my code so far:

Location Class
Code: Select all
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 int getRow(){
      return row;
   }
   public int getCol(){
      return col;
   }
   
   public void set(int i, int x){
      row = i;
      col = x;
   }

}

Grid Class
Code: Select all
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.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(){
           return enemies;
        }
       
        public Pacman getPac()
        {
           return Player;
        }
       
        public static void move(int direction)
        {
           Location nw = new Location(getLocationInDirection(direction, Player.getLocation()));
           Location old = new Location(Player.getLocation());
           if(isValid(nw))
           {
              int num = (old.getRow() * 25) + old.getCol();
              removeIt(old);
              moveTo(nw);
              Player.setLocation(nw);
           }
              
        }
       
        public static void move(int direction, Ghost g)
        {
           Location nw = new Location(getLocationInDirection(direction, g.getLocation()));
           Location old = new Location(g.getLocation());
           if(isValid(nw))
           {
              int num = (old.getRow() * 25) + old.getCol();
              removeIt(old);
              pane.add(new JLabel(new ImageIcon("dot.gif")), num);
              moveTo(nw, g.index);
              enemies.get(g.index).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 moveTo(Location x)
        {
           //moves pacman to location
           for(int i = 0; i < food.size(); i++)
              if(food.get(i).getLocation().equals(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
           Ghost g = new Ghost();
           g.setLocation(x);
           g.setIndex(index);
           index++;
           enemies.add(g);
        }
       
        public static void removeIt(Location loc)
        {
           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;   
        }

       
       /* public ArrayList<Location> getNeighbors(Location loc)
        {
           ArrayList<Location> locs = new ArrayList<Location>();
           if(isValid(new Location(loc.getRow() -1, loc.getCol())))
              locs.add(new Location(loc.getRow() -1, loc.getCol()));
           if(isValid(new Location(loc.getRow() + 1, loc.getCol())))
              locs.add(new Location(loc.getRow() +1, loc.getCol()));
           if(isValid(new Location(loc.getRow(), loc.Col() + 1)))
              locs.add(new Location(loc.getRow(), loc.Col() + 1));
           if(isValid(new Location(loc.getRow(), loc.Col() - 1)))
              locs.add(new Location(loc.getRow(), loc.Col() - 1));

        }
        */
       
}


Players Class
Code: Select all
import javax.swing.*;
import java.awt.*;

public class Players{
   public Players(){
   
   }
}

PacMan Class
Code: Select all
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());
   }
      
}


Ghost Class
Code: Select all
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


public class Ghost extends Players
{
   private Location loc;
   public Ghost()
   {
   }
   
   public void setLocation(Location k)
   {
      loc.set(k.getRow(), k.getCol());
   }
      
   
   public void getLocation()
   {
      return loc;
   }
   
   public ImageIcon getImage()
   {
      return new ImageIcon("Ghost.gif");
   }
}


PacRunner Class
Code: Select all
public class PacRunner
{
   public static void main (String[] args)
   {
      Grid gr = new Grid();
      Pacman a = new Pacman();
      gr.add(new Ghost());
      gr.add(a, new Location(5,1));
      gr.move(Location.UP, a);
      gr.show();
   }
}


Mover Class **Ignore the act() method for now
Code: Select all
import java.util.ArrayList;
import java.util.EventObject;
import java.awt.event.KeyEvent;
import java.awt.Event;



public class Mover
{
   Grid test;
   public Mover(Grid gr)
   {
      test = gr;
   }
   
  public boolean keyUp(Event e, int key)
  {
    if (key == Event.LEFT) test.move(Location.LEFT);
    if (key == Event.RIGHT) test.move(Location.RIGHT);
    if (key == Event.UP) test.move(Location.UP);
    if (key == Event.DOWN) test.move(Location.DOWN);
    return true;
  }

   
   public void act(Grid gr)
   {
      while(true)
      {
         Pacman temp = new Pacman();
           temp.setLocation(gr.getPac().getLocation());
         for(Ghost x: gr.getActors()){
              int r1 = temp.getLocation().getRow(), r2 = x.getLocation().getRow(),
              c1 = temp.getLocation().getCol(), c2 = x.getLocation().getCol();
              if(r2 < r1)
              {
                 gr.move(Location.RIGHT, x);
              }
              if(c2 < c1)
              {
                 gr.move(Location.UP, x);
              }
              if(c2 > c1)
              {
                 gr.move(Location.DOWN, x);
              }
           
              if(r2 > r1)
              {
                 gr.move(Location.LEFT, x);
              }
         }
      }
   }
   
}


Thanks!
Last edited by Mondos on Wed May 18, 2011 6:50 pm, edited 2 times in total.
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
Mondos
Gerbil XP
 
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Re: PacMan Game Help Needed

Postposted on Fri May 13, 2011 8:53 pm

Modified GridWorld (AP CompSci thing)?

Anyways, quick look at the location thing made me think of a few things. If you're trying to create a new variable loc2 with the same attributes as loc you may want to avoid using an =. Typically this just tells the program to look at the same memory location for each variable, so when you modify one you end up changing both (though I may be wrong).
Personally, I'd try
Code: Select all
Location loc2 = new Location(loc.getRow(), loc.getCol());
'
You should also probably add the default constructor for the Location class as it's always a handy thing to have.
Dunno though. Didn't take much time to look at your code and didn't really thing too much on it, so my suggestions may not be useful at all.
Lenovo W520
IBM dx340
Nokia Lumia 928
Sony a7 with far too many lenses to list or even count
lonleyppl
Gerbil XP
 
Posts: 347
Joined: Wed Jan 26, 2011 2:59 pm

Re: PacMan Game Help Needed

Postposted on Fri May 13, 2011 9:52 pm

I was afraid that I would have to do that way. I guess I will, thanks.

I am using GridWorld as a reference but am trying to make it from scratch.
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
Mondos
Gerbil XP
 
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Re: PacMan Game Help Needed

Postposted on Sat May 14, 2011 9:41 am

Could always go with a secondary constructor to.
Code: Select all
public Location (Location loc) {
    row = loc.getRow();
    col = loc.getCol();
}

Then create new ones passing in the old location.
Code: Select all
Location loc2 = new Location (loc1);
Intel i7 860, Asus P7P55D Pro, 4x2GB Corsair XMS3 1600 (CMX4GX3M2A1600C9), EVGA GTX 560 Ti Superclocked
Seagate 7200.7 160GB, WD Caviar Black 640GB, WD Caviar Green 1TB, WD Caviar Green 2TB
Dell 2408WFP and Dell 2407WFP-HC for dual-24" goodness
emorgoch
Gerbil Elite
 
Posts: 686
Joined: Tue Mar 27, 2007 11:26 am
Location: Toronto, ON

Re: PacMan Game Help Needed

Postposted on Sun May 15, 2011 4:55 pm

Ok. So I have the move method working in Grid, it moves the pacman. But how do I bring it all together so I can move the pacman with the keyboard keys?

I just don't know which class to put the methods in.
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
Mondos
Gerbil XP
 
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Re: PacMan Game Help Needed

Postposted on Tue May 17, 2011 3:31 pm

As with most tasks... its best NOT to start from scratch.

A simple google search reveals multiple places where you could download existing pacman code... I would suggest using it as a base and modifying it. If thats not your style, then you could at least see how they did stuff and replicate it when you get stuck.
ShadowTiger
Gerbil First Class
Gold subscriber
 
 
Posts: 120
Joined: Wed Oct 01, 2008 2:39 pm

Re: PacMan Game Help Needed

Postposted on Wed May 18, 2011 6:53 pm

The only source code I could find was using an applet (i am using a GUI). I did manage to get some code from it though, for the key movement, though they aren't working. I put them in the new Mover class. I call it via PacRunner:
Code: Select all
public class PacRunner
{
   public static void main (String[] args)
   {
      
      Grid gr = new Grid();
      Pacman a = new Pacman();
      gr.addGhost(new Location(1,1));
      gr.moveTo(new Location(4,1));
      gr.moveTo(new Location(1,1), 0);
      Mover m = new Mover(gr);
      gr.show();
      //m.act(gr);

   }
}


I put the code for the moving int he KeyUp method in Mover. It just doesn't work, nothing happens. Help is much appreciated, really. :D
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
Mondos
Gerbil XP
 
Posts: 440
Joined: Thu May 22, 2008 4:33 pm


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 2 guests