Exemplo n.º 1
0
 /**
  * check if human collides with a ghost
  *
  * @param game
  * @return the ghost where the human collides with
  */
 public Ghost checkGhostCollision(Game game) {
   // ghost collision
   for (Ghost ghost : game.getGhosts()) {
     if (!ghost.getRip() && !ghost.getDead()) {
       if (checkHitboxCollision(this.getPosition(), 90, 90, ghost.getPosition(), 90, 90)) {
         return ghost;
       }
     }
   }
   return null;
 }
Exemplo n.º 2
0
 public void update(List<Point> ghostPositions) {
   for (int i = 0; i < ghosts.size(); ++i) {
     Ghost ghost = ghosts.get(i);
     Cell currentCell = getCell(ghost.getPosition());
     Cell newCell = getCell(ghostPositions.get(i));
     if (!currentCell.equals(newCell)) {
       grid.get(currentCell.row).get(currentCell.column).remove(ghost);
       grid.get(newCell.row).get(newCell.column).add(ghost);
     }
   }
 }
Exemplo n.º 3
0
 public void addGhosts() {
   for (int i = 0; i < 2; i++) {
     for (int j = 0; j < 2; j++) {
       if (!(i == 0 && j == 0)) {
         ghost = new Ghost(19 * j * 25 + 7.5, 19 * i * 25 + 7.5, boxSize);
         ghost.setFilled(true);
         ghost.setColor(Color.MAGENTA);
         add(ghost);
         ghostList.add(ghost);
       }
     }
   }
 }
Exemplo n.º 4
0
 /**
  * Check at the intersection of Pac-man with a Ghost
  *
  * @return Ghost or null
  */
 public Ghost isHereGhost() {
   for (Ghost ghost : Game.ghosts) {
     if (ghost.getX() == previousX
         && ghost.getY() == previousY
         && ghost.previousX == x
         && ghost.previousY == y) {
       return ghost;
     }
     if (ghost.getX() == x && ghost.getY() == y) {
       return ghost;
     }
   }
   return null;
 }
Exemplo n.º 5
0
  private void build(List<Ghost> ghosts) {
    grid = new ArrayList<List<List<Ghost>>>();
    for (int i = 0; i < nRows; ++i) {
      List<List<Ghost>> columns = new ArrayList<List<Ghost>>();
      for (int j = 0; j < nColumns; ++j) {
        columns.add(new LinkedList<Ghost>());
      }
      grid.add(columns);
    }

    for (Ghost ghost : ghosts) {
      Cell c = getCell(ghost.getPosition());
      grid.get(c.row).get(c.column).add(ghost);
    }
  }
Exemplo n.º 6
0
  public void run() {
    addKeyListeners();

    File boardFile = new File("largerboard.txt");
    try {
      Scanner sc = new Scanner(boardFile);
      int rowNum = 0;
      while (sc.hasNextLine()) {
        String s = sc.nextLine();
        char[] nums = s.toCharArray();
        for (int i = 0; i < nums.length; i++) {
          char2D[i][rowNum] = nums[i];
          System.out.print(char2D[i][rowNum] + " ");
        }
        System.out.println();
        rowNum++;
      }
    } catch (IOException e) {
      System.out.println("File not found.");
    }

    GRect box;
    GOval o;
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        // int rand = (int) (3*Math.random());
        // twoD[j][i] = rand;
        // System.out.print(twoD[j][i] + " ");

        box = new GRect(boxSize * j, boxSize * i, boxSize, boxSize);
        add(box);

        if (char2D[j][i] == '0') {
          box.setFilled(true);
          box.setColor(Color.BLACK);
          o = new GOval(boxSize * j + 10, boxSize * i + 10, 5, 5);
          o.setFilled(true);
          o.setColor(Color.YELLOW);
          add(o);
          food.add(o);
        } else if (char2D[j][i] == '1') {
          box.setFilled(true);
          box.setColor(Color.BLUE);
        }
      }
      // System.out.println();
    }
    // end of nested for loops
    player = new GOval(playerX + 5, playerY + 5, 15, 15);
    player.setFilled(true);
    player.setColor(Color.RED);
    add(player);

    addGhosts();

    while (true) {

      // ghost movement
      // 0: up, 1: down, 2: right, 3: left
      for (int i = 0; i < ghostList.size(); i++) {
        ghost = (Ghost) ghostList.get(i);
        if (timer % 500 == 0) {
          if (playerX < ghost.getGhostX()) {
            ghost.move(3);
          } else if (playerY > ghost.getGhostY()) {
            ghost.move(1);
          } else if (playerX > ghost.getGhostX()) {
            ghost.move(2);
          } else if (playerY < ghost.getGhostY()) {
            ghost.move(0);
          }
          //				int rand =(int) (4*Math.random()); // between 0-3
          //				if(rand == 2 && isCollidable(ghost.getGhostX()+1, ghost.getGhostY())){
          //					ghost.move(rand);
          //				}
          //				else if(rand == 3 && isCollidable(ghost.getGhostX()-1, ghost.getGhostY())){
          //					ghost.move(rand);
          //				}
          //				else if(rand == 1 && isCollidable(ghost.getGhostX(), ghost.getGhostY()+1)){
          //					ghost.move(rand);
          //				}
          //				else if(rand == 0 && isCollidable(ghost.getGhostX(), ghost.getGhostY()-1)){
          //					ghost.move(rand);
          //				}
          // System.out.println(ghost.getGhostX() +" , "+ ghost.getGhostY() );
        }
      }

      if (collision(ghost, player)) {
        remove(player);
        hasLost = true;
        break;
      }

      if (food.size() == 0) {
        break;
      }

      for (int i = 0; i < food.size(); i++) {
        GOval foodPiece = (GOval) food.get(i);
        if (collision(player, foodPiece)) {
          remove(foodPiece);
          food.remove(i);
        }
      }

      pause(10);
      timer = timer + 10;
    }

    GLabel wonLost;

    if (hasLost) {
      wonLost = new GLabel("YOU LOSE", 220, 250);
    } else {
      wonLost = new GLabel("YOU WON AND ARE AWESOME", 220, 250);
    }

    wonLost.setColor(Color.WHITE);
    add(wonLost);
  }