Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- long start = System.nanoTime();
- // If the player is alive, this should move the player in the direction of the
- // key that has been pressed
- // Note: See keyPressed and keyReleased methods in the GameManager class.
- this.player.performAction();
- // If the enemy is alive, the enemy must move towards the goal. The goal object
- // is obtained
- // via the GameManager object that is given at the time of creating an Enemy
- // object.
- // Note: The amount that the enemy moves by must be much smaller than that of
- // the player above
- // or else the game becomes too hard to play.
- for (int i = 0; i < this.numEnemies; i++) {
- this.enemies[i].performAction();
- }
- if ((Math.abs(this.goal.getX() - this.player.getX()) < (this.goal.getCurrentImage().getWidth() / 2))
- && (Math.abs(this.goal.getY() - this.player.getY()) < (this.goal.getCurrentImage().getWidth() / 2))) {
- for (int i = 0; i < this.numEnemies; i++) {
- // Sets the image of the enemy to the "dead" image and sets its status to
- // indicate dead
- this.enemies[i].die();
- }
- // Sets the image of the enemy to the "dead" image and sets its status to
- // indicate dead
- this.goal.die();
- // Sets the background of the stage to the finished game background.
- this.stage.setGameOverBackground();
- this.continueGame = false;
- }
- // If an enemy is close to the goal, the player and goal die
- int j = 0;
- while (j < this.numEnemies) {
- if ((Math.abs(this.goal.getX() - this.enemies[j].getX()) < (this.goal.getCurrentImage().getWidth() / 2))
- && (Math.abs(this.goal.getY() - this.enemies[j].getY()) < (this.goal.getCurrentImage().getWidth()
- / 2))) {
- this.player.die();
- this.goal.die();
- this.stage.setGameOverBackground();
- j = this.numEnemies;
- this.continueGame = false;
- }
- j++;
- }
- try {
- // Draw stage
- this.canvasGraphics.drawImage(stage.getCurrentImage(), 0, 0, null);
- // Draw player
- this.canvasGraphics.drawImage(player.getCurrentImage(),
- this.player.getX() - (this.player.getCurrentImage().getWidth() / 2),
- this.player.getY() - (this.player.getCurrentImage().getHeight() / 2), null);
- // Draw enemies
- for (int i = 0; i < this.numEnemies; i++) {
- this.canvasGraphics.drawImage(this.enemies[i].getCurrentImage(),
- this.enemies[i].getX() - (this.enemies[i].getCurrentImage().getWidth() / 2),
- this.enemies[i].getY() - (this.enemies[i].getCurrentImage().getHeight() / 2), null);
- }
- // Draw goal
- this.canvasGraphics.drawImage(this.goal.getCurrentImage(),
- this.goal.getX() - (this.goal.getCurrentImage().getWidth() / 2),
- this.goal.getY() - (this.goal.getCurrentImage().getHeight() / 2), null);
- } catch (Exception e) {
- // -----------------------------------//
- System.err.println(e.getMessage());
- }
- // Draw everything.
- this.gameGraphics.drawImage(this.canvas, this.borderLeft, this.borderTop, this);
- long end = System.nanoTime();
- this.gameGraphics.drawString("FPS: " + String.format("%2d", (int) (1000000000.0 / (end - start))),
- this.borderLeft + 50, this.borderTop + 50);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement