User Tools

Site Tools


packperson

Chase's Pack Person Game

PacMaze.jar

Move Monster

MazeMaker

Frogger

packman.gif

Executable Jar file that tests the Cell, Level, and PackMan classes

Cell.java
import java.awt.*;
 
public class Cell
{
    public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;
 
    public static final int GAP = 5;
 
    private boolean[] canExit;// NSEW true if an exit
    private boolean visited;
    private int x, y, size;
 
    public Cell (int x, int y , int size)
    {
        this.x = x;
        this.y = y;
        this.size = size;
        canExit = new boolean[4];
        visited = false;
        setAllExits(true);    
    }
 
    public boolean canExit(int dir){
        return canExit[dir];
    }
 
    public void noExit(int dir) {
        canExit[dir] = false;
    }
 
    public void exit(int dir) {
        canExit[dir] = true;
    }
 
    public void setAllExits(boolean b){
        for(int i = 0; i < canExit.length; i++)
            canExit[i] = b;
    }
 
    public int getX(){
        return x+size/2; //center
    }
 
    public int getY(){
        return y+size/2;
    }
 
    public boolean beenVisited(){
        return visited;
    }
 
    public void setVisited(boolean v)
    {
        visited = v;
    }
 
    public boolean contains(int x0, int y0){
        return (x0 >= x) && (x0 <= x + size) &&
        (y0 >= y) && (y0 <= y + size);
    }
 
    public void draw(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, size, size);
        if( !visited){
            g.setColor(Color.YELLOW);
            g.fillRect(x+size/2, y+size/2, GAP,GAP);
        }
        g.setColor(Color.MAGENTA.darker());
        if(!canExit[NORTH]){
            g.fillRect(x, y, size, GAP);
        }
        if(!canExit[SOUTH]){
            g.fillRect(x, y+size-GAP, size, GAP);
        }
        if(!canExit[EAST]){
            g.fillRect(x+size-GAP, y, GAP, size);
        }
        if(!canExit[WEST]){
            g.fillRect(x, y, GAP, size);
        }
    }
 
    public String toString(){
        String result="";
        result += "("+x +", "+y+") Exits: ";
        if(canExit[Cell.NORTH]) 
            result += "N ";
        if(canExit[Cell.SOUTH]) 
            result += "S ";
        if(canExit[Cell.EAST]) 
            result += "E ";
        if(canExit[Cell.WEST]) 
            result += "W ";
        return result;
    }
}
Level.java
import java.awt.*;
 
public class Level
{
    private Cell[][] grid;
    private int rows, cols, top, left, size;
    public Level(int rows, int cols, int top, int left, int size)
    {
        this.rows = rows;
        this.cols = cols;
        this.top = top;
        this.left = left;
        this.size = size;
        grid = new Cell[rows][cols];
        for(int r=0; r<rows; r++)
            for(int c = 0; c < cols; c++){
                grid[r][c] = new Cell(left+c*size, top+r*size ,size);
            }
        for(int c =0; c < cols; c++){
            grid[0][c].noExit(Cell.NORTH);
            grid[rows-1][c].noExit(Cell.SOUTH);
        }
        for(int r=0; r < rows; r++){
            grid[r][0].noExit(Cell.WEST);
            grid[r][cols-1].noExit(Cell.EAST);
        }
    }
 
    public void noExit(int r, int c, int dir)
    {
        grid[r][c].noExit(dir);
    }
 
    public int getXbyCol(int col){
        return grid[0][col].getX();
    }
 
    public int getYbyRow(int row){
        return grid[row][0].getY();
    }
 
    public int getSize(){
        return size;
    }
 
    public Cell getCell(int x, int y){
        for(Cell[] row:grid)
            for(Cell c : row)
                if (c.contains(x,y))
                    return c;
        return null;               
    }
 
    /**
     * CountCrumbs returns the number of unvisited cells
     */
 
    public int crumbsLeft(){
        int n=0;
        for(Cell[] row:grid)
            for(Cell c : row)
                if (!c.beenVisited())
                    n++;
        return n;
 
    }
 
    public void draw(Graphics g)
    {
        for(Cell[] row:grid)
            for(Cell c : row)
                c.draw(g);
    }
}
PackMan.java
import java.awt.*;
 
public class PackMan
{
    // instance variables - replace the example below with your own
    private int x, y, size, direction;
    private Level lev;
    private Cell myCell;
    private boolean isOpen;
    public static final int SPEED = 10;
    /**
     * Constructor for objects of class PackMan
     */
    public PackMan(Level lev, int row, int col)
    {
        this.lev = lev;
        this.size = lev.getSize()-10;
        this.x = lev.getXbyCol(col);
        this.y = lev.getYbyRow(row);
        this.myCell = lev.getCell(x,y);
        isOpen = false;
        direction = Cell.SOUTH;
        lev.getCell(x, y).setVisited(true);
    }
 
