#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#define X 21
#define Y 21
using namespace std;
bool Map[Y][X];
int ballx,bally;
int zx[7],zy[7],high[7];
int hy[7],l=0,flag=false,ig=X-1;
char c;
long long df=0;
void csh();
void gotoxy(int x,int y);
void gamestart();
void drawmap();
void cball();
void drawball();
void drawz();
void drawh();
void fh();
void drawdf();
void _cin();
bool Gameover();
int main(){
while(true){
df=0,l=0,flag=false,ig=X-1;
csh();
gamestart();
drawmap();
srand(time(NULL));
while(true){
l++;df++;
if(l==75){
if(df>=150)
fh();
flag=true;
l=0;
}
if(flag){
ig--;
if(ig==-1){
flag=false;
ig=X-1;
}
}
_cin();
if(Gameover()){
gotoxy(Y+1,0);
printf("Game over!\n");
system("pause");
break;
}
if(flag)
drawh();
drawz();
drawdf();
drawball();
for(int i=0;i<7;i++)
high[i]++;
for(int i=0;i<7;i++){
if(high[i]-i*2>Y+1){
high[i]=0;
zx[i]=ballx+rand()%3-1;
if(zx[i]==0) zx[i]++;
if(zx[i]==X-1) zx[i]--;
}
}
if(Gameover()){
gotoxy(Y+1,0);
printf("Game over!\n");
system("pause");
break;
}
_sleep(75);
}
}
return 0;
}
void gotoxy(int i,int j){
COORD position={j,i};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
}
void csh(){
system("color 1B");
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(int i=0;i<Y;i++)
for(int j=0;j<X;j++)
Map[i][j]=false;
for(int i=0;i<Y;i++)
Map[i][0]=Map[i][X-1]=true;
for(int j=0;j<X;j++)
Map[0][j]=Map[Y-1][j]=true;
ballx=X/2;
bally=Y/4*3+1;
for(int i=0;i<7;i++)
zy[i]=-(i*2+2);
for(int i=0;i<7;i++)
zx[i]=i*3+1;
for(int i=0;i<7;i++)
hy[i]=i*3+1;
for(int i=0;i<7;i++)
high[i]=0;
return;
}
void gamestart(){
system("cls");
printf("按任意键开始游戏……\n");
c=getch();
system("cls");
printf("游戏规则:\n");
printf("w,a,s,d键控制气球移动\n");
printf("如果碰到箭头,则结束游戏\n");
printf("按任意键继续……\n");
c=getch();
system("cls");
return;
}
void drawmap(){
printf("得分:%d\n",df);
for(int i=0;i<Y;i++){
for(int j=0;j<X;j++){
if(Map[i][j]) printf("#");
else printf(" ");
}
printf("\n");
}
return;
}
void cball(){
gotoxy(bally+1,ballx);
printf(" ");
return;
}
void drawball(){
gotoxy(bally+1,ballx);
printf("O");
return;
}
void drawz(){
for(int i=0;i<7;i++){
if(zy[i]+high[i]>=2){
gotoxy(zy[i]+high[i],zx[i]);
printf(" ");
}
}
for(int i=0;i<7;i++){
if(zy[i]+high[i]>=1&&zy[i]+high[i]<Y-1){
gotoxy(zy[i]+high[i]+1,zx[i]);
printf("v");
}
}
return;
}
void drawh(){
for(int i=0;i<7;i++){
if(ig+1<X-1){
gotoxy(hy[i]+1,ig+1);
printf(" ");
}
}
for(int i=0;i<7;i++){
if(ig>0){
gotoxy(hy[i]+1,ig);
printf("<");
}
}
}
void fh(){
hy[0]=rand()%2+1;
for(int i=1;i<3;i++)
hy[i]=rand()%3+i*3;
hy[3]=bally-1;
hy[4]=bally;
hy[5]=bally+1;
if(hy[3]==0) hy[3]++;
if(hy[5]==Y-1) hy[5]--;
for(int i=6;i<7;i++)
hy[i]=rand()%3+i*3;
hy[6]=rand()%2+Y-3;
}
void drawdf(){
gotoxy(0,5);
printf("%d\n",df);
return;
}
void _cin(){
if(kbhit()!=0){
while(kbhit()!=0)
c=getch();
cball();
switch(c){
case 'a':case 'A':{
if(ballx>1)ballx--;
break;
}
case 'd':case 'D':{
if(ballx<X-2)ballx++;
break;
}
case 'w':case 'W':{
if(bally>1)bally--;
break;
}
case 's':case 'S':{
if(bally<Y-2)bally++;
break;
}
default:break;
}
drawball();
}
return;
}
bool Gameover(){
for(int i=0;i<7;i++){
if(zy[i]+high[i]==bally&&zx[i]==ballx)
return true;
if(hy[i]==bally&&ig==ballx)
return true;
}
return false;
}//qybcjmy
04-12
593
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-04
02-12