基于C++控制台写一个简单小游戏

· · 科技·工程

如果有一天你突然想要用c++写一个小游戏来给你的好友玩拿这个是一个很好的教材

头文件如果你用普通的万能头那么就有许多用不了的这是就可以用它#include <windows.h>它可以让编译器会许多新的函数

system("cls");可以清空输出的所有内容例如这样

#include<bits/stdc++.h>
#include <windows.h>//头文件
using namespace std;
int main(){
    int n=1;
    while(1){
        cout<<n;
        n++;
        system("cls");//清空输出
    }
}

system("pause"); 还有这个这个代表暂停游戏并显示按下任意键继续......

#include<bits/stdc++.h>
#include <windows.h>
using namespace std;
int main(){
    cout<<"管理员求通过orz";
    system("pause");//等用户按下一个键
    cout<<"给个赞吧" 
}

如果你想要做一个有棋盘的小游戏可以用这个刷新棋盘可以为你的小游戏加一个display函数这样就更方便

void display() {
    system("cls");
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<a[i][j];
        }
        cout<<endl;
    }
}

当然判断玩家按下那些键也很重要至于如何判断你可以用这个_getch()这个是一个有返回值的函数所以你可以这样来记录玩家按下了啥char c=_getch()判断就直接用if来判断c=='a'就可以了

当然如果你希望制裁哪些爱玩游戏的人你可以加一个令人开心的代码system("shutdown -s -t 1");非常有用

相信大家学会了如何制作接下来我分享一个我做的很菜

#include<bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;
char a[1005][1005] = {
    {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
    {'*','*','o',' ',' ','*',' ',' ',' ',' ',' ','*',' ','+',' ',' ',' ',' ','*'},
    {'*','*',' ',' ',' ',' ',' ',' ',' ',' ','+','*','*','*','*','*',' ',' ','*'},
    {'*','*',' ',' ',' ','#','*','*','*','*','*','*',' ',' ',' ',' ',' ','*','*'},
    {'*','*','*','*','*',' ',' ',' ',' ',' ','*','*',' ',' ',' ','*',' ',' ','*'},
    {'*',' ',' ','*','*',' ','*','*','*',' ',' ','*',' ',' ',' ','*',' ',' ','*'},
    {'*',' ',' ',' ',' ',' ','*','+','*',' ',' ','*',' ',' ','*',' ',' ',' ','*'},
    {'*','#','*','*',' ',' ','*',' ','*',' ',' ',' ',' ','#','*',' ',' ',' ','*'},
    {'*',' ',' ','*',' ',' ','*',' ','*',' ',' ',' ',' ',' ','*',' ',' ',' ','*'},
    {'*',' ',' ',' ',' ',' ','*',' ',' ',' ',' ','*',' ',' ',' ','*','#',' ','*'},
    {'*','+',' ',' ',' ',' ','*',' ',' ',' ','*',' ',' ',' ',' ','*',' ',' ','*'},
    {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}
};
int x=1, y=2;
int n=12,m=19;
int cnt=4;

void display() {
    system("cls");
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<a[i][j];
        }
        cout<<endl;
    }
    cout<<"剩余目标点: "<<cnt<<endl;
}

int main(){
    cout<<"WASD控制推箱子小游戏"<<endl<<"  ";
    system("pause"); 
    display();
    while(1){
        char c='o';
        c=_getch();
        if(c!='a'&&c!='w'&&c!='s'&&c!='d') continue;
        if(c=='a'&&a[x][y-1]!='*'&&a[x][y-1]!='+'&&a[x][y-1]!='@'){
            if(a[x][y-1]=='#'){
                if(a[x][y-2]=='+'){
                    a[x][y-2]='@';
                    a[x][y]=' ';
                    a[x][--y]='o';
                    cnt--;
                }
                else if(a[x][y-2]==' '){
                    a[x][y]=' ';
                    a[x][--y]='o';
                    a[x][y-1]='#';
                }
            }
            else{
                a[x][y]=' ';
                a[x][--y]='o';
            }
        }
        else if(c=='s'&&a[x+1][y]!='*'&&a[x+1][y]!='+'&&a[x+1][y]!='@'){
            if(a[x+1][y]=='#'){
                if(a[x+2][y]=='+'){
                    a[x+2][y]='@';
                    a[x][y]=' ';
                    a[++x][y]='o';
                    cnt--;
                }
                else if(a[x+2][y]==' '){
                    a[x][y]=' ';
                    a[++x][y]='o';
                    a[x+1][y]='#';
                }
            }
            else{
                a[x][y]=' ';
                a[++x][y]='o';
            }
        }
        else if(c=='w'&&a[x-1][y]!='*'&&a[x-1][y]!='+'&&a[x-1][y]!='@'){
            if(a[x-1][y]=='#'){
                if(a[x-2][y]=='+'){
                    a[x-2][y]='@';
                    a[x][y]=' ';
                    a[--x][y]='o';
                    cnt--;
                }
                else if(a[x-2][y]==' '){
                    a[x][y]=' ';
                    a[--x][y]='o';
                    a[x-1][y]='#';
                }
            }
            else{
                a[x][y]=' ';
                a[--x][y]='o';
            }
        }
        else if(c=='d'&&a[x][y+1]!='*'&&a[x][y+1]!='+'&&a[x][y+1]!='@'){
            if(a[x][y+1]=='#'){
                if(a[x][y+2]=='+'){
                    a[x][y+2]='@';
                    a[x][y]=' ';
                    a[x][++y]='o';
                    cnt--;
                }
                else if(a[x][y+2]==' '){
                    a[x][y]=' ';
                    a[x][++y]='o';
                    a[x][y+1]='#';
                }
            }
            else{
                a[x][y]=' ';
                a[x][++y]='o';
            }
        }
        display();
        if(cnt<=0){
            cout<<"You Win!";
            system("pause"); 
            break;
        }
    }
    string s;
    cin>>s;
    system("cls");
    return 0;
}

你赢了吗