问题标题: 小恐龙C++版1.0.1

3
0
已解决
马博闻
马博闻
新手守护
新手守护

不知道大家还记不记得暑假时连载的《小恐龙》系列

我最近在C++上复制了他的部分功能

代码如下:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <unistd.h>
#include <windows.h>
#define k(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
struct map{
    string s;
    int f;
}map[13][25]; //8*20
struct st{
    int x,y;//map[x][y]
    string s;
    int f;
    bool h;
}pl,zh[100000],jn[100000];
int random(int min, int max) {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    srand((time_t)ts.tv_nsec);
    return min + rand() % (max - min + 1);
}
void setColor(int color) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, color);
}
void csh(){
    for(int i=1;i<=8;i++){
        for(int j=1;j<=20;j++){
            map[i][j].s="  ";
        }
    }
    pl.x=8;
    pl.y=3;
    map[8][3].s="pl";
    return;
}
void out(){
    for(int i=1;i<=8;i++){
        for(int j=1;j<=20;j++){
            if(map[i][j].s=="pl")setColor(10);
            if(map[i][j].s==" X")setColor(4);
            if(map[i][j].s==" H")setColor(6);
            if(map[i][j].s=="水")setColor(265);
            if(map[i][j].s=="血")setColor(270);
            if(map[i][j].s==" |")setColor(11);
            cout<<map[i][j].s;
            setColor(0);
        }
        cout<<endl;
    }
    setColor(136);
    for(int i=1;i<=20;i++){
        cout<<"==";
    }
    setColor(0);
    return;
}
void gotoxy(int x, int y) {
    COORD pos = {x, y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}
void game(){
    int ju=0,cnt=0,cnt1=0,blood=5,jn_cnt=0;
    int fl1=0,fl2=0,fl3=1,fl4=0;
    while(true){
        if(cnt>=500){
            fl2=1;
            break;
        }
        if(blood==0){
            fl2=2;
            break;
        }
        if(fl3!=0){
            fl3++;
            if(fl3==24)fl3=0;
            for(int i=1;i<=8;i++){
                if(map[i][fl3].s!="pl"){
                    map[i][fl3].s=" |";
                    for(int j=1;j<=cnt1;j++){
                        if((i==8||i==7)&&fl3==zh[j].y){
                            zh[j].h=0;
                        }
                    }
                }
                if(map[i][fl3-1].s!="pl"){
                    map[i][fl3-1].s="  ";
                }
            }
        }
        cnt++;
        Sleep(30);
        gotoxy(0,0);
        setColor(7);
        cout<<"time:"<<cnt<<endl;
        cout<<"blood:"<<blood<<endl;
        out();
        map[pl.x][pl.y].s="  ";
        if(k('W')&&ju==0&&cnt>fl1+5){//跳! 
            ju=1;
            fl1=cnt;
        }
        if(ju==1){
            pl.x-=2;
            ju++;
        }else if(ju==3){
            pl.x--;
            ju++;
        }else if(ju==5){
            pl.x--;
            ju++;
        }else if(ju==10){
            pl.x++;
            ju++;
        }else if(ju==12){
            pl.x++;
            ju++;
        }else if(ju==14){
            pl.x+=2;
            ju=0;
        }
        if(ju!=0){
            ju++;
        }
        map[pl.x][pl.y].s="pl";
        int r=0;
        if(cnt>fl4){
            r=random(1,20);
            fl4=cnt+2;
        }
        else r=-1;
        if(r==1){//小仙人掌 
            cnt1++;
            zh[cnt1].f=1;
            zh[cnt1].h=1;
            zh[cnt1].y=20;
            fl2=cnt;
        }else if(r==3){
            cnt1++;
            zh[cnt1].f=2;
            zh[cnt1].h=1;
            zh[cnt1].y=20;
            fl2=cnt;
        }
        int r2=0; 
        if(cnt%3==1)r2=random(1,40);
        else         r2=-1;
        if(r2==1&&jn[jn_cnt].y<16){
            jn_cnt++;
            jn[jn_cnt].f=1;//水 
            jn[jn_cnt].h=1;
            jn[jn_cnt].y=20; 
            jn[jn_cnt].x=random(1,8);
        }else if(r2==2&&jn[jn_cnt].y<16){
            jn_cnt++;
            jn[jn_cnt].f=2;//金 
            jn[jn_cnt].h=1;
            jn[jn_cnt].y=20; 
            jn[jn_cnt].x=random(1,8);
        }
        for(int i=1;i<=jn_cnt;i++){
            if(jn[i].h==0)continue;
            map[jn[i].x][jn[i].y].s="  ";
            jn[i].y--;
            if(jn[i].x==1)jn[i].x+=random(0,1);
            else if(jn[i].x==8)jn[i].x-=random(0,1);
            else jn[i].x+=random(0,2)-1;
            if(jn[i].f==1)map[jn[i].x][jn[i].y].s="水";
            else if(jn[i].f==2)map[jn[i].x][jn[i].y].s="血";
            if(jn[i].x==pl.x&&jn[i].y==pl.y){
                jn[i].h=0;
                if(jn[i].f==2)blood+=3;
                else          fl3=1;
            }
            if(jn[i].y==0){
                jn[i].h=0;
            }
        }
        for(int i=1;i<=cnt1;i++){
            if(zh[i].h==0)continue;
            map[8][zh[i].y].s="  ";
            map[7][zh[i].y].s="  ";
            if(cnt%2==1)zh[i].y--;
            if(zh[i].f==1){
                map[8][zh[i].y].s=" X";
                if(pl.x==8&&pl.y==zh[i].y){
                    blood--;
                }
            }else if(zh[i].f==2){
                map[8][zh[i].y].s=" H";
                map[7][zh[i].y].s=" H";
                if((pl.x==8||pl.x==7)&&pl.y==zh[i].y){
                    blood--;
                }
            }
            if(zh[i].y==1){
                zh[i].h=0;
                map[8][zh[i].y].s="  ";
                map[7][zh[i].y].s="  ";
                zh[i].y=0;
            }
        }
    }
    if(fl2==1){
        setColor(110);
        cout<<endl<<"You Win!!!";
        setColor(0);
        return;
    }else{
        setColor(4);
        cout<<endl<<"You Lose...";
        setColor(0);
        return;
    }
}
int main(){
    csh();
    game();
    return 0;
}

觉得好玩的点个赞


0
0
0
我要回答