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 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!