记录一下自己以前学习做的一些小项目,本人水平一般,如若各位大佬有什么建议,欢迎一起议论。
java版本的文字小游戏,代码总共分四个部分“游戏地图、读取键盘输入、人物移动、游戏结束判定”
游戏地图
①、创建字符类型数组arr,将地图存储进数组内,数组的长度一定要与编写的地图长度一致;
②、创建“location_x”与“location_y”来记录人物的位置;
③、用循环将地图写入到数组arr内;
④、输出地图;
//地图存储空间
String[][] arr = new String[8][8];
int location_x = 1; //O的x轴位置
int location_y = 0; //O的y轴位置
int step = 0; //输入步数
//游戏地图
String pdd1 = "########";
String pdd2 = "O # #";
String pdd3 = "## # # ";
String pdd4 = "## # ###";
String pdd5 = "# # #";
String pdd6 = "# #### #";
String pdd7 = "# #";
String pdd8 = "########";
//将游戏地图存储到数列内
for ( int x = 0 ; x <= 7 ; x++ ){ arr[0][x] = pdd1.substring(x,x+1); }
for ( int x = 0 ; x <= 7 ; x++ ){ arr[1][x] = pdd2.substring(x,x+1); }
for ( int x = 0 ; x <= 7 ; x++ ){ arr[2][x] = pdd3.substring(x,x+1); }
for ( int x = 0 ; x <= 7 ; x++ ){ arr[3][x] = pdd4.substring(x,x+1); }
for ( int x = 0 ; x <= 7 ; x++ ){ arr[4][x] = pdd5.substring(x,x+1); }
for ( int x = 0 ; x <= 7 ; x++ ){ arr[5][x] = pdd6.substring(x,x+1); }
for ( int x = 0 ; x <= 7 ; x++ ){ arr[6][x] = pdd7.substring(x,x+1); }
for ( int x = 0 ; x <= 7 ; x++ ){ arr[7][x] = pdd8.substring(x,x+1); }
//输出地图
for ( int x = 0; x < arr.length ; x++) {
for ( int y = 0 ; y < arr[x].length ; y++ ){
System.out.print(arr[x][y]); //1111
}
System.out.println();
}
读取键盘输入
//读取输入的值
System.out.println("O是玩家自己,输入W往上走,输入S往下走,输入A往左走,输入D往右走,输入完毕后按回车键结束输入");
Scanner scanner = new Scanner(System.in);
String value = scanner.next();
人物移动
①、对输入的值进行判断,看输入的是“w、s、a、d"中的哪一个;
②、以“w”举例,判断当前人物位置上方的位置是否为墙壁“#”,若上方是墙壁则会提示无法前进,若上方不是墙壁,则向上方移动一个位置;
③、移动完毕后,更新人物的坐标;
//向上方移动
if (value.equals("w")) {
System.out.println("向上移动");
if (arr[location_x - 1][location_y].equals("#")) {
System.out.println("上方位置无法前进,请重新输入。");
} else {
arr[location_x - 1][location_y] = "O";
arr[location_x][location_y] = " ";
location_x = location_x - 1;
}
}
//向下方移动
if (value.equals("s")) {
System.out.println("向下移动");
if (arr[location_x + 1][location_y].equals("#")) {
System.out.println("下方位置无法前进,请重新输入。");
} else {
arr[location_x + 1][location_y] = "O";
arr[location_x][location_y] = " ";
location_x = location_x + 1;
}
}
//向左方移动
if (value.equals("a")) {
if ( location_y - 1 < 0 ){
System.out.println("左方已超出地图范围!! 请重新输入。");
continue;
}
System.out.println("向左移动");
if (arr[location_x][location_y - 1].equals("#")) {
System.out.println("左方位置无法前进,请重新输入。");
} else {
arr[location_x][location_y - 1] = "O";
arr[location_x][location_y] = " ";
location_y = location_y - 1;
}
}
//向右方移动
if (value.equals("d")) {
System.out.println("向右移动");
if (arr[location_x][location_y + 1].equals("#")) {
System.out.println("右方位置无法前进,请重新输入。");
} else {
arr[location_x][location_y + 1] = "O";
arr[location_x][location_y] = " ";
location_y = location_y + 1;
}
}
游戏结束判定
①、判断当前位置是否已到达终点,若是已经到终点,则提示游戏通关,并结束人物移动的循环判断;
//到达终点后结束游戏
if ( location_x == 2 && location_y == 7 ){
System.out.println("恭喜通关!! 游戏结束。");
break;
}
全部代码如下:
package org.example;
import java.util.Scanner;
public class ExcelUtils {
public static void main(String[] args) {
//地图存储空间
String[][] arr = new String[8][8];
int location_x = 1; //O的x轴位置
int location_y = 0; //O的y轴位置
int step = 0; //输入步数
//游戏地图
String pdd1 = "########";
String pdd2 = "O # #";
String pdd3 = "## # # ";
String pdd4 = "## # ###";
String pdd5 = "# # #";
String pdd6 = "# #### #";
String pdd7 = "# #";
String pdd8 = "########";
//将游戏地图存储到数列内
for ( int x = 0 ; x <= 7 ; x++ ){
arr[0][x] = pdd1.substring(x,x+1);
}
for ( int x = 0 ; x <= 7 ; x++ ){
arr[1][x] = pdd2.substring(x,x+1);
}
for ( int x = 0 ; x <= 7 ; x++ ){
arr[2][x] = pdd3.substring(x,x+1);
}
for ( int x = 0 ; x <= 7 ; x++ ){
arr[3][x] = pdd4.substring(x,x+1);
}
for ( int x = 0 ; x <= 7 ; x++ ){
arr[4][x] = pdd5.substring(x,x+1);
}
for ( int x = 0 ; x <= 7 ; x++ ){
arr[5][x] = pdd6.substring(x,x+1);
}
for ( int x = 0 ; x <= 7 ; x++ ){
arr[6][x] = pdd7.substring(x,x+1);
}
for ( int x = 0 ; x <= 7 ; x++ ){
arr[7][x] = pdd8.substring(x,x+1);
}
//输出地图
for ( int x = 0; x < arr.length ; x++) {
for ( int y = 0 ; y < arr[x].length ; y++ ){
System.out.print(arr[x][y]); //1111
}
System.out.println();
}
while (true) {
//读取输入的值
System.out.println("O是玩家自己,输入W往上走,输入S往下走,输入A往左走,输入D往右走,输入完毕后按回车键结束输入");
Scanner scanner = new Scanner(System.in);
String value = scanner.next();
step = step + 1; //步数
//向上方移动
if (value.equals("w")) {
System.out.println("向上移动");
if (arr[location_x - 1][location_y].equals("#")) {
System.out.println("上方位置无法前进,请重新输入。");
} else {
arr[location_x - 1][location_y] = "O";
arr[location_x][location_y] = " ";
location_x = location_x - 1;
}
}
//向下方移动
if (value.equals("s")) {
System.out.println("向下移动");
if (arr[location_x + 1][location_y].equals("#")) {
System.out.println("下方位置无法前进,请重新输入。");
} else {
arr[location_x + 1][location_y] = "O";
arr[location_x][location_y] = " ";
location_x = location_x + 1;
}
}
//向左方移动
if (value.equals("a")) {
if ( location_y - 1 < 0 ){
System.out.println("左方已超出地图范围!! 请重新输入。");
continue;
}
System.out.println("向左移动");
if (arr[location_x][location_y - 1].equals("#")) {
System.out.println("左方位置无法前进,请重新输入。");
} else {
arr[location_x][location_y - 1] = "O";
arr[location_x][location_y] = " ";
location_y = location_y - 1;
}
}
//向右方移动
if (value.equals("d")) {
System.out.println("向右移动");
if (arr[location_x][location_y + 1].equals("#")) {
System.out.println("右方位置无法前进,请重新输入。");
} else {
arr[location_x][location_y + 1] = "O";
arr[location_x][location_y] = " ";
location_y = location_y + 1;
}
}
//输出地图
for (int x = 0; x < arr.length; x++) {
for (int y = 0; y < arr[x].length; y++) {
System.out.print(arr[x][y]); //1111
}
System.out.println();
}
//到达终点后结束游戏
if ( location_x == 2 && location_y == 7 ){
System.out.println("恭喜通关!! 游戏结束。");
break;
}
}
System.out.println("使用步数:"+step);
}
}