@Override public void update() { if (paused) { return; } eManager.update(); tManager.update(eManager.getLiveEnemies()); if (player.getLives() <= 0) { gsm.setState(GameStateManager.GAMEOVERSTATE); } }
/** * Draw the HUD * * @param g the g */ public void draw(Graphics2D g) { // Draw the number of lives the player has for (int i = 0; i < player.getLives(); i++) { g.drawImage(image, i * 30, 0, null); } g.setFont(font); g.setColor(Color.GREEN); g.drawString("1UP", 130, 26); g.setColor(Color.WHITE); g.drawString("" + player.getExtraLive(), 130, 54); g.setColor(Color.RED); g.drawString("HIGH SCORE", 325, 26); g.setColor(Color.WHITE); g.drawString("" + player.getScore(), 325, 54); }
public void keyPressed(KeyEvent arg0) { // the controller should not do anything when the player is dead if (player.getLives() <= 0) { return; } String key = ("" + arg0.getKeyChar()).toLowerCase(); if (key.equals("a")) { // move to the left player.moveLeft(); } if (key.equals("d")) { // move to the right player.moveRight(); } if (key.equals(" ")) { // shoot player.fire(); } }
/** * 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(); } }
/** @param args the command line arguments */ public static void main(String[] args) throws IOException { // This BufferedReader handles navigation related inputs java.io.BufferedReader navInput = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); // This BufferedReader handles navigation related inputs java.io.BufferedReader actionInput = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); // Holds the items that the player is carrying ArrayList<Item> playerItems = new ArrayList<>(); // Create a new instance of world in a default state World world = new World(); // Creates a new instance object of player/avatar at location with 3 lives Player player = new Player(world.locations.get(0), 3); GoodByeMessages goodByeMessage = new GoodByeMessages(); String cmd = ""; String action = ""; // Loop through for as long the player does not quit and the number of lives is more than 0 while (!cmd.equalsIgnoreCase("quit") && (player.getLives() > 0)) { System.out.println("You are " + player.location.desc); System.out.println("What next?"); try { cmd = navInput.readLine(); // Scan in player command } catch (java.io.IOException e) { System.out.println(e); } // If the player just wants to take a peek without making a turn or navigating away // from current location. The look describes the surrounding of the avatar if (cmd.equalsIgnoreCase("look")) { // First check if there are any items of this location by checking // the size of the array list that holds the items for the location if (player.location.getItems().size() > 0) { System.out.println("You see:"); // Print all items at this location for (int i = 0; i < player.location.getItems().size(); i++) { System.out.println(player.location.getItems().get(i).getItemName()); } } else { // if no items at this location System.out.println("There are no items at this location"); } } // If the player wants to pick up an item. The take command picks up items // from the current location and places this items into the player's hands // The item must exist at this location if (cmd.equalsIgnoreCase("take")) { // First check if there are any items at this location if (player.location.getItems().size() > 0) { // loop through all items one by one and ask if player wants to pick this item for (int i = 0; i < player.location.getItems().size(); i++) { // do not allow player to pick up the dragon item // Therefore only make available all items apart from the dragon if (player.location.getItems().get(i).getItemName().equals("Dragon")) { continue; } System.out.println("Take this? Y/N"); System.out.println(player.location.getItems().get(i).getItemName()); action = actionInput.readLine(); // If the player wants to take the item; // Do: add item to the player items collection, and remove item from this location if (action.equalsIgnoreCase("y")) { // add item to the player items list playerItems.add(player.location.getItems().get(i)); // remove the item from this location player.location.getItems().remove(i); } } } else { // inform player that there are no items at this location System.out.println("There are no items at this location"); } } // If the player wants to drop an item. // The drop command is for dropping the items the player is carrying if (cmd.equalsIgnoreCase("drop")) { // First check if the player is carrying any item if (playerItems.size() > 0) { // Loop through all the items the player is carryig. // one by one ask the player if he/she wants to drop the item for (int i = 0; i < playerItems.size(); i++) { System.out.println("drop this? Y/N"); System.out.println(playerItems.get(i).getItemName()); action = actionInput.readLine(); // if the player wants to drop, then add the selected item to // the current location, and remove the item from the player's location if (action.equalsIgnoreCase("y")) { // add item to this locationk player.location.setItems(playerItems.get(i).getItemName()); // set the item to null, you cannot use remove here because // if you remove it will change the size of the array list // which will disallow the player to drop some items without having to type drop again playerItems.set(i, null); } } // check if some items are null and get rid of all null values in the array list if (playerItems.contains(null)) { playerItems.removeAll(Collections.singleton(null)); } } else { // inform user they have no items System.out.println("You have nothing to drop"); } } // if the user wants to kill the dragon. // Conditions: 1. Player must be in dragon cave, // 2. Player must have a sword and a lamp // and if player tries to kill the dragon without any of the required items, // he will get killed by the dragon and lose a life and will wake up at babbling brook if (cmd.equalsIgnoreCase("kill")) { // check if the player is in the dragon cave. if (player.location.getLocationName().equalsIgnoreCase("Dragon's Cave")) { // check if the player is carrying any items at all if (playerItems.size() > 0) { boolean sword = false; boolean lamp = false; // loop through the items the playe is holding for (int i = 0; i < playerItems.size(); i++) { // check if the player has the necessary items to kill the dragon if (playerItems.get(i).getItemName().equals("Sword")) { sword = true; } if (playerItems.get(i).getItemName().equals("Lamp")) { lamp = true; } } // If the player has both the sword and the lamp, he can kill the dragon if (sword && lamp) { System.out.println("Hurray! You have killed the Dragon"); break; } else { System.out.println("Awww! You have been killed by the Dragon"); player.decreaseLives(); // decrement the number of lives player.location = world.locations.get(0); } } else { System.out.println("Awww! You have been killed by the Dragon"); player.decreaseLives(); // decrement the number of lives player.location = world.locations.get(0); } } else { // inform player, there is no dragon here System.out.println("Easy Tiger!, we ain't there yet"); } } System.out.println(); // if player is trying to navigate to the north of the current location if (cmd.equalsIgnoreCase("n")) { // Check if there is an exit towards that direction if (player.location.north != null) { // if there if an exit, then set the current location to the new location player.location = player.location.north; } else { System.out.println("There is no exit to the north."); } } // if player is trying to navigate to the east of the current location if (cmd.equalsIgnoreCase("e")) { // Check if there is an exit towards that direction if (player.location.east != null) { // if there if an exit, then set the current location to the new location player.location = player.location.east; } else { System.out.println("There is no exit to the east."); } } // if player is trying to navigate to the south of the current location if (cmd.equalsIgnoreCase("s")) { // Check if there is an exit towards that direction if (player.location.south != null) { // if there if an exit, then set the current location to the new location player.location = player.location.south; } else { System.out.println("There is no exit to the south."); } } // if player is trying to navigate to the west of the current location if (cmd.equalsIgnoreCase("w")) { // Check if there is an exit towards that direction if (player.location.west != null) { // if there if an exit, then set the current location to the new location player.location = player.location.west; } else { System.out.println("There is no exit to the west."); } } if (cmd.equalsIgnoreCase("quit")) { System.out.println(goodByeMessage.getGoodByeMessage(getRandomIdx())); } } if (player.getLives() == 0) { System.out.println(goodByeMessage.getGoodByeMessage(getRandomIdx())); } }
@Override public boolean isGameOver() { return player.getLives() < 1; }