java数组游戏_Java之迷宫小游戏(二维数组)

importjava.util.Scanner;public classgame1 {static int score = 0; //分数

static int blood = 3; //生命

static int step = 0; //步数//& 玩家//@ 终点

static String[][] arr ={//0 1 2 3 4 5 6 7 8 9

{"#","#","#","#","#","#","#","#","#","#"},//0

{"#","@"," ","#","#"," "," ","&","4","#"},//1

{"#","#"," "," ","#"," ","#","#"," ","#"},//2

{"#","#","#"," "," ","3","#"," "," ","#"},//3

{"#","#","#","#","#","#","#"," "," ","#"},//4

{"#","#","#","#","#","#","#","#","#","#"},//5

};public static void print(String arr1[][]){ //打印地图的方法

for(int i =0;i

System.out.print(arr1[i][j]+"\t");

}

System.out.println();

}

}public static voidmain(String[] args){

Scanner sc= newScanner(System.in);int x=1; //起始位置

int y=7;int ex=1; //终点位置

int ey=1;

System.out.println("迷宫小游戏");

System.out.println("使用WASD移动小人");

System.out.println("只能移动20步 只有三条命");

System.out.println("***************************");

System.out.printf("当前分数:"+score+"\t");

System.out.printf("当前生命:"+blood+"\t");

System.out.println("当前步数:"+step+"\t");

print(arr);while(arr[ex][ey] != "&" & step<=20 & blood !=0){ //到达终点 步数超过20步 生命为0 则跳出循环

String str =sc.nextLine();switch(str){case "w":if (arr[x - 1][y] == "#") { //如果下一步为墙则判断是否还有命

if(blood==0){ break;} //有命则扣血,同时步数增加,无命则跳出循环,宣告死亡

else {blood-=1;step++;break;}

}else if (arr[x - 1][y] == " "){ //如果下一步为路 则将下一步位置设置为小人

arr[x][y] = " "; //同时将小人先前的位置设置为路

x--;

arr[x][y]= "&";

step++;break;}else if (arr[x - 1][y] == "3"){ //如果下一步为金币,则将金币修改为小人

arr[x][y] = " "; //将小人先前位置设置为路

x--; //同时金币数增加,步数加1

arr[x][y] = "&";

score+=3;step++;break;}else if (arr[x - 1][y] == "4"){

arr[x][y]= " ";

x--;

arr[x][y]= "&";

score+=4;step++;break;}else if (arr[x - 1][y] == "@"){ //如果小人下一步为终点,则将终点修改为小人

arr[x][y] = " "; //将小人之前的位置设置为路

x--; //同时步数加1

arr[x][y] = "&";step++;break;}case "a":if (arr[x][y-1] == "#") {if(blood==0){ break;}else {blood-=1;step++;break;}

}else if (arr[x][y-1] == " "){

arr[x][y]= " ";

y--;

arr[x][y]= "&";step++;break;}else if (arr[x][y-1] == "3"){

arr[x][y]= " ";

y--;

arr[x][y]= "&";

score+=3;step++;break;}else if (arr[x][y-1] == "4"){

arr[x][y]= " ";

y--;

arr[x][y]= "&";

score+=4;step++;break;}else if (arr[x][y-1] == "@"){

arr[x][y]= " ";

y--;

arr[x][y]= "&";step++;break;}case "s":if (arr[x+1][y] == "#") {if(blood==0){ break;}else {blood-=1;step++;break;}

}else if (arr[x+1][y] == " "){

arr[x][y]= " ";

x++;

arr[x][y]= "&";step++;break;}else if (arr[x+1][y] == "3"){

arr[x][y]= " ";

x++;

arr[x][y]= "&";

score+=3;break;}else if (arr[x+1][y] == "4"){

arr[x][y]= " ";

x++;

arr[x][y]= "&";

score+=4;step++;break;}else if (arr[x+1][y] == "@"){

arr[x][y]= " ";

x++;

arr[x][y]= "&";step++;break;}case "d":if (arr[x][y+1] == "#") {if(blood==0){ break;}else {blood-=1;step++;break;}

}else if (arr[x][y+1] == " "){

arr[x][y]= " ";

y++;

arr[x][y]= "&";step++;break;}else if (arr[x][y+1] == "3"){

arr[x][y]= " ";

y++;

arr[x][y]= "&";

score+=3;step++;break;}else if (arr[x][y+1] == "4"){

arr[x][y]= " ";

y++;

arr[x][y]= "&";

score+=4;step++;break;}else if (arr[x][y+1] == "@"){

arr[x][y]= " ";

y++;

arr[x][y]= "&";step++;break;}default:break;

}

System.out.println("***************************");

System.out.printf("当前分数:"+score+"\t");

System.out.printf("当前生命:"+blood+"\t");

System.out.println("当前步数:"+step+"\t");

print(arr);}if(step>20){System.out.println("步数超过20,游戏结束");}else if(blood==0) {System.out.println("没有生命了,游戏结束");}else{System.out.println("恭喜你成功闯关");}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值