//在Po学校听了Mark老师的一节公开课后,自己尝试完成的打字母游戏
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <windows.h>
void entry_space(int num)
{
for (int i = 0; i < num;i++)
{
printf("%c", ' ');
}
}
void entry_line(int num)
{
for (int i = 0; i < num; i++)
{
printf("\r\n");
}
}
void game_interface(int levels, int score)
{
system("cls");
entry_space(10);
printf("levels:%d级", levels);
entry_space(10);
printf("score:%d分\r\n", score);
entry_space(10);
printf("数字1:暂停");
entry_space(10);
printf("数字0:退出\r\n");
for (int i = 0; i < 50; i++)
{
putchar('-');
}
}
void judge_condition(int* levels, int score)//处理一些条件
{
if (score<-50)
{
entry_line(15);
entry_space(40);
printf("Game Over\n");
exit(0);//退出程序
}
if (score>(*levels)*100)
{
(*levels)++;//升级
}
}
void play_game(int* levels,int* score)//开始游戏
{
time_t ts;
srand(time(&ts));
char outputch='a'+ rand() % 26;//为了改变字符
int spaceNum = rand() % 50;//控制字符的横坐标位置
int lineNum = 0;//用于保存换了几行,超过行数代表失败
while (true)
{
lineNum++;
entry_line(1);
entry_space(spaceNum);
putchar(outputch);//输出字符
Sleep(1000/(*levels));
printf("\b ");//退一个,将当前位置输出空格
if (kbhit())//获取键盘输入,保存在输入流
{
char inputch = getch();//获取输入流的一个数据
if (inputch==outputch || inputch==(outputch-32))
{
*score += 10;//分数加十
break;
}
}
if (lineNum>25)
{
*score -= 10;//分数减十
break;
}
}
}
void start_game()
{
int levels = 1;//代表难度
int score = 0;//代表分数
while (true)
{
game_interface(levels,score);
play_game(&levels,&score);
judge_condition(&levels, score);
}
}
int main()
{
entry_line(15);
entry_space(40);
//system("pause");
printf("Please input anykey to start\r\n");
getch();
start_game();
return 0;
}
Po学校Mark老师教的打字母游戏
最新推荐文章于 2017-12-14 12:34:15 发布