Make the following game user friendly with GUI, with some simple graphics. The GUI should be in another seperate class, with some ImageIcon, and Game class should be added into the pane. The following code works as this: The objective of the player is to escape from this labyrinth. The player starts at the bottom left corner of the labyrinth. He has to get to the top right corner of the labyrinth as fast he can, avoiding a meeting with the evil dragon. The player can move only in four directions: left, right, up or down. There are several escape paths in all labyrinths. The player’s character should be able to moved with the well known WASD keyboard buttons. If the dragon gets to a neighboring field of the player, then the player dies. Because it is dark in the labyrinth, the player can see only the neighboring fields at a distance of 3 units. Cell Class: public class Cell { private boolean isWall; public Cell(boolean isWall) { this.isWall = isWall; } public boolean isWall() { return isWall; } public void setWall(boolean isWall) { this.isWall = isWall; } @Override public String toString() { return isWall ? "#" : "."; } } Labyrinth Class: import java.util.Random; public class Labyrinth { private final int size; private final Cell[][] grid; public Labyrinth(int size) { this.size = size; this.grid = new Cell[size][size]; generateLabyrinth(); } private void generateLabyrinth() { Random rand = new Random(); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { // Randomly create walls and paths grid[i][j] = new Cell(rand.nextBoolean()); } } // Ensure start and end are open paths grid[0][0].setWall(false); grid[size - 1][size - 1].setWall(false); } public Cell getCell(int x, int y) { return grid[x][y]; } public int getSize() { return size; } public void display(Player player, Dragon dragon) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == player.getX() && j == player.getY()) { System.out.print("P "); } else if (i == dragon.getX() && j == dragon.getY()) { System.out.print("D "); } else { System.out.print(grid[i][j] + " "); } } System.out.println(); } } } Player Class: public class Player { private int x; private int y; public Player(int startX, int startY) { this.x = startX; this.y = startY; } public int getX() { return x; } public int getY() { return y; } public void move(char direction, Labyrinth labyrinth) { int newX = x; int newY = y; switch (direction) { case 'W': newX--; break; // Move up case 'S': newX++; break; // Move down case 'A': newY--; break; // Move left case 'D': newY++; break; // Move right } if (newX >= 0 && newX < labyrinth.getSize() && newY >= 0 && newY < labyrinth.getSize() && !labyrinth.getCell(newX, newY).isWall()) { x = newX; y = newY; } } } Dragon Class: import java.util.Random; public class Dragon { private int x; private int y; private char direction; private final Random rand = new Random(); public Dragon(int startX, int startY) { this.x = startX; this.y = startY; chooseDirection(); } public int getX() { return x; } public int getY() { return y; } public void move(Labyrinth labyrinth) { int newX = x; int newY = y; switch (direction) { case 'W': newX--; break; case 'S': newX++; break; case 'A': newY--; break; case 'D': newY++; break; } if (newX >= 0 && newX < labyrinth.getSize() && newY >= 0 && newY < labyrinth.getSize() && !labyrinth.getCell(newX, newY).isWall()) { x = newX; y = newY; } else { chooseDirection(); // Choose a new direction if blocked } } private void chooseDirection() { char[] directions = {'W', 'S', 'A', 'D'}; direction = directions[rand.nextInt(directions.length)]; } } Game Class: import java.util.Scanner; public class Game { public static void main(String[] args) { int size = 10; // Example size Labyrinth labyrinth = new Labyrinth(size); Player player = new Player(0, 0); Dragon dragon = new Dragon(size - 1, size - 1); Scanner scanner = new Scanner(System.in); boolean running = true; while (running) { labyrinth.display(player, dragon); if (player.getX() == size - 1 && player.getY() == size - 1) { System.out.println("Congratulations! You escaped the labyrinth!"); break; } if (Math.abs(player.getX() - dragon.getX()) <= 1 && Math.abs(player.getY() - dragon.getY()) <= 1) { System.out.println("The dragon caught you! Game Over!"); break; } System.out.print("Move (W/A/S/D): "); char move = scanner.next().toUpperCase().charAt(0); player.move(move, labyrinth); dragon.move(labyrinth); } scanner.close(); } }
Make the following game user friendly with GUI, with some simple graphics. The GUI should be in another seperate class, with some ImageIcon, and Game class should be added into the pane.
The following code works as this: The objective of the player is to escape from this labyrinth. The player starts at the bottom left corner of the labyrinth. He has to get to the top right corner of the labyrinth as fast he can, avoiding a meeting with the evil dragon. The player can move only in four directions: left, right, up or down. There are several escape paths in all labyrinths. The player’s character should be able to moved with the well known WASD keyboard buttons. If the dragon gets to a neighboring field of the player, then the player dies. Because it is dark in the labyrinth, the player can see only the neighboring fields at a distance of 3 units.
Cell Class:
public class Cell { private boolean isWall; public Cell(boolean isWall) { this.isWall = isWall; } public boolean isWall() { return isWall; } public void setWall(boolean isWall) { this.isWall = isWall; } @Override public String toString() { return isWall ? "#" : "."; } }
Labyrinth Class:
import java.util.Random; public class Labyrinth { private final int size; private final Cell[][] grid; public Labyrinth(int size) { this.size = size; this.grid = new Cell[size][size]; generateLabyrinth(); } private void generateLabyrinth() { Random rand = new Random(); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { // Randomly create walls and paths grid[i][j] = new Cell(rand.nextBoolean()); } } // Ensure start and end are open paths grid[0][0].setWall(false); grid[size - 1][size - 1].setWall(false); } public Cell getCell(int x, int y) { return grid[x][y]; } public int getSize() { return size; } public void display(Player player, Dragon dragon) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == player.getX() && j == player.getY()) { System.out.print("P "); } else if (i == dragon.getX() && j == dragon.getY()) { System.out.print("D "); } else { System.out.print(grid[i][j] + " "); } } System.out.println(); } } }
Player Class:
public class Player { private int x; private int y; public Player(int startX, int startY) { this.x = startX; this.y = startY; } public int getX() { return x; } public int getY() { return y; } public void move(char direction, Labyrinth labyrinth) { int newX = x; int newY = y; switch (direction) { case 'W': newX--; break; // Move up case 'S': newX++; break; // Move down case 'A': newY--; break; // Move left case 'D': newY++; break; // Move right } if (newX >= 0 && newX < labyrinth.getSize() && newY >= 0 && newY < labyrinth.getSize() && !labyrinth.getCell(newX, newY).isWall()) { x = newX; y = newY; } } }
Dragon Class:
import java.util.Random; public class Dragon { private int x; private int y; private char direction; private final Random rand = new Random(); public Dragon(int startX, int startY) { this.x = startX; this.y = startY; chooseDirection(); } public int getX() { return x; } public int getY() { return y; } public void move(Labyrinth labyrinth) { int newX = x; int newY = y; switch (direction) { case 'W': newX--; break; case 'S': newX++; break; case 'A': newY--; break; case 'D': newY++; break; } if (newX >= 0 && newX < labyrinth.getSize() && newY >= 0 && newY < labyrinth.getSize() && !labyrinth.getCell(newX, newY).isWall()) { x = newX; y = newY; } else { chooseDirection(); // Choose a new direction if blocked } } private void chooseDirection() { char[] directions = {'W', 'S', 'A', 'D'}; direction = directions[rand.nextInt(directions.length)]; } }
Game Class:
import java.util.Scanner; public class Game { public static void main(String[] args) { int size = 10; // Example size Labyrinth labyrinth = new Labyrinth(size); Player player = new Player(0, 0); Dragon dragon = new Dragon(size - 1, size - 1); Scanner scanner = new Scanner(System.in); boolean running = true; while (running) { labyrinth.display(player, dragon); if (player.getX() == size - 1 && player.getY() == size - 1) { System.out.println("Congratulations! You escaped the labyrinth!"); break; } if (Math.abs(player.getX() - dragon.getX()) <= 1 && Math.abs(player.getY() - dragon.getY()) <= 1) { System.out.println("The dragon caught you! Game Over!"); break; } System.out.print("Move (W/A/S/D): "); char move = scanner.next().toUpperCase().charAt(0); player.move(move, labyrinth); dragon.move(labyrinth); } scanner.close(); } }
Step by step
Solved in 2 steps