Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pkj1;
- import javafx.scene.shape.Rectangle;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- import javafx.scene.paint.Color;
- import javafx.scene.layout.GridPane;
- import javafx.geometry.Insets;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.input.KeyEvent;
- public class Maze extends Application {
- public Rectangle r;
- private static int x = 7;
- private static int y = 1;
- private int score = 0;
- private int Food1X = 5, Food1Y = 3, Food2X = 2, Food2Y = 1, Food3X = 3, Food3Y = 5;
- private boolean flag1 = true;
- private ImageView Player = new ImageView(new Image("Pictures/Bangara.bmp"));
- private ImageView Food1 = new ImageView(new Image("Pictures/Orangepng.png"));
- private ImageView Food2 = new ImageView(new Image("Pictures/Orangepng.png"));
- private ImageView Food3 = new ImageView(new Image("Pictures/Orangepng.png"));
- private ImageView Enemy = new ImageView(new Image("Pictures/enemypng.png"));
- private Stage primaryStage;
- private GridPane grid;
- /**
- * please add private access modifer
- * please choose descriptive names
- * please follow java naming conventions
- * 3shan l mo3eed my8lessh :D
- * don't forget menna shatra ;)
- * don't forget to remove this comment :D
- */
- @Override
- public void start(Stage primaryStage) {
- this.primaryStage = primaryStage;
- GridPane h = new GridPane();
- this.grid = h;
- Player.setFitHeight(50);
- Player.setFitWidth(50);
- Food1.setFitHeight(50);
- Food1.setFitWidth(50);
- Food2.setFitHeight(50);
- Food2.setFitWidth(50);
- Food3.setFitHeight(50);
- Food3.setFitWidth(50);
- Enemy.setFitHeight(50);
- Enemy.setFitWidth(50);
- int m[][] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
- { 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1 }, { 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 },
- { 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 }, { 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 },
- { 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1 }, { 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1 } };
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 11; j++) {
- if (m[i][j] == 1) {
- r = drawWall(i, j);
- h.add(r, i, j);
- } else if (m[i][j] == 0) {
- r = drawFloor(i, j);
- h.add(r, i, j);
- }
- }
- }
- h.add(Enemy, 5, 5);
- if (flag1 == true) {
- h.add(Food1, Food1X, Food1Y);
- }
- h.add(Food2, Food2X, Food2Y);
- h.add(Food3, Food3X, Food3Y);
- h.add(Player, x, y);
- Player.setOnKeyPressed(e -> {
- switch (e.getCode()) {
- case RIGHT: {
- if (m[x + 1][y] != 1) {
- x = x + 1;
- h.add(Player, x, y);
- } else if (m[x + 1][y] == m[Food1X][Food1Y]) {
- h.getChildren().remove(Food1);
- }
- break;
- }
- case LEFT: {
- if (m[x - 1][y] != 1) {
- x = x - 1;
- h.add(Player, x, y);
- } else if (m[x - 1][y] == m[Food1X][Food1Y]) {
- }
- break;
- }
- case UP: {
- if (m[x][y - 1] != 1) {
- y = y - 1;
- h.add(Player, x, y);
- } else if (m[x][y - 1] == m[Food1X][Food1Y]) {
- h.getChildren().remove(Food1);
- }
- break;
- }
- case DOWN: {
- if (m[x][y + 1] != 1) {
- y = y + 1;
- h.add(Player, x, y);
- } else if (m[x][y + 1] == m[Food1X][Food1Y]) {
- h.getChildren().remove(Food1);
- }
- break;
- }
- }
- });
- Scene s = new Scene(h, 400, 570);
- primaryStage.setTitle("Maze");
- primaryStage.setScene(s);
- primaryStage.show();
- Player.requestFocus();
- }
- public static void main(String[] args) {
- Application.launch(args);
- }
- private Rectangle drawWall(int i, int j) {
- Rectangle r = new Rectangle(i, j, 50, 50);
- r.setFill(Color.BLUE);
- r.setStroke(Color.BLACK);
- return r;
- }
- private void removeFood(int row, int column) {
- /*
- * assuming the coordinate for the grid square touched by the player
- * now the grid pane is accessible through out all the of the class
- * this.grid
- * just call this method by the desired indexes parameter
- * remove the image from the pane
- */
- start(this.primaryStage);
- }
- private Rectangle drawFloor(int i, int j) {
- Rectangle r = new Rectangle(i, j, 50, 50);
- r.setFill(Color.YELLOW);
- r.setStroke(Color.YELLOW);
- return r;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement