迷宫(加入金币)

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <cstdlib> 
 
using namespace std;

char maze[15][35] = {
    "###X############################",
    "#R $  $X$    $               $ #",
    "#XX  $X3X$    $      $         #",
    "#XX##############XX#########X X#",
    "#$   O      X             $  $ #",
    "#$$  O ##########    $   $ $   #",
    "#$$  X        $   X     $X X$ #E",
    "#   $X     X  $  X+X     O O   #",
    "#    $            X      X X   #",
    "##########################E#####",
    "3=???                           ",
    
};
char maze2[15][35] = {
    "###X############################",
    "#R X  X$X    X               X #",
    "#$X  X$3$X    X      X         #",
    "#$X##############$$#########$ $#",
    "#X   O      X             $  $ #",
    "#XX  O ##########    $   $ $   #",
    "#XX  $        X   X     $X X$ #E",
    "#   X$     $  X  X+X     O O   #",
    "#    X            X      X X   #",
    "##########################E#####",
    "3=???                           ",
    
};

int main()
{
    for(int i=0;i<=99;i++){
        Sleep(0.1);
        cout<<"                  加载中  "<<i<<"%";
        system("cls");
    }
    cout<<"                  加载中  "<<99<<"%";
    Sleep(5000);
    system("cls");
    cout<<"                  加载中  "<<100<<"%";
    Sleep(100);
    system("cls");
    cout<<endl<<endl<<endl<<"                         小学生出品";
    Sleep(3000);
    system("cls");
    char op;
    int x = 1, y = 1,s=5;
    int ex = 3;
    int ey = 2;
    long long score = 0;
    srand(time(0));
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 35; j++) {
            cout << maze[i][j] << " ";
        }
        cout << endl;
    }
    cout<<"提示:X=减生命,O=传送(地点未知),s=钱,E=终点"<<endl<<"有一个快速终点处哦!"<<endl; 
    cout<<"生命:"<<s<<endl;
    cout << "score:" << score << endl;
    bool done = false;
    while (true) {
        // 主角移动 
        op = getch();
        if (op == 'w' && maze[x-1][y] != '#') {
            maze[x][y] = ' ';
            x--;
        }
        else if (op == 's' && maze[x+1][y] != '#') {
            maze[x][y] = ' ';
            x++;
        }
        else if (op == 'a' && maze[x][y-1] != '#') {
            maze[x][y] = ' ';
            y--;
        }
        else if (op == 'd' && maze[x][y+1] != '#') {
            maze[x][y] = ' ';
            y++;
        }
        else if (op == 'f' && maze[x][y+3] != '#') {
            maze[x][y] = ' ';
            y+=3;
        }
        else if (op == 'q' && maze[x][y-3] != '#') {
            maze[x][y] = ' ';
            y-=3;
        }
        else if (op == 'e' && maze[x-3][y] != '#') {
            maze[x][y] = ' ';
            x-=3;
        }
        else if (op == 'x' && maze[x+1][y] != '#'&& maze[x-1][y] != '#'&& maze[x][y+1] != '#'&& maze[x][y-1] != '#') {
            maze[x+1][y] = ' ';
            maze[x][y+1] = ' ';
            maze[x-1][y] = ' ';
            maze[x][y-1] = ' ';
        }
        else if (op == 'z' && maze[x+3][y] != '#') {
            maze[x][y] = ' ';
            x+=3;
        }
        else {
            continue;
        }
        // 判断是否主动走到敌人脸上 
        if (maze[x][y] == 'O') {
        x=1;
        y=1;}
        if(((x==1&&y==7)||(x==2&&y==6)||(x==2&&y==8))&&maze[x][y]=='X')s--;
        if (maze[x][y] == 'X') s-=1;
        if(x==0&&y==3) {
        done=true;maze[x][y]='R';s++;break;}
        // 判断是否走到终点 
        if (maze[x][y] == 'E') {
            maze[x][y] = 'R';
            done = true;
            break;
        }
        if(maze[x][y]=='+'){
            s+=1;
        }
        else if(s<=0){
            done=false;
            break;
        }
        // 判断是否吃到钱 
        if (maze[x][y] == '$') score++;
        if (maze[x][y] == '3') {
        score+=1000000000000;
        s+=2;}
        // 设置主角在地图上的位置
        maze[x][y] = 'R';
        // 敌人随机移动 
        int eop = rand()%4;
        maze[ex][ey] = ' ';
        if (eop == 0 && maze[ex-1][ey] != '#'&& maze[ex-1][ey] != '$') {
            ex--;
        } 
        else if (eop == 1 && maze[ex+1][ey] != '#'&& maze[ex+1][ey] != '$') {
            ex++;
        } 
        else if (eop == 2 && maze[ex][ey-1] != '#'&& maze[ex][ey-1] != '$') {
            ey--;
        } 
        else if (eop == 3 && maze[ex][ey+1] != '#'&& maze[ex][ey+1] != '$') {
            ey++;
        }
        
        maze[ex][ey] = 'X';
        // 敌人移动之后判定是否死亡 
        if (ex == x && ey == y) break;
        // 正常移动,更新并输出地图 
        system("cls");
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 35; j++) {
                cout << maze[i][j] << " ";
            }
            cout << endl;
        }
    cout<<"生命:"<<s<<endl; 
        cout << "score:" << score << endl;
    }
    // 游戏结束,更新一次地图
    system("cls");
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 35; j++) {
            cout << maze[i][j] << " ";
        }
        cout << endl;
    }
    cout << "--------------------------------" << endl;
    if (done) cout << "You win!" << endl;
    else cout << "You failed." << endl;
    cout<<"生命:"<<s<<endl; 
    cout << "Your score:" << score<<endl;
    if(score>=100&&s>5){
        cout<<"***";
    }
    else if(score>=20&&score<100)cout<<"**";
    else cout<<"*"<<endl;
    cout<<"第二关";
    //___________________________________------------------------------------
     x = 1; y = 1;s=5;
    ex = 3;
    ey = 2;
    score = 0;
    srand(time(0));
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 35; j++) {
            cout << maze2[i][j] << " ";
        }
        cout << endl;
    }
    cout<<"提示:X=减生命,O=传送(地点未知),$=钱,E=终点"<<endl<<"有一个快速终点处哦!"<<endl; 
    cout<<"生命:"<<s<<endl;
    cout << "score:" << score << endl;
    done = false;
    while (true) {
        // 主角移动 
        op = getch();
        if (op == 'w' && maze2[x-1][y] != '#') {
            maze2[x][y] = ' ';
            x--;
        }
        else if (op == 's' && maze[x+1][y] != '#') {
            maze2[x][y] = ' ';
            x++;
        }
        else if (op == 'a' && maze[x][y-1] != '#') {
            maze2[x][y] = ' ';
            y--;
        }
        else if (op == 'd' && maze[x][y+1] != '#') {
            maze2[x][y] = ' ';
            y++;
        }
        else if (op == 'f' && maze[x][y+3] != '#') {
            maze2[x][y] = ' ';
            y+=3;
        }
        else if (op == 'q' && maze[x][y-3] != '#') {
            maze2[x][y] = ' ';
            y-=3;
        }
        else if (op == 'e' && maze[x-3][y] != '#') {
            maze2[x][y] = ' ';
            x-=3;
        }
        else if (op == 'x' && maze[x+1][y] != '#'&& maze[x-1][y] != '#'&& maze[x][y+1] != '#'&& maze[x][y-1] != '#') {
            maze2[x+1][y] = ' ';
            maze2[x][y+1] = ' ';
            maze2[x-1][y] = ' ';
            maze2[x][y-1] = ' ';
        }
        else if (op == 'z' && maze[x+3][y] != '#') {
            maze2[x][y] = ' ';
            x+=3;
        }
        else {
            continue;
        }
        // 判断是否主动走到敌人脸上 
        if (maze2[x][y] == 'O') {
        x=1;
        y=1;}
        if(((x==1&&y==7)||(x==2&&y==6)||(x==2&&y==8))&&maze2[x][y]=='X')s--;
        if (maze2[x][y] == 'X') s-=1;
        if(x==0&&y==3) {
        done=true;maze2[x][y]='R';s++;break;}
        // 判断是否走到终点 
        if (maze2[x][y] == 'E') {
            maze2[x][y] = 'R';
            done = true;
            break;
        }
        if(maze2[x][y]=='+'){
            s+=1;
        }
        else if(s<=0){
            done=false;
            break;
        }
        // 判断是否吃到钱 
        if (maze2[x][y] == '$') score++;
        if (maze2[x][y] == '3') {
        score+=1000000000000;
        s+=2;}
        // 设置主角在地图上的位置
        maze2[x][y] = 'R';
        // 敌人随机移动 
        int eop = rand()%4;
        maze2[ex][ey] = ' ';
        if (eop == 0 && maze2[ex-1][ey] != '#'&& maze2[ex-1][ey] != '$') {
            ex--;
        } 
        else if (eop == 1 && maze2[ex+1][ey] != '#'&& maze2[ex+1][ey] != '$') {
            ex++;
        } 
        else if (eop == 2 && maze2[ex][ey-1] != '#'&& maze2[ex][ey-1] != '$') {
            ey--;
        } 
        else if (eop == 3 && maze2[ex][ey+1] != '#'&& maze2[ex][ey+1] != '$') {
            ey++;
        }
        
        maze2[ex][ey] = 'X';
        // 敌人移动之后判定是否死亡 
        if (ex == x && ey == y) break;
        // 正常移动,更新并输出地图 
        system("cls");
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 35; j++) {
                cout << maze2[i][j] << " ";
            }
            cout << endl;
        }
    cout<<"生命:"<<s<<endl; 
        cout << "score:" << score << endl;
    }
    // 游戏结束,更新一次地图
    system("cls");
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 35; j++) {
            cout << maze2[i][j] << " ";
        }
        cout << endl;
    }
    cout << "--------------------------------" << endl;
    if (done) cout << "You win!" << endl;
    else cout << "You failed." << endl;
    cout<<"生命:"<<s<<endl; 
    cout << "Your score:" << score<<endl;
    if(score>=100&&s>5){
        cout<<"***";
    }
    else if(score>=20&&score<100)cout<<"**";
    else cout<<"*";
    Sleep(5000);
    return 0;
}

