packperson
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| packperson [2023/05/08 14:45] – frchris | packperson [2023/05/19 09:29] (current) – frchris | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Chase' | ====== Chase' | ||
| + | |||
| + | [[https:// | ||
| [[https:// | [[https:// | ||
| Line 6: | Line 8: | ||
| [[https:// | [[https:// | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | {{:: | ||
| + | |||
| + | {{ :: | ||
| + | |||
| + | <code java 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; | ||
| + | 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, | ||
| + | if( !visited){ | ||
| + | g.setColor(Color.YELLOW); | ||
| + | g.fillRect(x+size/ | ||
| + | } | ||
| + | g.setColor(Color.MAGENTA.darker()); | ||
| + | if(!canExit[NORTH]){ | ||
| + | g.fillRect(x, | ||
| + | } | ||
| + | if(!canExit[SOUTH]){ | ||
| + | g.fillRect(x, | ||
| + | } | ||
| + | if(!canExit[EAST]){ | ||
| + | g.fillRect(x+size-GAP, | ||
| + | } | ||
| + | if(!canExit[WEST]){ | ||
| + | g.fillRect(x, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | 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> | ||
| + | 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, | ||
| + | } | ||
| + | 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, | ||
| + | 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); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <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; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code java LevelTester.java> | ||
| + | import javax.swing.*; | ||
| + | import java.awt.*; | ||
| + | import java.awt.event.*; | ||
| + | import javax.swing.Timer; | ||
| + | |||
| + | public class LevelTester extends JPanel implements KeyListener, | ||
| + | { | ||
| + | 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, | ||
| + | lev1.noExit(0, | ||
| + | lev1.noExit(0, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | lev1.noExit(1, | ||
| + | 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) { | ||
| + | LevelTester app= new LevelTester(); | ||
| + | JFrame window = new JFrame(" | ||
| + | window.setSize(WIDTH, | ||
| + | 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, | ||
| + | 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.1683571535.txt.gz · Last modified: 2023/05/08 14:45 by frchris