/** Checks if the player collides with a blip, power blip, or a ghost. */ public void checkCollision() { Rectangle playerRect = new Rectangle(player.getCenter().x - 8, player.getCenter().y - 8, 13, 13); for (Enemy enemy : enemies) { Rectangle enemyRect = new Rectangle(enemy.getCenter().x - 8, enemy.getCenter().y - 8, 13, 13); if (playerRect.intersects(enemyRect)) { if (!enemy.isAlive()) { continue; } if (Globals.state == ENEMY_HUNTER_STATE) { Globals.game.playerKilled(); player.loseALife(); // player is dead, so we paint the dead player screen paintDeadPlayer(); try { // sleep to make it stay on screen Thread.sleep(1500); } catch (InterruptedException e) { // DO nothing } resetCanvas(); // we return because only one enemy should be able to make a player lose a life return; } else if (Globals.state == ENEMY_HUNTED_STATE) { if (enemy.isAlive()) { enemy.setAlive(false); player.addPoints(POINTS_FOR_KILLING_ENEMY); } } } else { continue; } } }
public void update() { for (Iterator ite = enemyList.iterator(); ite.hasNext(); ) { Enemy e = (Enemy) ite.next(); if (!e.isAlive()) { ite.remove(); } } }
private int testEnemyCol() { for (int i = 0; i < game.enemies.size(); i++) { Enemy e = game.enemies.get(i); if (bounding.intersects(e.getBounding()) && e.isAlive()) { e.die(); return i; } } return -1; }
/** * This starts the main game loop. This is the guts of the game. When the loop starts it checks to * see if the player has any lives left, if not, it prints "Game Over" and resets the game. If * there are lives left, it decides what the ghosts next move will be, detects any collisions, and * acts accordingly, and also gets input from the player */ public void start() { Globals.state = ENEMY_HUNTER_STATE; Globals.game.setPlayer(new Player()); player = Globals.game.getPlayer(); player.setCenter(board.getPlayerStartPoint()); newGame(); paused = false; waiting = true; Point regenDoorPoint = board.getRegenDoor(); Point regenPoint = board.getRegenPoint(); Point playerPoint = player.getCenter(); while ((this.isVisible()) && (player.getLives() >= 0)) { if (paused) { paintPauseScreen(); } while (paused || stopped || waiting) { Thread.yield(); } if (Globals.blipsLeft <= 0) { Globals.speed *= SPEED_MOD; resetCanvas(); board.reset(); Globals.blipsLeft = board.getBlipCount(); } // now we need to see if the current state is enemy hunted if (Globals.state == ENEMY_HUNTED_STATE) { // now we need to see if we should go out of it if (Globals.timeUntilLeaveingHuntedState <= 0) { Globals.state = ENEMY_HUNTER_STATE; Globals.timeUntilLeaveingHuntedState = TIME_IN_HUNTED_STATE; } else { // we shouldn't, so we decrement the counter Globals.timeUntilLeaveingHuntedState -= Globals.speed; } } // move the player player.move(board.getMoveOptions(player), playerChoice); if (Globals.aiMode == FSA_AI_MODE) { // now updated the enemy movements for (int i = 0; i < enemies.length; i++) { // these are put here to reduce program jumping and cache misses as well Enemy enemy = enemies[i]; int moveOptions = board.getMoveOptions(enemy); // first, we need to take apporpriate actions if we're on a regen tile if (board.getCurrentTile(enemy).isRegen()) { // first check to see if the enemy is alive if (!enemy.isAlive()) { // it's not, so we make it alive enemy.setAlive(true); } // now we tell the enemy to move towards the the door enemy.move(moveOptions, regenDoorPoint, true); } else { // We're not in the regen pen, so now we check if the enemy is alive if (!enemy.isAlive()) { // it's not, so we want to go towards the regen point enemy.move(moveOptions, regenPoint, false); } else { // the enemy is alive, so we tell it towards/away from the player enemy.move(moveOptions, playerPoint, false); } } } } else if (Globals.aiMode == ANN_AI_MODE) { // Globals.game.getNet().process(ANNTools.getBoardCondition(board, enemies, player)); player.setInputValues(board); for (Enemy enemy : enemies) { enemy.setInputValues(board); } Globals.game.getNet().generateActivations(); for (int i = 0; i < enemies.length; i++) { // Last 2 values don't matter here enemies[i].move( board.getMoveOptions(enemies[i]), board.getRegenDoor(), board.getCurrentTile(enemies[i]).isRegen()); } } // no we check for collisions and act appropriately checkCollision(); // check to see if we should notify the game that we have stepped if (Globals.trainingMode != TRAINING_MODE_OFF) { // we should, so we do. Globals.game.step(); } // And now we sleep. The sleep time is the current speed try { Thread.sleep(Globals.speed); } catch (InterruptedException e) { break; } // no update the screen update(); } if (player.getLives() <= 0) { paintGameOver(); try { Thread.sleep(2500); } catch (InterruptedException e) { // DO nothing } newGame(); start(); } }
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { counter += delta; Input input = gc.getInput(); float fdelta = delta * Player.speed; Player.setpdelta(fdelta); double rightlimit = (grassMap.getWidth() * SIZE) - (SIZE * 0.75); // System.out.println("Right limit: " + rightlimit); float projectedright = Player.x + fdelta + SIZE; boolean cangoright = projectedright < rightlimit; // there are two types of fixes. A kludge and a hack. This is a kludge. if (input.isKeyDown(Input.KEY_UP)) { sprite = up; float fdsc = (float) (fdelta - (SIZE * .15)); if (!(isBlocked(Player.x, Player.y - fdelta) || isBlocked((float) (Player.x + SIZE + 1.5), Player.y - fdelta))) { sprite.update(delta); // The lower the delta the slower the sprite will animate. Player.y -= fdelta; } } else if (input.isKeyDown(Input.KEY_DOWN)) { sprite = down; if (!isBlocked(Player.x, Player.y + SIZE + fdelta) || !isBlocked(Player.x + SIZE - 1, Player.y + SIZE + fdelta)) { sprite.update(delta); Player.y += fdelta; } } else if (input.isKeyDown(Input.KEY_LEFT)) { sprite = left; if (!(isBlocked(Player.x - fdelta, Player.y) || isBlocked(Player.x - fdelta, Player.y + SIZE - 1))) { sprite.update(delta); Player.x -= fdelta; } } else if (input.isKeyDown(Input.KEY_RIGHT)) { sprite = right; // the boolean-kludge-implementation if (cangoright && (!(isBlocked(Player.x + SIZE + fdelta, Player.y) || isBlocked(Player.x + SIZE + fdelta, Player.y + SIZE - 1)))) { sprite.update(delta); Player.x += fdelta; } // else { System.out.println("Right limit reached: " + // rightlimit);} } Player.rect.setLocation(Player.getplayershitboxX(), Player.getplayershitboxY()); // for (Item i : stuff) { // // if (Player.rect.intersects(i.hitbox)) { // //System.out.println("yay"); // if (i.isvisible) { // Player.health += 10000; // i.isvisible = false; // } // } // } /*for (Ninja n : dojo) { if (Player.rect.intersects(n.hitbox)) { //System.out.println("yay"); if (n.isvisible) { Player.health += 10000; n.isvisible = false; } } } */ for (Bone b : doghouse) { if (Player.rect.intersects(b.hitbox)) { // System.out.println("yay"); if (b.isvisible) { Player.speed += .1f; b.isvisible = false; Player.Counter += 1; } } if (Player.Counter > 10) { Bone.Counter = false; sbg.enterState(3, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); } } /*for (Item1 h : stuff1) { if (Player.rect.intersects(h.hitbox)) { //System.out.println("yay"); if (h.isvisible) { Player.speed += .1f; h.isvisible = false; } } } */ // for (Itemwin w : stuffwin) { // if (Player.rect.intersects(w.hitbox)) { // //System.out.println("yay"); // if (w.isvisible) { // w.isvisible = false; // makevisible(); // sbg.enterState(3, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); // // } // // } // } for (Enemy e : bonez) { if (e.isAlive) { e.move(); } } for (Enemy e : bonez) { if (e.isAlive) { if (Player.rect.intersects(e.rect)) { Player.health = 0; e.isAlive = true; // once they touch the enemy player die } } } Player.health -= counter / 1000; if (Player.health <= 0) { makevisible(); sbg.enterState(2, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); } }