跳到正文

个人记录

C++小游戏【走迷宫】

lzh009约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年4月23日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1
C++
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <bits/stdc++.h>
using namespace std;
void print(char a[10][10]){
    system("cls");
    for(int i=0;i<5;i++)
    cout<<a[i]<<endl;
}
int main(){
    int x=1,y=1;
    int ch;
    char a[10][10] = {
        "######",
        "#0 # #",
        "#    #",
        "#  #  ",
        "######"
    };
    print(a);
    while(x != 3 || y != 5){
        if (_kbhit()){
            ch = _getch();
            if (ch == 27){ break; }
            if (ch == 97 && a[x][y-1] == ' '){
                a[x][y] = ' ';
                y--;
                a[x][y] = '0';
                print(a);
            }
            if (ch == 100 && a[x][y+1] == ' '){
                a[x][y] = ' ';
                y++;
                a[x][y] = '0';
                print(a);
            }
            if (ch == 115 && a[x+1][y] == ' '){
                a[x][y] = ' ';
                x++;
                a[x][y] = '0';
                print(a);
            }
            if (ch == 119 && a[x-1][y] == ' '){
                a[x][y] = ' ';
                x--;
                a[x][y] = '0';
                print(a);
            }
        }
    }
    system("cls");
    cout<<"you win";
    for( ; ; ) ;
    return 0;
}
原文