User Tools

Site Tools


packperson

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
packperson [2023/05/14 10:17] frchrispackperson [2023/05/19 09:29] (current) frchris
Line 1: Line 1:
 ====== Chase's Pack Person Game ====== ====== Chase's Pack Person Game ======
 +
 +[[https://mathorama.com/apcs2/PacMaze.jar|PacMaze.jar]]
  
 [[https://mathorama.com/apcs/pmwiki.php?n=Main.MoveAMonsterApplication|Move Monster]] [[https://mathorama.com/apcs/pmwiki.php?n=Main.MoveAMonsterApplication|Move Monster]]
Line 9: Line 11:
 {{::level1.png?400|}} {{::level1.png?400|}}
  
-{{::pacman.gif?400|}}+{{::pacman.gif|packman.gif}} 
 + 
 +{{ ::packman.jar | Executable Jar file that tests the Cell, Level, and PackMan classes}}
  
 <code java Cell.java> <code java Cell.java>
Line 17: Line 21:
 { {
     public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;     public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;
-    +
     public static final int GAP = 5;     public static final int GAP = 5;
-    +
     private boolean[] canExit;// NSEW true if an exit     private boolean[] canExit;// NSEW true if an exit
     private boolean visited;     private boolean visited;
     private int x, y, size;     private int x, y, size;
-    +
     public Cell (int x, int y , int size)     public Cell (int x, int y , int size)
     {     {
Line 33: Line 37:
         setAllExits(true);             setAllExits(true);    
     }     }
 +
     public boolean canExit(int dir){     public boolean canExit(int dir){
-        return canExit(dir);+        return canExit[dir];
     }     }
 +
     public void noExit(int dir) {     public void noExit(int dir) {
         canExit[dir] = false;         canExit[dir] = false;
     }     }
 +
     public void exit(int dir) {     public void exit(int dir) {
         canExit[dir] = true;         canExit[dir] = true;
     }     }
 +
     public void setAllExits(boolean b){     public void setAllExits(boolean b){
         for(int i = 0; i < canExit.length; i++)         for(int i = 0; i < canExit.length; i++)
-             canExit[i] = b;+            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)     public void draw(Graphics g)
     {     {
Line 68: Line 99:
         }         }
     }     }
-} 
  
 +    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;
 +    }
 +}
 </code> </code>
  
 <code java Level.java> <code java Level.java>
 import java.awt.*; import java.awt.*;
- 
  
 public class Level public class Level
Line 86: Line 129:
         this.top = top;         this.top = top;
         this.left = left;         this.left = left;
 +        this.size = size;
         grid = new Cell[rows][cols];         grid = new Cell[rows][cols];
         for(int r=0; r<rows; r++)         for(int r=0; r<rows; r++)
-           for(int c = 0; c < cols; c++){ +            for(int c = 0; c < cols; c++){ 
-               grid[r][c] = new Cell(left+c*size, top+r*size ,size); +                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 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++){         for(int r=0; r < rows; r++){
             grid[r][0].noExit(Cell.WEST);             grid[r][0].noExit(Cell.WEST);
Line 100: Line 144:
         }         }
     }     }
 +
     public void noExit(int r, int c, int dir)     public void noExit(int r, int c, int dir)
     {     {
         grid[r][c].noExit(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)     public void draw(Graphics g)
     {     {
         for(Cell[] row:grid)         for(Cell[] row:grid)
             for(Cell c : row)             for(Cell c : row)
-                   c.draw(g);+                c.draw(g);
     }     }
 } }
 +</code>
 +
 +<code java 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;
 +    }
 +}
 +
 </code> </code>
  
Line 116: Line 300:
 import javax.swing.*; import javax.swing.*;
 import java.awt.*; import java.awt.*;
 +import java.awt.event.*;
 +import javax.swing.Timer;
  
- +public class LevelTester extends JPanel implements KeyListener, ActionListener, MouseListener
-public class LevelTester extends JPanel+
 { {
     public static int WIDTH=800;     public static int WIDTH=800;
     public static int HEIGHT=600;     public static int HEIGHT=600;
     public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;     public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;
-    +    private PackMan pm;
     private Level lev1;     private Level lev1;
 +    private Timer chewTimer, moveTimer;
 +    private String message, message2;
 +    private int moveCnt;
 +
     public LevelTester(){     public LevelTester(){
-        lev1 = new Level(3, 7,  100,  50,  50);+        int size =50; 
 +        lev1 = new Level(3, 7,  100,  size,  size);
         lev1.noExit(0,1,SOUTH);         lev1.noExit(0,1,SOUTH);
         lev1.noExit(0,2,SOUTH);         lev1.noExit(0,2,SOUTH);
Line 145: Line 335:
         lev1.noExit(2,3,NORTH);         lev1.noExit(2,3,NORTH);
         lev1.noExit(2,4,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) {     public static void main(String[] args) {
         LevelTester app= new LevelTester();         LevelTester app= new LevelTester();
Line 152: Line 352:
         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         window.getContentPane().add(app);         window.getContentPane().add(app);
-// remember to add Key Listeners for the game interface+        // remember to add Key Listeners for the game interface 
 +        window.addKeyListener(app); 
 +        window.getContentPane().addMouseListener(app);
         window.setVisible(true);         window.setVisible(true);
  
     }     }
 +
     public void paintComponent(Graphics g){     public void paintComponent(Graphics g){
         super.paintComponent(g);         super.paintComponent(g);
Line 161: Line 364:
         g.fillRect(0, 0, getWidth(),getHeight());         g.fillRect(0, 0, getWidth(),getHeight());
         lev1.draw(g);         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) {
 +
 + }
 +
 } }
 </code> </code>
 +
 +
  
packperson.1684073823.txt.gz · Last modified: 2023/05/14 10:17 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki