#include<bits/stdc++.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include <time.h>
#include <dos.h>
#define Speed 4
#define Bar 8 //障碍数
using namespace std;
int brid_x,brid_y;
int high,width;
int speed;
int num;
int iskill;
void gotoxy(int x, int y) //光标移动到x,y位置
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos); //两个参数分别是指定哪个窗体,具体位置
}
void HideCursor() // 用于隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//函数和结构体都在windows.h中定义。
}
struct bars
{
int y;
int top_x;
int width_x;
}bar[Bar];
void startup()
{
srand(time(NULL));
HideCursor();
high=28;
width=100;
brid_x=high/2;
brid_y=10;
speed=0;
num=0;
iskill=0;
for(int i=0;i<Bar;i++)
{
bar[i].y=i*(width/Bar)+20;
bar[i].top_x=rand()%high*3/5;
bar[i].width_x=rand()%5+4;
}
}
void show()
{
gotoxy(0,0);
printf("目前分数:%d\n",num);
for(int i=1;i<high;i++)
{
for(int j=0;j<width;j++)
{
if(i==brid_x&&
j==brid_y) printf("@");
for(int z=0;z<Bar;z++)
{
if(bar[z].y==j&&
(bar[z].top_x>=i
||bar[z].width_x
+bar[z].top_x<=i))
{
cout<<"*";
j++;
}
}
if(i!=brid_x||
j!=brid_y) printf(" ");
}
printf("\n");
}
for(int j=0;j<width+1;j++)
cout<<"-";
cout<<endl;
if(iskill==1)
{
cout<<"呜呜,失败了\n";
system("pause");
exit(0);
}
// printf("*");
}
void updateWithoutInput()
{
if(speed<Speed-num/5) speed++;
if(speed>=Speed-num/5)
{
brid_x++;
for(int i=0;i<Bar;i++)
{
bar[i].y--;
}
speed=0;
}
if(brid_x<=1) brid_x=1;
if(brid_x>=high)
{
brid_x=high-1;
iskill=1;
}
for(int i=0;i<Bar;i++)
{
if(bar[i].y==brid_y&&
(bar[i].top_x>=brid_x
||bar[i].width_x
+bar[i].top_x<=brid_x))
{
iskill=1;
num--;
}
}
for(int i=0;i<Bar;i++)
{
if(bar[i].y<brid_y)
{
num++;
bar[i].top_x=rand()%high*3/5;
bar[i].width_x=rand()%5+4;
bar[i].y=width;
}
}
}
void updateWithInput()
{
char input;
if(kbhit())
{
input=getch();
if(input==' ')
{
brid_x-=2;
}
if(input==27) system("pause");
}
}
int main()
{
startup(); //数据初始化
while(1) //游戏循环执行
{
show(); //游戏画面
updateWithoutInput(); //与输入无关更新
updateWithInput(); //与输入有关更新
}
return 0;
}