Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1.  
  2. import mayflower.*;
  3.  
  4. import java.awt.*;
  5.  
  6.  
  7. public class MainStage extends Stage {
  8. private HealthDisplay healthDisplay;
  9. private Player player;
  10. private Block ground;
  11. private String[][][] map;
  12. private int currentScreen;
  13. private Text scoreText;
  14.  
  15. public MainStage() {
  16. scoreText = new Text("SCORE: "+String.format("%03d", 0), Color.GRAY, 30, "Impact");
  17. addActor(scoreText, 650, 0);
  18. map = new String[][][]{
  19. {
  20. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  21. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  22. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "05_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  23. {"00_00", "00_00", "03_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "04_03", "00_00", "00_00", "00_00", "00_00", "00_00",},
  24. {"00_00", "03_00", "00_00", "00_00", "00_00", "00_00", "03_00", "00_00", "00_00", "01_17", "01_18", "01_19", "00_00", "00_00", "00_00", "00_00",},
  25. {"00_00", "00_00", "00_00", "00_00", "00_00", "01_04", "01_18", "01_06", "00_00", "00_00", "00_00", "00_00", "00_00", "05_00", "00_00", "00_00",},
  26. {"00_00", "00_00", "00_00", "00_00", "02_01", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  27. {"00_00", "00_00", "00_00", "00_00", "02_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "01_17", "01_18", "01_18",},
  28. {"00_00", "00_00", "00_00", "00_00", "02_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  29. {"01_17", "01_18", "01_18", "01_18", "01_05", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  30. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  31. {"00_00", "00_00", "00_00", "00_00", "01_17", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_19", "00_00",},
  32. },
  33. {
  34. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  35. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  36. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  37. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "07_04", "05_00", "00_00", "08_01",},
  38. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "08_00",},
  39. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "01_04", "01_18", "01_18", "01_18", "01_19",},
  40. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "05_00", "00_00", "00_00", "02_01", "00_04", "00_00", "00_00", "00_00", "00_00",},
  41. {"01_18", "01_06", "06_00", "06_00", "01_04", "01_19", "00_00", "00_00", "00_00", "00_01", "02_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  42. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "07_00", "00_00", "04_00", "00_00", "02_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  43. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "01_17", "01_18", "01_18", "01_18", "01_19", "00_00", "00_00", "00_00", "00_00", "00_00",},
  44. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  45. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  46. }, {
  47. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  48. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  49. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "05_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  50. {"00_00", "00_00", "03_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "04_03", "00_00", "00_00", "00_00", "00_00", "00_00",},
  51. {"00_00", "03_00", "00_00", "00_00", "00_00", "00_00", "03_00", "00_00", "00_00", "01_17", "01_18", "01_19", "00_00", "00_00", "00_00", "00_00",},
  52. {"00_00", "00_00", "00_00", "00_00", "00_00", "01_04", "01_18", "01_06", "00_00", "00_00", "00_00", "00_00", "00_00", "05_00", "00_00", "00_00",},
  53. {"00_00", "00_00", "00_00", "00_00", "02_01", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  54. {"00_00", "00_00", "00_00", "00_00", "02_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "01_17", "01_18", "01_18",},
  55. {"00_00", "00_00", "00_00", "00_00", "02_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  56. {"01_17", "01_18", "01_18", "01_18", "01_05", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  57. {"00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00", "00_00",},
  58. {"00_00", "00_00", "00_00", "00_00", "01_17", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_18", "01_19", "00_00",},
  59. },
  60. };
  61. currentScreen = 0;
  62. generateMap();
  63. Picture bgPicture = new Picture("src/img/BG/main.png");
  64. setBackground(bgPicture);
  65. //setBackgroundColor(Color.ORANGE);
  66. player = new Player();
  67. addActor(player, 75, 100);
  68.  
  69.  
  70. //showBounds(true);
  71. HealthDisplay health = new HealthDisplay();
  72. addActor(health, 55, 15);
  73. }
  74. public void update() {
  75.  
  76. Keyboard kb = getKeyboard();
  77.  
  78. Actor[] actors = getActors();
  79. HealthDisplay localHealth = new HealthDisplay();
  80. Player localPlayer = new Player();
  81. for(Actor health : actors){
  82. if(health instanceof HealthDisplay){
  83. //((HealthDisplay) health).adjustHealth(-1);
  84. localHealth = (HealthDisplay) health;
  85. }
  86. }
  87. for(Actor player : actors){
  88. if(player instanceof Player){
  89. //((HealthDisplay) health).adjustHealth(-1);
  90. localPlayer = (Player) player;
  91. }
  92. }
  93.  
  94. if (kb.isKeyPressed("r")) {
  95. localPlayer.setPosition(25, 50);
  96. localPlayer.setGravity(0.0);
  97. clearMap();
  98. generateMap();
  99.  
  100. }
  101. if(localPlayer.getX() > 800){
  102. localPlayer.setPosition(0, localPlayer.getY());
  103. currentScreen ++;
  104. clearMap();
  105. generateMap();
  106. }
  107. if(1500 < localPlayer.getY()){
  108. localPlayer.setPosition(25, 50);
  109. localPlayer.setGravity(0.0);
  110. clearMap();
  111. generateMap();
  112. localHealth.adjustHealth(-1);
  113. }
  114. if(localHealth.getHealth() < 0){
  115. getMayflower().setStage(new GameOver());
  116. }
  117. scoreText.setText("SCORE: "+String.format("%03d", localPlayer.getScore()));
  118. }
  119. public void clearMap() {
  120. Actor[] actors = getActors();
  121. for(Actor actor : actors){
  122. if(!(actor instanceof Player || actor instanceof HealthDisplay || actor instanceof Text)){
  123. this.removeActor(actor);
  124. }
  125. }
  126. }
  127. public void generateMap(){
  128. String[][] world = map[currentScreen];
  129. for (int r = 0; r < world.length; r++) {
  130. for (int c = 0; c < world[0].length; c++) {
  131. int object = Integer.parseInt(world[r][c].split("_")[0]);
  132. int style = Integer.parseInt(world[r][c].split("_")[1]);
  133. int posX = c * 50 + 25;
  134. int posY = r * 50 + 25;
  135. switch (object) {
  136. case 1:
  137. Grass grass = new Grass(style);
  138. addActor(grass, posX, posY);
  139. break;
  140. case 2:
  141. Ladder ladder = new Ladder(style);
  142. addActor(ladder, posX, posY);
  143. break;
  144. case 3:
  145. Spike spike = new Spike();
  146. addActor(spike, posX, posY);
  147. break;
  148. case 4:
  149. Flag flag = new Flag(style);
  150. addActor(flag, posX, posY);
  151. break;
  152. case 5:
  153. Coin coin = new Coin();
  154. addActor(coin, posX, posY);
  155. break;
  156. case 6:
  157. Bridge bridge = new Bridge(style);
  158. addActor(bridge, posX, posY);
  159. break;
  160. case 7:
  161. Slime slime = new Slime(style);
  162. addActor(slime, posX, posY);
  163. break;
  164. case 8:
  165. Door door = new Door(style);
  166. addActor(door, posX, posY);
  167. break;
  168. }
  169.  
  170. }
  171. }
  172. }
  173. public HealthDisplay getHealthDisplay(){
  174. return healthDisplay;
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement