C++小游戏

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;

char s[5005][5005],c,ss[5005][5005];

char Map[11];

int fx[4][2]={{0,-1},{0,1},{-1,0},{1,0}};

int x,y,fxb,n,m,bullet_x[1000001],bullet_y[1000001],bullet,fxbb[1000001];

bool bullets[1000001];

int hp[5005][5005];

inline void out();
inline void scan();
inline void bullet_fly();

inline void out(){
    for(int i=0;i<n;i++)
        cout<<s[i]<<endl;
}

inline void scan(){
    c=getch();
    if(c==27)exit(0);
    if(c==-32){
        c=getch();
        if(c==75)fxb=0;
        if(c==77)fxb=1;
        if(c==72)fxb=2;
        if(c==80)fxb=3;
    }
    else if(c==32)
        bullet_fly();
    else {
        s[x][y]=ss[x][y];
        if(c=='a')
            if(y>0&&ss[x][y-1]=='.')y--;
        if(c=='d')
            if(y<m-1&&ss[x][y+1]=='.')y++;
        if(c=='w')
            if(x>0&&ss[x-1][y]=='.')x--;
        if(c=='s')
            if(x<n-1&&ss[x+1][y]=='.')x++;
        s[x][y]='@';
        system("cls");
        out();
    }
}

inline void bullet_fly(){
    bullet_x[++bullet]=x+fx[fxb][0],bullet_y[bullet]=y+fx[fxb][1],bullets[bullet]=1;
    fxbb[bullet]=fxb;
    int cnt=0;
    do{
        cnt=0;
        for(int bullet1=1;bullet1<=bullet;bullet1++){
            if(!bullets[bullet1])continue;
            cnt++;
            if(s[bullet_x[bullet1]-fx[fxbb[bullet1]][0]][bullet_y[bullet1]-fx[fxbb[bullet1]][1]]!='@')
                s[bullet_x[bullet1]-fx[fxbb[bullet1]][0]][bullet_y[bullet1]-fx[fxbb[bullet1]][1]]=ss[bullet_x[bullet1]-fx[fxbb[bullet1]][0]][bullet_y[bullet1]-fx[fxbb[bullet1]][1]];
            if(bullet_x[bullet1]>=0&&bullet_y[bullet1]>=0&&bullet_x[bullet1]<n&&bullet_y[bullet1]<m&&ss[bullet_x[bullet1]][bullet_y[bullet1]]=='.'){
                s[bullet_x[bullet1]][bullet_y[bullet1]]='*';
                bullet_x[bullet1]+=fx[fxbb[bullet1]][0],bullet_y[bullet1]+=fx[fxbb[bullet1]][1];
            }
            else bullets[bullet1]=0;
            if(ss[bullet_x[bullet1]][bullet_y[bullet1]]=='#'){
                hp[bullet_x[bullet1]][bullet_y[bullet1]]--;
                s[bullet_x[bullet1]][bullet_y[bullet1]]='%';
                system("cls");
                out();
                if(!hp[bullet_x[bullet1]][bullet_y[bullet1]])
                    ss[bullet_x[bullet1]][bullet_y[bullet1]]=s[bullet_x[bullet1]][bullet_y[bullet1]]='.';
                else {
                    s[bullet_x[bullet1]][bullet_y[bullet1]]=ss[bullet_x[bullet1]][bullet_y[bullet1]];
                }
            }
        }
        Sleep(50);
        system("cls");
        out();
        if(kbhit())scan();
    }while(cnt);
}

int main(){
    srand(time(0));
    cout<<"n,m:"<<endl;
    cin>>n>>m;
    Map[0]=Map[1]=Map[2]=Map[4]=Map[5]=Map[6]='.',Map[3]='#';
    system("cls");
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            ss[i][j]=s[i][j]=Map[rand()%7];
            if(ss[i][j]=='#')hp[i][j]=10;
        }
    s[0][0]='@';
    ss[0][0]='.';
    out();
    while(1)scan();
    return 0;
}

 wsad:上下左右

↑↓←→:改变射击方向

[space]:开枪

会更新

猜你喜欢

转载自www.cnblogs.com/wyzwyz/p/11255708.html