Advertisement
FNSY

Untitled

May 19th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. package pkj1;
  2.  
  3. import javafx.scene.shape.Rectangle;
  4. import javafx.application.Application;
  5. import javafx.scene.Scene;
  6. import javafx.stage.Stage;
  7. import javafx.scene.paint.Color;
  8. import javafx.scene.layout.GridPane;
  9. import javafx.geometry.Insets;
  10. import javafx.scene.image.Image;
  11. import javafx.scene.image.ImageView;
  12. import javafx.scene.input.KeyEvent;
  13.  
  14. public class Maze extends Application {
  15. public Rectangle r;
  16. private static int x = 7;
  17. private static int y = 1;
  18. private int score = 0;
  19. private int Food1X = 5, Food1Y = 3, Food2X = 2, Food2Y = 1, Food3X = 3, Food3Y = 5;
  20. private boolean flag1 = true;
  21. private ImageView Player = new ImageView(new Image("Pictures/Bangara.bmp"));
  22. private ImageView Food1 = new ImageView(new Image("Pictures/Orangepng.png"));
  23. private ImageView Food2 = new ImageView(new Image("Pictures/Orangepng.png"));
  24. private ImageView Food3 = new ImageView(new Image("Pictures/Orangepng.png"));
  25. private ImageView Enemy = new ImageView(new Image("Pictures/enemypng.png"));
  26. private Stage primaryStage;
  27. private GridPane grid;
  28. /**
  29. * please add private access modifer
  30. * please choose descriptive names
  31. * please follow java naming conventions
  32. * 3shan l mo3eed my8lessh :D
  33. * don't forget menna shatra ;)
  34. * don't forget to remove this comment :D
  35. */
  36. @Override
  37. public void start(Stage primaryStage) {
  38. this.primaryStage = primaryStage;
  39. GridPane h = new GridPane();
  40. this.grid = h;
  41. Player.setFitHeight(50);
  42. Player.setFitWidth(50);
  43. Food1.setFitHeight(50);
  44. Food1.setFitWidth(50);
  45. Food2.setFitHeight(50);
  46. Food2.setFitWidth(50);
  47. Food3.setFitHeight(50);
  48. Food3.setFitWidth(50);
  49. Enemy.setFitHeight(50);
  50. Enemy.setFitWidth(50);
  51. int m[][] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  52. { 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1 }, { 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 },
  53. { 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 }, { 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 },
  54. { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1 }, { 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1 } };
  55.  
  56. for (int i = 0; i < 8; i++) {
  57. for (int j = 0; j < 11; j++) {
  58. if (m[i][j] == 1) {
  59. r = drawWall(i, j);
  60. h.add(r, i, j);
  61.  
  62. } else if (m[i][j] == 0) {
  63. r = drawFloor(i, j);
  64. h.add(r, i, j);
  65. }
  66. }
  67. }
  68.  
  69. h.add(Enemy, 5, 5);
  70. if (flag1 == true) {
  71. h.add(Food1, Food1X, Food1Y);
  72. }
  73. h.add(Food2, Food2X, Food2Y);
  74. h.add(Food3, Food3X, Food3Y);
  75. h.add(Player, x, y);
  76.  
  77. Player.setOnKeyPressed(e -> {
  78. switch (e.getCode()) {
  79. case RIGHT: {
  80. if (m[x + 1][y] != 1) {
  81. x = x + 1;
  82. h.add(Player, x, y);
  83. } else if (m[x + 1][y] == m[Food1X][Food1Y]) {
  84. h.getChildren().remove(Food1);
  85.  
  86. }
  87. break;
  88. }
  89. case LEFT: {
  90. if (m[x - 1][y] != 1) {
  91. x = x - 1;
  92. h.add(Player, x, y);
  93. } else if (m[x - 1][y] == m[Food1X][Food1Y]) {
  94.  
  95. }
  96.  
  97. break;
  98. }
  99. case UP: {
  100. if (m[x][y - 1] != 1) {
  101. y = y - 1;
  102. h.add(Player, x, y);
  103. } else if (m[x][y - 1] == m[Food1X][Food1Y]) {
  104.  
  105. h.getChildren().remove(Food1);
  106.  
  107. }
  108.  
  109. break;
  110. }
  111.  
  112. case DOWN: {
  113. if (m[x][y + 1] != 1) {
  114. y = y + 1;
  115. h.add(Player, x, y);
  116. } else if (m[x][y + 1] == m[Food1X][Food1Y]) {
  117. h.getChildren().remove(Food1);
  118. }
  119. break;
  120. }
  121.  
  122. }
  123. });
  124.  
  125. Scene s = new Scene(h, 400, 570);
  126. primaryStage.setTitle("Maze");
  127. primaryStage.setScene(s);
  128. primaryStage.show();
  129. Player.requestFocus();
  130. }
  131.  
  132. public static void main(String[] args) {
  133. Application.launch(args);
  134. }
  135.  
  136. private Rectangle drawWall(int i, int j) {
  137. Rectangle r = new Rectangle(i, j, 50, 50);
  138. r.setFill(Color.BLUE);
  139. r.setStroke(Color.BLACK);
  140. return r;
  141. }
  142.  
  143.  
  144. private void removeFood(int row, int column) {
  145. /*
  146. * assuming the coordinate for the grid square touched by the player
  147. * now the grid pane is accessible through out all the of the class
  148. * this.grid
  149. * just call this method by the desired indexes parameter
  150. * remove the image from the pane
  151. */
  152. start(this.primaryStage);
  153. }
  154.  
  155. private Rectangle drawFloor(int i, int j) {
  156. Rectangle r = new Rectangle(i, j, 50, 50);
  157. r.setFill(Color.YELLOW);
  158. r.setStroke(Color.YELLOW);
  159. return r;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement