Java
时间: 2024-02-09 11:08:05 浏览: 27
以下是一个基于Java语言编写的简单迷宫鼠游戏代码示例:
```
import java.util.Scanner;
public class MazeRunner {
public static void main(String[] args) {
Maze myMap = new Maze();
intro(myMap);
userMove(myMap);
}
public static void intro(Maze myMap) {
System.out.println("Welcome to Maze Runner!");
System.out.println("Here is your current position:");
myMap.printMap();
}
public static void userMove(Maze myMap) {
Scanner input = new Scanner(System.in);
String userMove = "";
while (!myMap.didIWin()) {
System.out.print("Where would you like to move? (R, L, U, D) ");
userMove = input.next();
switch (userMove) {
case "R":
if (myMap.canIMoveRight()) {
myMap.moveRight();
myMap.printMap();
} else {
System.out.println("Sorry, you've hit a wall.");
}
break;
case "L":
if (myMap.canIMoveLeft()) {
myMap.moveLeft();
myMap.printMap();
} else {
System.out.println("Sorry, you've hit a wall.");
}
break;
case "U":
if (myMap.canIMoveUp()) {
myMap.moveUp();
myMap.printMap();
} else {
System.out.println("Sorry, you've hit a wall.");
}
break;
case "D":
if (myMap.canIMoveDown()) {
myMap.moveDown();
myMap.printMap();
} else {
System.out.println("Sorry, you've hit a wall.");
}
break;
default:
System.out.println("Invalid input. Please enter R, L, U, or D.");
break;
}
}
System.out.println("Congratulations, you made it out of the maze!");
}
}
class Maze {
private String[][] map = {
{"#", "#", "#", "#", "#", "#", "#", "#", "#", "#"},
{"#", " ", " ", " ", "#", " ", " ", " ", " ", "#"},
{"#", " ", "#", " ", "#", " ", "#", "#", " ", "#"},
{"#", " ", "#", " ", " ", " ", " ", " ", " ", "#"},
{"#", " ", "#", "#", "#", "#", "#", "#", " ", "#"},
{"#", " ", " ", " ", " ", " ", " ", " ", " ", "#"},
{"#", "#", "#", "#", "#", "#", "#", "#", "#", "#"}
};
private int currentRow = 1;
private int currentCol = 1;
public void printMap() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
public boolean didIWin() {
if (currentRow == map.length - 2 && currentCol == map[0].length - 2) {
return true;
} else {
return false;
}
}
public boolean canIMoveRight() {
if (map[currentRow][currentCol + 1] == " ") {
return true;
} else {
return false;
}
}
public boolean canIMoveLeft() {
if (map[currentRow][currentCol - 1] == " ") {
return true;
} else {
return false;
}
}
public boolean canIMoveUp() {
if (map[currentRow - 1][currentCol] == " ") {
return true;
} else {
return false;
}
}
public boolean canIMoveDown() {
if (map[currentRow + 1][currentCol] == " ") {
return true;
} else {
return false;
}
}
public void moveRight() {
currentCol++;
}
public void moveLeft() {
currentCol--;
}
public void moveUp() {
currentRow--;
}
public void moveDown() {
currentRow++;
}
}
```
这个示例代码展示了一个简单的迷宫鼠游戏,包括地图、鼠标移动和判断是否获胜等功能。但是这只是一个简单的示例,请注意这个代码可能不足以完全符合实际需要,需要根据实际情况进行修改和完善。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)