//好了,今天的代码就到此结束了,各位点个赞呗。

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一个简单的Java迷宫游戏代码,并加入计时功能。以下是代码: ```java import java.util.Scanner; public class MazeGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[][] maze = { // 迷宫地图 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 1, 0, 0, 0, 1, 0, 1}, {1, 0, 0, 1, 0, 0, 0, 1, 0, 1}, {1, 0, 0, 0, 0, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1, 1, 0, 1}, {1, 1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; int startX = 1; // 起点坐标 int startY = 1; int endX = 8; // 终点坐标 int endY = 8; int x = startX; int y = startY; int stepCount = 0; // 步数计数器 long startTime = System.currentTimeMillis(); // 计时开始 while (x != endX || y != endY) { maze[x][y] = 2; // 标记已走过的路 System.out.println("当前位置:(" + x + "," + y + ")"); System.out.print("请输入方向(上:W,下:S,左:A,右:D):"); String direction = scanner.next(); switch (direction) { case "W": if (maze[x - 1][y] == 0) { x--; stepCount++; } break; case "S": if (maze[x + 1][y] == 0) { x++; stepCount++; } break; case "A": if (maze[x][y - 1] == 0) { y--; stepCount++; } break; case "D": if (maze[x][y + 1] == 0) { y++; stepCount++; } break; default: System.out.println("请输入正确的方向!"); break; } if (maze[x][y] == 1) { // 撞墙了 System.out.println("很遗憾,您撞墙了!"); break; } } long endTime = System.currentTimeMillis(); // 计时结束 long timeElapsed = endTime - startTime; // 计算用时(毫秒) System.out.println("恭喜您成功走出迷宫!用时:" + timeElapsed / 1000.0 + "秒,步数:" + stepCount); } } ``` 这个迷宫游戏使用二维数组表示地图,1表示墙,0表示路,2表示已走过的路。玩家通过控制台输入上下左右方向键移动,遇到墙自动停止。当到达终点时,游戏结束,输出用时和步数。 我在代码中添加了计时功能,使用`System.currentTimeMillis()`方法获取当前时间,用于计算游戏用时。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值