#include
#include//使用true时应包含
#include//使用Sleep()
void EntrySpace(int num)//打印空格
{
int i;
for (i = 0; i < num; i++)
{
printf(" ");
}
}
void EntryEnter(int num)//打印回车
{
int i;
for (i = 0; i < num; i++)
{
printf("\n");
}
}
int AlphbetFall(int levels,int lines,int score,int col,int num)//字母下落及按键检测 返回分数
{ //等级 行 分数 列 字母偏移量
while (true)
{
EntryEnter(1);
lines++;
if (lines > 12)
{
lines = 0;
score -= 10;
if (score == -50)//成绩为-50时,游戏结束
{
system("cls");
EntryEnter(10);
EntrySpace(30);
printf("You lose!");
exit(0);
}
return score;
}
EntrySpace(col);
printf("%c", 'A' + num);
Sleep(300 - levels * 50);//延时函数,等级越高,速度越快
printf("\b \n");
if (kbhit())//kbhit()是一个C和C++函数,用于非阻塞地响应键盘输入事件。其中文可译为“键盘敲击”(keyboard hit)
{//功能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0。
char c = getch();
if (c == 'A' + num || c == 'a' + num)//检测按键值是否相同,相同成绩+10
{//不区分大小写
score += 10;
return score;
}
else if (c == '0')//按下0,游戏结束
{
system("cls");
EntryEnter(10);
EntrySpace(30);
printf("Game Over!");
exit(0);
}
else if (c == '1')//按下1,游戏暂停
{
EntryEnter(5);
EntrySpace(30);
//printf("Game is pause!");
printf("Pause");//暂停标记
while (1)
{
if (kbhit())
{
char pause = getch();//读取按键值
if (pause == '1')
{
printf("\b\b\b\b\b ");//消除暂停标记
break;
}
}
}
}
}
}
}
void GameRegullar()//游戏主界面
{
int levels = 0, score = 0, lines = 0, col = 0, num = 0;//等级、分数、行、列、字母偏移量
int flag = 1;//控制有游戏等级
while (true)
{
system("cls");
EntrySpace(10);
printf("levels:%-3d ", levels);//等级越高,下落越快
EntrySpace(10);
printf("score:%d \n", score);//成绩为-50游戏结束
EntrySpace(10);
printf("1---Pause");//暂停
EntrySpace(12);
printf("0---Exit\n");//退出
for (int i = 0; i < 60; i++)
printf("-");
num = rand() % 26;//下落的字母偏移量
col = rand() % 60;//下落字母的列位置
score = AlphbetFall(levels,lines, score, col, num);
if (score > 50 * flag)//成绩每提高100,等级上升1
{
levels++;
flag++;
}
else
flag--;
if (score < 50 * flag)//成绩每减少100,等级降低1
{
levels--;
flag--;
}
else
flag++;
}
}
int main()
{
EntryEnter(10);
EntrySpace(30);
printf("press anykey to start");
getchar();//任意键开始游戏
system("cls");
GameRegullar();
return 0;
}