C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++三体星战游戏

C++三体星战小游戏源代码

作者:强国豪,林中霖

这篇文章主要给大家介绍了关于C++三体星战小游戏的相关资料,文中给出了详细完整的代码示例,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下

物理小游戏,懒得 写注释。

游戏代码

#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int toint(double a){return ((int)(a*10+5))/10;}
int rand(int a){return rand()%a;}
void SlowDisplay(int x,char *p){while(1){if(*p!=0) printf("%c",*p++);else break;Sleep(x);}}
void Setpos(double x,double y){COORD pos;pos.X=toint(y*2),pos.Y=toint(x);SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}
void Color(int a){
    if(a==-1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
    if(a==-2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
    if(a==-3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
    if(a==0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    if(a==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED);
    if(a==2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED);
    if(a==3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN);
    if(a==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE);
    if(a==5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED|BACKGROUND_BLUE);
    if(a==6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE);
}   
struct node{int what;double x,y,vx,vy,r,m;bool life;int gun,master=-1;}Sun[1000001];
int b,T,m[41][41],n[41][41],m2[41][41],n2[41][41],dif[11],Speed=10,Ba,Hotguntime,Hotguntimemax=10,Blood,Score;
double Speedmax,Speedless,Speedmore=1,Balljump=-0.7,Fire=0.08;
void Push(int a,int b){
    if(Sun[a].master==b||Sun[b].master==a) return;
    if(Sun[a].life==0||Sun[b].life==0) return;
    if(Sun[a].what==3||Sun[b].what==3) return;
    if(Sun[a].what==4||Sun[b].what==4) return;
    double Ax=Sun[a].x-Sun[b].x,Ay=Sun[a].y-Sun[b].y,Dis=sqrt(Ax*Ax+Ay*Ay)*1.0,fDis=sqrt((Sun[a].vx-Sun[b].vx)*(Sun[a].vx-Sun[b].vx)+(Sun[a].vy-Sun[b].vy)*(Sun[a].vy-Sun[b].vy));
    if(Dis==0) return;if(abs(Ay)<=0.0001) Ay=0.0001;
    if(Sun[a].what==2||Sun[b].what==2) int c;
    else if(Dis<=Sun[a].r+Sun[b].r+fDis){
        double Vx=(Sun[a].vx+Sun[b].vx)/2.0,Vy=(Sun[a].vy+Sun[b].vy)/2.0,aX=(Sun[a].x+Sun[b].x+Sun[a].vx+Sun[b].vx)/2.0,aY=(Sun[a].y+Sun[b].y+Sun[a].vy+Sun[b].vy)/2.0;
        Sun[a].vx=Sun[b].vx=Vx,Sun[a].vy=Sun[b].vy=Vy;Sun[a].x=aX-(Ax/Dis)/2.0,Sun[b].x=aX+(Ax/Dis)/2.0;Sun[a].y=aY-(Ay/Dis)/2.0,Sun[b].y=aY+(Ay/Dis)/2.0;
        return;
    }
    double ac=Sun[a].m*Sun[b].m/(Dis*Dis)*1.0,afx=0,afy=0,d=abs(Ax/Ay*1.0);
    afy=sqrt(ac/(1+d*d))*1.0,afx=sqrt(ac/(1+d*d))*d*1.0;
    if(Ax>0) afx*=-1;if(Ay>0) afy*=-1;
#define A Sun[a].vx+=afx/Sun[a].m*Speedless,Sun[a].vx=max(-Speedmax,min(Speedmax,Sun[a].vx)),Sun[a].vy+=afy/Sun[a].m*Speedless,Sun[a].vy=max(-Speedmax,min(Speedmax,Sun[a].vy))
#define B Sun[b].vx-=afx/Sun[b].m*Speedless,Sun[b].vx=max(-Speedmax,min(Speedmax,Sun[b].vx)),Sun[b].vy-=afy/Sun[b].m*Speedless,Sun[b].vy=max(-Speedmax,min(Speedmax,Sun[b].vy))
    Speedmax=0.1,Speedless=0.01; 
    if(Sun[a].what==2&&Sun[b].what==2) Speedmax=3.0;
    if(Sun[a].what==0) {Speedless=0.008;Speedmax=1.0; A;}
    else if(Sun[a].what==1) {A; if(Sun[b].what==1) B;}
    else if(Sun[a].what==2) {Speedless=0.8;B;if(Sun[b].what==2) A;}
    else {A;B;}
}
int Painting[41][41]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,2,9,9,9,9,9,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,3,9,9,9,9,9,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,4,9,9,9,9,9,9,9,9,9,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},};
void Paint(int a);
void Move(int a){
    Blood=10000000;
    memset(m,0,sizeof(m));
    memset(m2,0,sizeof(m2));
    #define ix toint(Sun[i].x/1.0)
    #define iy toint(Sun[i].y/1.0)
    #define jx toint(Sun[j].x/1.0)
    #define jy toint(Sun[j].y/1.0)
    if(Sun[0].life==1){
    Sun[0].x+=Sun[0].vx/Speedmore;Sun[0].y+=Sun[0].vy/Speedmore;
    if(Sun[0].x>40) Sun[0].x=40,Sun[0].vx=0;if(Sun[0].x<0) Sun[0].x=0,Sun[0].vx=0;if(Sun[0].y>40) Sun[0].y=40,Sun[0].vy=0;if(Sun[0].y<0) Sun[0].y=0,Sun[0].vy=0;}
    for(int i=0;i<=b;i++) for(int j=i+1;j<=b;j++) Push(i,j);
    for(int i=0;i<=b;i++) for(int j=0;j<=b;j++)
    {
        if(i==j||Sun[i].life==0||Sun[j].life==0) continue;
    if(Sun[i].what==4&&(Sun[j].what>=100&&Sun[j].what<200)) {if(abs(ix-jx)<1&&abs(iy-jy)<1) Ba--,Sun[i].life=Sun[j].life=0,Score+=10;}
        if(Sun[i].what==203&&Sun[i].master!=j&&Sun[j].what<200) if(abs(ix-jx)<2&&abs(iy-jy)<2) Sun[j].vx-=Sun[i].vx/10.0,Sun[j].vy-=Sun[i].vy/10.0,Sun[i].life=0;
        if(Sun[i].what==204&&Sun[i].master!=j&&Sun[j].what<200) if(abs(ix-jx)<2&&abs(iy-jy)<2) Sun[j].vx+=Sun[i].vx/10.0,Sun[j].vy+=Sun[i].vy/10.0,Sun[i].life=0;
    }
    int i=0;
    if(m[ix][iy]>0&&m[ix][iy]<100)
    {
        if(m[ix][iy]<=1) Blood-=1;
        else if(m[ix][iy]<=4) Blood-=2;
        else if(m[ix][iy]<=10) Blood-=3;
        else if(m[ix][iy]>10&&m[ix][iy]<100) Blood-=4;
    }
    for(int i=1;i<=b;i++){
        if(Sun[i].life==0) continue;
        Sun[i].x+=Sun[i].vx/Speedmore;Sun[i].y+=Sun[i].vy/Speedmore;
        Balljump=-0.7;
        if(Sun[i].what==3) Balljump=-1;
        if(Sun[i].what==4) Balljump=-1;
        if(Sun[i].x>40) Sun[i].x=40,Sun[i].vx*=Balljump;
        if(Sun[i].x<0) Sun[i].x=0,Sun[i].vx*=Balljump;if(Sun[i].y>40) Sun[i].y=40,Sun[i].vy*=Balljump;if(Sun[i].y<0) Sun[i].y=0,Sun[i].vy*=Balljump;
        if(Sun[i].what==1) {m[ix][iy]+=2;int R;R=rand(3);if(R==0&&ix<40) m[ix+1][iy]++;R=rand(3);if(R==0&&ix>0) m[ix-1][iy]++;R=rand(3);if(R==0&&iy<40) m[ix][iy+1]++;R=rand(3);if(R==0&&iy>0) m[ix][iy-1]++;}
        if(Sun[i].what==2) {m[ix][iy]+=2;if(ix<40) m[ix+1][iy]+=10;if(ix>0) m[ix-1][iy]+=10;if(iy<40) m[ix][iy+1]+=10;if(iy>0) m[ix][iy-1]+=10;if(ix<40&&iy<40) m[ix+1][iy+1]+=4;if(ix<40&&iy>0) m[ix+1][iy-1]+=4;if(ix>0&&iy<40) m[ix-1][iy+1]+=4;if(ix>0&&iy>0) m[ix-1][iy-1]+=4;m[ix][iy]=10086;} 
        if(Sun[i].what==3) {if(abs(ix-Sun[0].x)<1&&abs(iy-Sun[0].y)<1) Ba--,Sun[i].life=0;else m2[ix][iy]=2;}
        if(Sun[i].what==4) {m2[ix][iy]=2;}
        if(Sun[i].what>=100&&Sun[i].what<300)
        {
            if(Sun[i].x==0||Sun[i].y==0||Sun[i].x==40||Sun[i].y==40||(abs(Sun[i].vx)<=0.5&&abs(Sun[i].vy)<=0.5)) Sun[i].life=0;
            if(Sun[i].what==203||Sun[i].what==204) {m[ix][iy]=Sun[i].what;if(ix<40) m[ix+1][iy]=Sun[i].what;if(ix>0) m[ix-1][iy]=Sun[i].what;if(iy<40) m[ix][iy+1]=Sun[i].what;if(iy>0) m[ix][iy-1]=Sun[i].what;}
            else m2[ix][iy]=Sun[i].what;
        } 
    }
    if(Sun[0].life==1) m2[toint(Sun[0].x/1.0)][toint(Sun[0].y/1.0)]=1;
    Paint(a);
}
void Paint(int a){
for(int i=0;i<=40;i++) for(int j=0;j<=40;j++){
    if(a==0&&Painting[i][j]!=0) continue;
    if(m2[i][j]!=n2[i][j]){n2[i][j]=m2[i][j];n[i][j]=0;Setpos(i,j);
    if(m2[i][j]==0) Color(0),cout<<"  ";
    else if(m2[i][j]==1) Color(-1),cout<<"●";
    else if(m2[i][j]==2) Color(-3),cout<<"◎";
    else if(m2[i][j]==101) Color(-1),cout<<"☉";
    else if(m2[i][j]==102) Color(3),cout<<"  ";
    continue;}
    if(m[i][j]!=n[i][j]){n[i][j]=m[i][j];n2[i][j]=0;Setpos(i,j);
    if(m[i][j]>=10086) Color(4),cout<<"●";
    else if(m[i][j]==203) Color(5),cout<<"  ";
    else if(m[i][j]==204) Color(6),cout<<"  ";
    else if(m[i][j]==0) Color(0),cout<<"  ";else if(m[i][j]<=1) Color(1),cout<<"  ";else if(m[i][j]<=4) Color(2),cout<<"  ";else if(m[i][j]<=10) Color(3),cout<<"  ";else if(m[i][j]>10&&m[i][j]<100) Color(4),cout<<"  ";
    else Color(0),cout<<"?!";}Color(0);
    }
}
void RandStart(int a) {for(int i=1;i<=a;i++) {b++;Sun[b].x=rand(41),Sun[b].y=rand(41);Sun[b].vx=(rand(41)-20)/10.0,Sun[b].vy=(rand(41)-20)/10.0,Sun[b].m=1,Sun[b].r=1,Sun[b].life=1,Sun[b].what=1;}}
void CornerStart(int a) {for(int i=1;i<=a;i++) {b++;int k=rand(4);if(k==0||k==2) Sun[b].x=rand(41);else Sun[b].y=rand(41);if(k==0) Sun[b].y=0;if(k==1) Sun[b].x=0;if(k==2) Sun[b].y=40;if(k==3) Sun[b].x=40;Sun[b].vx=(rand(41)-20)/10.0,Sun[b].vy=(rand(41)-20)/10.0,Sun[b].m=1,Sun[b].r=1,Sun[b].life=1,Sun[b].what=1;}}
void SunStart(int x,int y,int r,int vx,int vy) {
b++;Sun[b].x=x,Sun[b].y=y;Sun[b].vx=vx,Sun[b].vy=vy,Sun[b].m=1000,Sun[b].r=1,Sun[b].life=1,Sun[b].what=2;
for(int i=x-r;i<=x+r;i++) for(int j=y-r;j<=y+r;j++){b++;Sun[b].x=i,Sun[b].y=j;Sun[b].vx=(rand(41)-20)/100.0+vx,Sun[b].vy=(rand(41)-20)/100.0+vy,Sun[b].m=1,Sun[b].r=1,Sun[b].life=1,Sun[b].what=1;}
for(float i=(int)(x-r/2)-0.5;i<=(int)(x+r/2)+0.5;i++) for(float j=(int)(y-r/2)-0.5;j<=(int)(y+r/2)+0.5;j++){b++;Sun[b].x=i,Sun[b].y=j;Sun[b].vx=(rand(41)-20)/100.0+vx,Sun[b].vy=(rand(41)-20)/100.0+vy,Sun[b].m=1,Sun[b].r=1,Sun[b].life=1,Sun[b].what=1;}
}
void Shoot(int a,bool q)
{
    double Sunavx=Sun[a].vx,Sunavy=Sun[a].vy;
    if(Sun[a].vx==0&&Sun[a].vy==0) Sunavx=-1;if(Sun[a].vy==0) Sunavy=0.001;
    if(Sun[a].gun>=1&&Sun[a].gun<=4)
    {
        b++;Sun[b].x=Sun[a].x,Sun[b].y=Sun[a].y;double ac;
        if(Sun[a].gun==1) ac=3.0; 
        if(Sun[a].gun>=2&&Sun[a].gun<=4) ac=7.0;
        double d=abs(Sunavx/Sunavy*1.0);Sun[b].vy=sqrt(ac/(1+d*d))*1.0,Sun[b].vx=sqrt(ac/(1+d*d))*d*1.0;
        if(Sunavx>0) Sun[b].vx*=-1;if(Sunavy>0) Sun[b].vy*=-1;if(q==1) Sun[b].vx*=-1,Sun[b].vy*=-1;Sun[b].life=1;
        Sun[b].master=a;
        int bb=b;
        if(Sun[a].gun==1||Sun[a].gun==2) Sun[b].what=100+Sun[a].gun,Sun[b].m=1;
        if(Sun[a].gun==3||Sun[a].gun==4) Sun[b].what=200+Sun[a].gun,Sun[b].m=1;
        if(Sun[a].gun>=2&&Sun[a].gun<=4){
    double kx=Sun[bb].x,ky=Sun[bb].y;
    for(int j=1;j<=7;j++)
    {
    kx-=Sun[bb].vx/3.5,ky-=Sun[bb].vy/3.5;
    if(kx<0||ky<0||kx>40||ky>40) continue;
    b++,Sun[b].x=kx,Sun[b].y=ky,Sun[b].vx=Sun[bb].vx,Sun[b].vy=Sun[bb].vy,Sun[b].what=Sun[bb].what,Sun[b].master=a,Sun[b].life=1;
    }
        }
    }
}
void Start(){
    for(int i=0;i<=40;i++) for(int j=0;j<=40;j++){if(Painting[i][j]==1) Setpos(i,j),Color(-2),cout<<"■";if(Painting[i][j]==2) Setpos(i,j),Color(-2),cout<<"作者:蒟蒻一枚";if(Painting[i][j]==3) Setpos(i,j),Color(-2),cout<<"按 y 开始游戏!";if(Painting[i][j]==4) Setpos(i,j),Color(-2),cout<<"不要搞错成拼音输入法!";if(Painting[i][j]==5) Setpos(i,j),Color(-2),cout<<"Three-Body STAR WARs!";}
    for(int i=1;i<=3;i++) SunStart(rand(31)+5,rand(31)+5,5,(rand(101)-50)/10.0,(rand(101)-50)/10.0);
    T=0;
    while(1){
        T++;
        if(T%50==0) CornerStart(1);
        if(kbhit()){char g=_getch();if(g=='y') break; }
        Move(0);
        Sleep(Speed);
    }
    Color(0);system("cls");
    Setpos(10,10);SlowDisplay(30,"你想要教程吗?(y/n)");
    char g=_getch();if(g!='y') return;Setpos(10,10);SlowDisplay(30,"那我们开始。。。   ");Sleep(500);
    system("cls");
    Setpos(30,10);system("cls");
    memset(Sun,0,sizeof(Sun));memset(m,0,sizeof(m));memset(m2,0,sizeof(m2));
    Sun[0].x=Sun[0].y=20;Sun[0].life=1;Sun[0].m=10;
    T=0;
    int step=0;
    while(1){
        T++;
        if(T==2)
        {
        Setpos(10,10),SlowDisplay(30,"这是你的飞船,你可以用↑↓←→键来操纵它。");Sleep(500);
        Setpos(12,10),SlowDisplay(30,"在宇宙航行没有阻力,也就是说你的惯性会主导一切。");Sleep(500);
        Setpos(14,10),SlowDisplay(30,"当你撞到边界时,你的速度会减为零。");Sleep(1000); 
        Setpos(16,10),SlowDisplay(30,"试试看。");
        }
        if(T==200)
        {
        Setpos(10,10),SlowDisplay(30,"在宇宙航行时有两个小技巧:                ");
        Setpos(12,10),SlowDisplay(30,"1、不要太快。                                   ");
        Setpos(14,10),SlowDisplay(30,"2、有时转向比调头更有用。         ");Sleep(1000);
        Setpos(16,10),SlowDisplay(30,"接下来我们稍稍提高点难度!请到达所有靶位点!");Sleep(500);
        b++;Sun[b].x=rand(40),Sun[b].y=rand(40);Sun[b].vx=(rand(101)-50)/30.0,Sun[b].vy=(rand(101)-50)/30.0,Sun[b].life=1,Sun[b].what=3;Ba++;
        Move(1);Sleep(1000); 
        }
        if(Ba>0) Setpos(1,1),cout<<"剩余靶位点:"<<Ba<<' ';
        if(step>=1) Setpos(5,1),cout<<"Cooling time: "<<Hotguntime<<"    ";
        if(T>200&&Ba==0&&step==0) T=201,step=1,Setpos(1,1),cout<<"           ";
        if(T>300&&Ba==0&&step==1) T=201,step=2,Setpos(1,1),cout<<"           ";
        if(T==202&&step==1)
        {
        Setpos(10,10),SlowDisplay(30,"做的好!看来是时候给你装备初始武器了。");
        Setpos(12,10),SlowDisplay(50,"。。。 星际电磁轨道炮 已装备。");
        Setpos(14,10),SlowDisplay(30,"按下w/s发射子弹!                         ");
        Sun[0].gun=1;Hotguntime=0;
        }
        if(T==300&&step==1)
        {
        Setpos(16,10),SlowDisplay(30,"来吧!请击毁所有靶位点!");Sleep(500);
        b++;Sun[b].x=rand(40),Sun[b].y=rand(40);Sun[b].vx=(rand(101)-50)/30.0,Sun[b].vy=(rand(101)-50)/30.0,Sun[b].life=1,Sun[b].what=4;Ba++;
        Move(1);Sleep(1000); Ba=1;
        }
        if(T==202&&step==2)
        {
        Setpos(10,10),SlowDisplay(30,"太阳什么的你也看到过了。               ");
        Setpos(12,10),SlowDisplay(30,"其旁缠绕着的火会扣你的血。      ");
        Setpos(14,10),SlowDisplay(30,"打靶位点可以积10分,发射子弹-1分,分数达到20通关,简单吧!");Sleep(1000); 
        Setpos(16,10),SlowDisplay(60,"让 我 们  开 始  吧!!!");Sleep(1000); 
        system("cls");return; 
        }
        double v=sqrt(Sun[0].vx*Sun[0].vx+Sun[0].vy*Sun[0].vy);
        if(GetAsyncKeyState(VK_UP)&0x8000) {if(Sun[0].vx>=0) Sun[0].vx-=2*Fire;else if(v<4) Sun[0].vx-=Fire;}
        else if(GetAsyncKeyState(VK_DOWN)&0x8000) {if(Sun[0].vx<=0) Sun[0].vx+=2*Fire;else if(v<4) Sun[0].vx+=Fire;}
        if(GetAsyncKeyState(VK_LEFT)&0x8000) {if(Sun[0].vy>=0) Sun[0].vy-=2*Fire;else if(v<4) Sun[0].vy-=Fire;}
        else if(GetAsyncKeyState(VK_RIGHT)&0x8000) {if(Sun[0].vy<=0) Sun[0].vy+=2*Fire;else if(v<4) Sun[0].vy+=Fire;}
        if(Hotguntime>0) Hotguntime--;
        if(kbhit()){
        char g=_getch();
        if(Hotguntime==0&&g=='s')
        {
            if(abs(Sun[0].vx)>=0.01) Sun[0].vx*=0.7;
            if(abs(Sun[0].vy)>=0.01) Sun[0].vy*=0.7;
            Shoot(0,0);
            Hotguntime=Hotguntimemax;
        }
        if(Hotguntime==0&&g=='w')
        {
            if(abs(Sun[0].vx)<=2) Sun[0].vx*=1.2;
            if(abs(Sun[0].vy)<=2) Sun[0].vy*=1.2;
            Shoot(0,1);
            Hotguntime=Hotguntimemax;
        }
        }
        Move(1);
        Sleep(Speed);
    }
}
int main()
{
    system("mode con cols=82 lines=43");
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
    srand((unsigned)time(NULL));
    Y:
    Start();
    memset(Sun,0,sizeof(Sun));memset(m,0,sizeof(m));memset(m2,0,sizeof(m2));
    YY:system("cls");
    Setpos(10,10),SlowDisplay(30,"选难度吧,几个太阳?(0~10,回车确定)");
    int i=10;while(dif[i]!=1&&i>=0){i--;}
    Setpos(12,10);
    if(i==-1) printf("推荐难度:0个太阳。");
    else if(i<10) printf("加油,推荐难度:%d个太阳。",i+1);
    else if(i==10) printf("你通关了!随便选吧!",i);
    Setpos(15,10);
    int p;cin>>p;
    if(p<0||p>10) {SlowDisplay(150,"输入错误!"),Sleep(1000);goto YY;}
    for(int i=1;i<=p;i++) SunStart(rand(31)+5,rand(31)+5,5,(rand(101)-50)/10.0,(rand(101)-50)/10.0);
    Sun[0].x=Sun[0].y=5;Sun[0].vx=0.1;Sun[0].life=1;Sun[0].m=10;
    T=0;
    Sun[0].gun=1;
    Blood=100;
    Score=0;Ba=0;
    for(int i=1;i<=10;i++) {b++;Sun[b].x=rand(40),Sun[b].y=rand(40);Sun[b].vx=(rand(101)-50)/30.0,Sun[b].vy=(rand(101)-50)/30.0,Sun[b].life=1,Sun[b].what=4;Ba++;}
    while(Blood>0&&Score<100){
        Setpos(1,1),cout<<"Blood: "<<Blood<<"    ";
        Setpos(3,1),cout<<"Score: "<<Score<<"    ";
        Setpos(5,1),cout<<"Cooling time: "<<Hotguntime<<"    ";
        T++;
        if(T==1) Sleep(1000);
        if(T%50==0) CornerStart(1);
        if(T%30==0&&Ba<5) {b++;Sun[b].x=rand(40),Sun[b].y=rand(40);Sun[b].vx=(rand(101)-50)/30.0,Sun[b].vy=(rand(101)-50)/30.0,Sun[b].life=1,Sun[b].what=4;Ba++;}
        if(T%40==0&&Blood<100) Blood++;
        double v=sqrt(Sun[0].vx*Sun[0].vx+Sun[0].vy*Sun[0].vy);
        if(GetAsyncKeyState(VK_UP)&0x8000) {if(Sun[0].vx>=0) Sun[0].vx-=2*Fire;else if(v<4) Sun[0].vx-=Fire;}
        else if(GetAsyncKeyState(VK_DOWN)&0x8000) {if(Sun[0].vx<=0) Sun[0].vx+=2*Fire;else if(v<4) Sun[0].vx+=Fire;}
        if(GetAsyncKeyState(VK_LEFT)&0x8000) {if(Sun[0].vy>=0) Sun[0].vy-=2*Fire;else if(v<4) Sun[0].vy-=Fire;}
        else if(GetAsyncKeyState(VK_RIGHT)&0x8000) {if(Sun[0].vy<=0) Sun[0].vy+=2*Fire;else if(v<4) Sun[0].vy+=Fire;}
        if(Hotguntime>0) Hotguntime--;
        if(kbhit()){
        char g=_getch();
        if(Hotguntime==0&&g=='s')
        {
            if(abs(Sun[0].vx)>=0.01) Sun[0].vx*=0.7;
            if(abs(Sun[0].vy)>=0.01) Sun[0].vy*=0.7;
            Shoot(0,0);
            Score=max(Score-1,0);
            Hotguntime=Hotguntimemax;
        }
        if(Hotguntime==0&&g=='w')
        {
            if(abs(Sun[0].vx)<=2) Sun[0].vx*=1.2;
            if(abs(Sun[0].vy)<=2) Sun[0].vy*=1.2;
            Shoot(0,1);
            Score=max(Score-1,0);
            Hotguntime=Hotguntimemax;
        }
        }Move(1);
        Sleep(Speed);
    }
    system("cls");
    if(Score<20) Setpos(10,10),SlowDisplay(150,"Sorry,你死了。"); 
    else Setpos(10,10),SlowDisplay(10,"赢了!快去挑战下一个难度吧!"),dif[p]=1; 
    Sleep(1000); 
    goto Y;
    return 0;
}

总结

到此这篇关于C++三体星战小游戏的文章就介绍到这了,更多相关C++三体星战游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文