Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java programming language . please do not copy and paste the code from Zetcode.

ID: 3705204 • Letter: J

Question

java programming language .

please do not copy and paste the code from Zetcode.

I want some new code for pacman motion and ghosts motion. Help is appreciated. Thank you!!

Desian your own version of pacman gdme using swioeYour version should deature the core me chantc from the original uith atleast one additional leature of our own destgn. Your do not need uegest do not mement Com puter 0 an players eq Reverst betuveen tuo h plders Rot take tuns. Your gome on needs to have one level that highlights all uman your features However, shoold he fa avable reo aphics Cimag es) or make uown (images or b painlyng. Tncludeo sound effects too

Explanation / Answer

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:

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");
}
}

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;
}
}

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");
}
}