packperson
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| packperson [2023/05/18 14:44] – 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 187: | Line 191: | ||
| } | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | <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 327: | Line 435: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | |||
packperson.1684435479.txt.gz · Last modified: 2023/05/18 14:44 by frchris