Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1.  
  2. long start = System.nanoTime();
  3.  
  4. // If the player is alive, this should move the player in the direction of the
  5. // key that has been pressed
  6. // Note: See keyPressed and keyReleased methods in the GameManager class.
  7. this.player.performAction();
  8.  
  9. // If the enemy is alive, the enemy must move towards the goal. The goal object
  10. // is obtained
  11. // via the GameManager object that is given at the time of creating an Enemy
  12. // object.
  13. // Note: The amount that the enemy moves by must be much smaller than that of
  14. // the player above
  15. // or else the game becomes too hard to play.
  16. for (int i = 0; i < this.numEnemies; i++) {
  17. this.enemies[i].performAction();
  18. }
  19.  
  20. if ((Math.abs(this.goal.getX() - this.player.getX()) < (this.goal.getCurrentImage().getWidth() / 2))
  21. && (Math.abs(this.goal.getY() - this.player.getY()) < (this.goal.getCurrentImage().getWidth() / 2))) {
  22. for (int i = 0; i < this.numEnemies; i++) {
  23. // Sets the image of the enemy to the "dead" image and sets its status to
  24. // indicate dead
  25. this.enemies[i].die();
  26. }
  27. // Sets the image of the enemy to the "dead" image and sets its status to
  28. // indicate dead
  29. this.goal.die();
  30.  
  31. // Sets the background of the stage to the finished game background.
  32. this.stage.setGameOverBackground();
  33. this.continueGame = false;
  34. }
  35. // If an enemy is close to the goal, the player and goal die
  36. int j = 0;
  37. while (j < this.numEnemies) {
  38. if ((Math.abs(this.goal.getX() - this.enemies[j].getX()) < (this.goal.getCurrentImage().getWidth() / 2))
  39. && (Math.abs(this.goal.getY() - this.enemies[j].getY()) < (this.goal.getCurrentImage().getWidth()
  40. / 2))) {
  41. this.player.die();
  42. this.goal.die();
  43. this.stage.setGameOverBackground();
  44. j = this.numEnemies;
  45. this.continueGame = false;
  46. }
  47. j++;
  48. }
  49. try {
  50. // Draw stage
  51. this.canvasGraphics.drawImage(stage.getCurrentImage(), 0, 0, null);
  52. // Draw player
  53. this.canvasGraphics.drawImage(player.getCurrentImage(),
  54. this.player.getX() - (this.player.getCurrentImage().getWidth() / 2),
  55. this.player.getY() - (this.player.getCurrentImage().getHeight() / 2), null);
  56. // Draw enemies
  57. for (int i = 0; i < this.numEnemies; i++) {
  58. this.canvasGraphics.drawImage(this.enemies[i].getCurrentImage(),
  59. this.enemies[i].getX() - (this.enemies[i].getCurrentImage().getWidth() / 2),
  60. this.enemies[i].getY() - (this.enemies[i].getCurrentImage().getHeight() / 2), null);
  61. }
  62. // Draw goal
  63. this.canvasGraphics.drawImage(this.goal.getCurrentImage(),
  64. this.goal.getX() - (this.goal.getCurrentImage().getWidth() / 2),
  65. this.goal.getY() - (this.goal.getCurrentImage().getHeight() / 2), null);
  66. } catch (Exception e) {
  67. // -----------------------------------//
  68. System.err.println(e.getMessage());
  69. }
  70. // Draw everything.
  71. this.gameGraphics.drawImage(this.canvas, this.borderLeft, this.borderTop, this);
  72. long end = System.nanoTime();
  73. this.gameGraphics.drawString("FPS: " + String.format("%2d", (int) (1000000000.0 / (end - start))),
  74. this.borderLeft + 50, this.borderTop + 50);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement