packperson
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| packperson [2023/05/14 10:19] – frchris | packperson [2023/05/19 09:29] (current) – frchris | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Chase' | ====== Chase' | ||
| + | |||
| + | [[https:// | ||
| [[https:// | [[https:// | ||
| Line 10: | Line 12: | ||
| {{:: | {{:: | ||
| + | |||
| + | {{ :: | ||
| <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; | for(int i = 0; i < canExit.length; | ||
| - | 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 += " | ||
| + | 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 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++){ | + | |
| - | | + | grid[r][c] = new Cell(left+c*size, |
| - | } | + | |
| - | for(int c =0; c < cols; c++){ | + | |
| - | | + | |
| - | | + | |
| } | } | ||
| + | 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, | ||
| + | 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); | + | |
| } | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | <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, | ||
| + | isOpen = false; | ||
| + | direction = Cell.SOUTH; | ||
| + | lev.getCell(x, | ||
| + | } | ||
| + | |||
| + | 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, | ||
| + | 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 ) | ||
| + | | ||
| + | else if (direction == Cell.EAST) | ||
| + | { | ||
| + | | ||
| + | }else if (direction == Cell.NORTH) | ||
| + | { | ||
| + | | ||
| + | }else if (direction == Cell.WEST) | ||
| + | { | ||
| + | | ||
| + | }else if (direction == Cell.SOUTH) | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | public String toString(){ | ||
| + | String result = " | ||
| + | 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 += " | ||
| + | return result; | ||
| + | } | ||
| + | } | ||
| + | |||
| </ | </ | ||
| 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 | |
| - | 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 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, | + | |
| + | | ||
| lev1.noExit(0, | lev1.noExit(0, | ||
| lev1.noExit(0, | lev1.noExit(0, | ||
| Line 145: | Line 335: | ||
| lev1.noExit(2, | lev1.noExit(2, | ||
| lev1.noExit(2, | lev1.noExit(2, | ||
| + | pm = new PackMan(lev1, | ||
| + | chewTimer = new Timer(150, this); | ||
| + | moveTimer = new Timer(50, | ||
| + | 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 | + | |
| + | 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, | g.fillRect(0, | ||
| lev1.draw(g); | lev1.draw(g); | ||
| + | pm.draw(g); | ||
| + | g.setColor(Color.BLACK); | ||
| + | g.drawString(message, | ||
| + | message2 = pm.toString(); | ||
| + | g.drawString(message2, | ||
| + | g.drawString(" | ||
| } | } | ||
| + | |||
| + | 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, | ||
| + | |||
| + | repaint(); | ||
| + | } | ||
| + | @Override | ||
| + | public void mouseEntered(MouseEvent e) { | ||
| + | |||
| + | } | ||
| + | @Override | ||
| + | public void mouseExited(MouseEvent e) { | ||
| + | |||
| + | } | ||
| + | |||
| } | } | ||
| </ | </ | ||
| + | |||
| + | |||
packperson.1684073965.txt.gz · Last modified: 2023/05/14 10:19 by frchris