#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
#define X 35
#define Y 12
#define A 11
#define RED 4
#define BLUE 1
#define GREAN 2
#define WHITE 7
#define YELLOW 6
#define BLUE_GREAN 3
#define WAIT_TIME 150
bool Map[Y][X];
int birdx,birdy,s;
int z[4][3],zx[4];
char c;
int df;
int colour[4]={RED,BLUE,YELLOW,GREAN};
void color(int a);
void gotoxy(int x,int y);
void csh();
void drawmap();
void drawbird();
void cleanbird();
void _cin();
void change();
void drawz();
void cleanz();
void move();
bool lose();
void drawsd();
int main(){
srand(time(NULL));
while(true){
csh();
drawmap();
while(true){
cleanz();
move();
change();
drawz();
cleanbird();
if(birdy<Y-2)
birdy++;
_cin();
drawbird();
if(lose())
s--;
df++;
drawsd();
if(s==0){
system("cls");
printf("游戏结束!\n");
printf("最终得分:%d\n",df);
c=getch();
system("pause");
break;
}
_sleep(WAIT_TIME);
}
}
return 0;
}
void color(int a){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
return;
}
void gotoxy(int i,int j){
COORD position={j,i};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
return;
}
void csh(){
system("cls");
MessageBox(NULL,"w键向上,s键向下,a键置顶,s键置底,p键暂停","游戏规则:",MB_OK);
gotoxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(int y=1;y<Y-1;y++)
for(int x=1;x<X-1;x++)
Map[y][x]=false;
for(int y=0;y<Y;y++)
Map[y][0]=Map[y][X-1]=true;
for(int x=0;x<X;x++)
Map[0][x]=Map[Y-1][x]=true;
birdx=7;
birdy=Y/2;
zx[0]=X;
for(int i=1;i<4;i++)
zx[i]=zx[i-1]+A;
int _rand;
for(int i=0;i<4;i++){
_rand=rand()%(Y-4)+1;
z[i][0]=_rand;
z[i][1]=_rand+1;
z[i][2]=_rand+2;
}
s=3;
df=0;
return;
}
void drawmap(){
color(WHITE);
for(int y=0;y<Y;y++){
for(int x=0;x<X;x++){
if(Map[y][x]) printf("■");
else printf(" ");
}
printf("\n");
}
return;
}
void drawbird(){
color(BLUE_GREAN);
gotoxy(birdy,birdx*2);
printf("★");
return;
}
void cleanbird(){
gotoxy(birdy,birdx*2);
printf(" ");
return;
}
void _cin(){
if(kbhit()!=0){
while(kbhit()!=0)
c=getch();
switch(c){
case 'w':case 'W':{
for(int i=0;i<2;i++)
if(birdy>1)
birdy--;
break;
}
case 's':case 'S':{
if(birdy<Y-2)
birdy++;
break;
}
case 'a':case 'A':{
birdy=1;
break;
}
case 'd':case 'D':{
birdy=Y-2;
break;
}
case 'm':case 'M':{
birdy=Y/2;
break;
}
case 'g':case 'G':{
if(birdy!=Y-2)
birdy--;
break;
}
case 'p':case 'P':{
drawbird();
c=getch();
break;
}
}
}
return;
}
void change(){
int _rand;
if(zx[0]==0){
zx[0]=zx[3]+A;
_rand=rand()%(Y-4)+1;
z[0][0]=_rand;
z[0][1]=_rand+1;
z[0][2]=_rand+2;
}
for(int i=1;i<4;i++)
if(zx[i]==0){
zx[i]=zx[i-1]+A;
_rand=rand()%(Y-4)+1;
z[i][0]=_rand;
z[i][1]=_rand+1;
z[i][2]=_rand+2;
}
return;
}
void drawz(){
for(int i=0;i<4;i++){
color(colour[i]);
for(int y=1;y<Y-1;y++){
if((y!=z[i][0])&&(y!=z[i][1])&&(y!=z[i][2])&&(zx[i]<X-1)&&(zx[i]>0)){
gotoxy(y,zx[i]*2);
printf("◆");
}
}
}
return;
}
void cleanz(){
for(int i=0;i<4;i++){
for(int y=1;y<Y-1;y++){
if((y!=z[i][0])&&(y!=z[i][1])&&(y!=z[i][2])&&(zx[i]<X-1)&&(zx[i]>0)){
gotoxy(y,zx[i]*2);
printf(" ");
}
}
}
return;
}
void move(){
for(int i=0;i<4;i++)
zx[i]--;
return;
}
bool lose(){
for(int i=0;i<4;i++)
if((birdy!=z[i][0])&&(birdy!=z[i][1])&&(birdy!=z[i][2])&&(zx[i]==birdx))
return true;
return false;
}
void drawsd(){
gotoxy(Y,0);
printf("得分:%d 生命:%d ",df,s);
return;
}//qybcjmy