JAVA小游戏,走迷宫(文字版)

本文描述了一个简单的Java程序,通过控制台构建了一个文字冒险游戏,玩家通过键盘输入控制角色在预设的地图中移动,直到达到终点。游戏包括地图初始化、键盘输入处理、角色移动逻辑和游戏结束条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一下自己以前学习做的一些小项目,本人水平一般,如若各位大佬有什么建议,欢迎一起议论。

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);
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值