    public void setDirection(int dir)
    {
        direction = dir;
    }
    public void changeMouth()
    {
        isOpen = !isOpen;
    }
    public void move(){
        int nextX = this.x;
        int nextY = this.y;
        if (direction == Cell.NORTH)
            nextY = y - SPEED;
        else if (direction == Cell.SOUTH)
            nextY = y + SPEED;
        else if (direction == Cell.EAST)
            nextX = x + SPEED;
        else if (direction == Cell.WEST)
            nextX = x - SPEED;
        Cell nextCell = lev.getCell(nextX, nextY);  
        if (myCell.canExit(direction))
        {
            this.x = nextX;
            this.y = nextY;
            this.myCell.setVisited(true);
        }
        if (myCell != nextCell){
            myCell = nextCell;
            this.x = myCell.getX();
            this.y = myCell.getY();
        }
 
 
    }
    public Cell getCell(){ return myCell; }
    public int getDirection() {return direction;}
    public void draw(Graphics g)
    {
        g.setColor(Color.YELLOW);
        int x0 = x - size/2;
        int y0 = y - size/2;
        if( isOpen )
           g.fillOval(x0,y0,size,size);
        else if (direction == Cell.EAST)
        {
             g.fillArc(x0,y0,size,size, 45,270 );
        }else if (direction == Cell.NORTH)
        {
             g.fillArc(x0,y0,size,size, 135,270 );
        }else if (direction == Cell.WEST)
        {
             g.fillArc(x0,y0,size,size, 225,270 );
        }else if (direction == Cell.SOUTH)
        {
             g.fillArc(x0,y0,size,size, -45,270 );
        }
 
    }
    public String toString(){
        String result = "Pacman: ("+x+", "+y+") facing ";
        if(direction == Cell.NORTH) 
            result += "N ";
        if(direction == Cell.SOUTH) 
            result += "S ";
        if(direction == Cell.EAST) 
            result += "E ";
        if(direction == Cell.WEST) 
            result += "W ";
        result += myCell.toString()+ " can ";
        if (!myCell.canExit(direction))
            result += "NOT ";
        result += "exit.";    
        return result;
    }
}
LevelTester.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
 
public class LevelTester extends JPanel implements KeyListener, ActionListener, MouseListener
{
    public static int WIDTH=800;
    public static int HEIGHT=600;
    public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;
    private PackMan pm;
    private Level lev1;
    private Timer chewTimer, moveTimer;
    private String message, message2;
    private int moveCnt;
 
    public LevelTester(){
        int size =50;
        lev1 = new Level(3, 7,  100,  size,  size);
        lev1.noExit(0,1,SOUTH);
        lev1.noExit(0,2,SOUTH);
        lev1.noExit(0,5,SOUTH);
        lev1.noExit(1,0,EAST);
        lev1.noExit(1,1,WEST);
        lev1.noExit(1,1,NORTH);
        lev1.noExit(1,2,NORTH);
        lev1.noExit(1,2,SOUTH);
        lev1.noExit(1,3,EAST);
        lev1.noExit(1,3,SOUTH);
        lev1.noExit(1,4,WEST);
        lev1.noExit(1,4,SOUTH);
        lev1.noExit(1,5,NORTH);
        lev1.noExit(1,5,EAST);
        lev1.noExit(1,6,WEST);
        lev1.noExit(2,2,NORTH);
        lev1.noExit(2,3,NORTH);
        lev1.noExit(2,4,NORTH);
        pm = new PackMan(lev1, 2, 0);
        chewTimer = new Timer(150, this);
        moveTimer = new Timer(50,this);
        chewTimer.start();
        moveTimer.start();
        message = "Click a cell to show info";
        message2 = pm.toString();
        moveCnt=0;
 
    }
 
    public static void main(String[] args) {
        LevelTester app= new LevelTester();
        JFrame window = new JFrame("Level Tester");
        window.setSize(WIDTH, HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(app);
        // remember to add Key Listeners for the game interface
        window.addKeyListener(app);
        window.getContentPane().addMouseListener(app);
        window.setVisible(true);
 
    }
 
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(),getHeight());
        lev1.draw(g);
        pm.draw(g);
        g.setColor(Color.BLACK);
        g.drawString(message, 50,50);
        message2 = pm.toString();
        g.drawString(message2, 50,70);
        g.drawString("moves: "+moveCnt + " Crumbs: "+ lev1.crumbsLeft(), 50,90);
    }
 
    public void actionPerformed(ActionEvent e) {
 
        if (e.getSource()==chewTimer){
            pm.changeMouth();
        } else if (e.getSource()==moveTimer) {
            pm.move();
            moveCnt++;
        }
 
        repaint();
    }
 
    public void keyTyped(KeyEvent e) {}  
 
    public void keyPressed(KeyEvent e) {}
 
    public void keyReleased(KeyEvent e) {
        int keyCode=e.getKeyCode();
 
        if(keyCode == 37){
            //left
            pm.setDirection(Cell.WEST);
        } else if (keyCode ==38){
            // up
            pm.setDirection(Cell.NORTH);
        } else if (keyCode ==39){
            // right
            pm.setDirection(Cell.EAST);
        }else if (keyCode ==40){
            // down
            pm.setDirection(Cell.SOUTH);
        } 
 
        repaint();
 
    }
    @Override
	public void mouseClicked(MouseEvent e) {
 
	}
	@Override
	public void mousePressed(MouseEvent e) {
 
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		int x=e.getX();
		int y=e.getY();
		message = lev1.getCell(x,y).toString();
 
		repaint();
	}
	@Override
	public void mouseEntered(MouseEvent e) {
 
	}
	@Override
	public void mouseExited(MouseEvent e) {
 
	}
 
}
packperson.txt · Last modified: 2023/05/19 09:29 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki