C++ 小游戏-球球飞车
功能实现
- 地图随机生成
- 彩色界面打印
- 按键检测
- 存档
- 商店
运行截图
核心代码展示
地图生成:
void rand_map(int a,int pd) {
srand(time(0));
int s;
int rand1,rand2,rand3,rand4,rand5;
if(a<=10)s=100;
else if(a<=50)s=200;
else if(a<=100)s=500;
else if(a<=300)s=800;
else if(a<=500)s=1100;
else s=1500;
for(int m=0; m<=s; m++) {
if(m%5==0&&m!=0) {
if(m==5||m==s) {
if(m==5) {
map2[m][0]=5;
map2[m][1]=5;
map2[m][2]=5;
} else if(m==s) {
map2[m][0]=4;
map2[m][1]=4;
map2[m][2]=4;
}
} else {
rand1=rand()%3;
map2[m][rand1]=pd;
rand2=rand()%3;
while(rand2==rand1)rand2=rand()%3;
rand3=rand()%3+1;
while(rand3==pd)rand3=rand()%3+1;
map2[m][rand2]=rand3;
rand4=rand2;
rand2=rand()%3;
while(rand2==rand4||rand2==rand1)rand2=rand()%3;
rand3=rand()%3+1;
while(rand3==pd)rand3=rand()%3+1;
map2[m][rand2]=rand3;
}
} else {
map2[m][0]=0;
map2[m][1]=0;
map2[m][2]=0;
}
}
}
游戏主循环:
else if(x==' ') {
if(i==1) {
int s;
if(gk<=10) {
dj=1;
s=100;
} else if(gk<=50) {
dj=2;
s=200;
} else if(gk<=100) {
dj=3;
s=500;
} else if(gk<=300) {
dj=4;
s=800;
} else if(gk<=500) {
dj=5;
s=1100;
} else {
dj=6;
s=1500;
}
xy2=1;
sm=maxsm;
jl2=0;
clear2(8,0,29,95,126);
print("aaaaaaaaaaaaaaaa ",0,95,4);
write("关卡",0,106,15,4);
SetColorAndBackground(7, 4);
if(gk>=0&&gk<10)cout<<"0000";
if(gk>=10&&gk<100)cout<<"000";
if(gk>=100&&gk<1000)cout<<"00";
if(gk>=1000&&gk<10000)cout<<"0";
if(gk>=10000&&gk<100000)cout<<"";
cout<<gk;
write("生命:",1,95,15,8);
cout<<sm<<"条";
write("金币:",2,95,15,8);
cout<<jb<<"元";
write("距离:",3,95,15,8);
cout<<(jl*1.0)/1000<<"千米 ";
write("进度:",4,95,15,8);
cout<<jd<<"%";
write("速度:",5,95,15,8);
cout<<dj*2<<"米/秒";
write("等级:",6,95,15,8);
cout<<dj<<"级 (6级满级)";
sl=0;
for(int k=0;; k++) {
if(sl%((10-dj)*900)==0) {
jl++;
jl2++;
jd=(jl2*1.0)/s*100;
sd=2;
SetColorAndBackground(15,8);
gotoxy(1,101);
cout<<sm<<"条";
gotoxy(2,101);
cout<<jb<<"元";
gotoxy(3,101);
cout<<(jl*1.0)/1000<<"千米";
gotoxy(4,101);
cout<<"已完成"<<jl2<<"米 ("<<jd<<"%)";
gotoxy(5,101);
cout<<sd*2<<"米/秒";
gotoxy(6,101);
cout<<dj<<"级 (6级满级)";
if(map2[3+k][xy2]==rand3) {
jb++;
map2[3+k][xy2]=0;
} else if(map2[3+k][xy2]==5)map2[3+k][xy2]=5;
else if(map2[3+k][xy2]==4) {
text("成功,游戏结束");
Sleep(1000);
gk++;
ofstream fw; //写
fw.open("球球飞车.txt"); //打开 读入.in
fw << gk<<" "<<gk2<<" "<<xy1<<" "<<xy2<<" "<<sm<<" "<<jb<<" "<<jl<<" "<<jl2<<" "<<jd<<" "<<sd<<" "<<dj<<" "<<maxsm<<" "<<pf<<" "<<pfs<<endl; //输入内容
fw.close();
clear2(8,0,29,95,126);
break;
} else if(map2[3+k][xy2]==0)map2[3+k][xy2]=0;
else {
sm--;
text("横冲直撞,生命扣一");
if(sm==0) {
text("失败,游戏结束");
Sleep(1000);
clear2(8,0,29,95,126);
break;
}
}
for(int i=0; i<=29; i++) {
for(int j=0; j<=2; j++) {
if(!(i<=27&&i>=25)) {
if(map2[i+k][j]==0&&(map2[i+k-1][j]!=map2[i+k][j]||j==xy2
||i==3))write(" ",29-i,j*2+44,8,15);
if(map2[i+k][j]==1)write(pf,29-i,j*2+44,14,15);
if(map2[i+k][j]==2)write(pf,29-i,j*2+44,12,15);
if(map2[i+k][j]==3)write(pf,29-i,j*2+44,9,15);
if(map2[i+k][j]==4)write("↑",29-i,j*2+44,8,15);
if(map2[i+k][j]==5)write("↑",29-i,j*2+44,8,15);
}
if(j==xy2&&i==3) {
if(rand3==1)write(pf,26,j*2+44,14,15);
if(rand3==2)write(pf,26,j*2+44,12,15);
if(rand3==3)write(pf,26,j*2+44,9,15);
}
}
}
} else k--;
if(kbhit()) {
x=getch();
if(x=='a'&&xy2>0) {
text("左转,加速!");
xy2--;
} else if(x=='d'&&xy2<2) {
text("右转,加速!");
xy2++;
} else if(x==' ') {
if(MessageBox(NULL,
TEXT("确定要退出吗?\n退出将删除当前关卡的进度,无法继续\t\t\t\t\t"),
TEXT("游戏通知"),MB_ICONINFORMATION|MB_OKCANCEL)==1) {
clear2(8,0,29,95,126);
break;
}
}
}
sl++;
}
}
运行环境
Dev-c++上运行通过