c++ 游戏代码(1)

迷宫代码如下:

#include <iostream>
#include <windows.h>
#include <conio.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";
    return 0;
}

  运行如下:

 

 

 用a,s,w,d按键控制小球:

 移动代码如下:

#include <iostream>
#include <conio.h>
using namespace std;
void print(){
    cout<<"12345"<<endl<<"|||||"<<endl;
}
int main(){
    int ch,x=0;
    print();
    cout<<"*";
     while(true){
        if (_kbhit()){
            ch = _getch();
            if(ch==100 && x < 4){
                x++;
                system("cls");
                print();
                for(int i = 0;i < x;i++){
                    cout<<" ";
                }
                cout<<"*";
            }
            if(ch==97 && x > 0){
                x--;
                system("cls");
                print();
                for(int i = 0;i < x;i++){
                    cout<<" ";
                }
                cout<<"*";
            }
        }
    }
    return 0;
}

运行如下:

 

 用a,d按键控制小球

 

posted @ 2020-11-13 16:02  KevinLikesCoding  阅读(8948)  评论(0编辑  收藏  举报