c语言游戏源码_C语言打字游戏源码

到大街上,还是会羡慕那些情侣,但是依然相信舔狗一无所有,渣男满载而归。。。

----  网易云热评

#include #include #include #include #define MAX 51 //第一测试字母的最大长度void help(){       printf("\n************************");       printf("\n* 请快速输入上面的字母 *");       printf("\n************************");}void start(char *str){       srand((unsigned int)time(NULL));//产生一个随机种子       int i=0;       for (i=0;i-1;i++)       {              *(str + i) = rand() % 26 + 'a';  //随机获取一个0-25的数字并于‘a’相加,从而获取一个任意字符       }       *(str + MAX - 1) = '\0';//给字符串最后加上结束符}void on_game(char *str){       char ch;       int i = 0;       int count = 0;       time_t t_start, t_end; //开始时间和结束时间       printf("\n%s\n", str);       count = 0;       for (i=0;i-1;i++)       {              ch = getch();              if (i==0)              {                     t_start = time(NULL);              }              if (ch==*(str+i))              {                     printf("%c",ch);//输入正确,打印出字符                     count++;              }              else              {                     printf("*");//输入错误,打印*              }       }       t_end = time(NULL);//取结束时间       printf("\n用时%d 秒\n",t_end-t_start);       printf("正确率%lf %%\n", (count * 1.0 / (MAX - 1)) * 100);       printf("按下q键退出");}int main() {       char str[MAX] = { 0 };       while (1)       {              help();//弹出开始内容              start(str);//生成随机字母                     on_game(str);//开始游戏              char ch = getch();              if (ch == 'q') //按下esc键退出              {                     break;              }              system("cls");                     }       system("pause");       return 0;}

1、getch(), 用户按下某个字符时,函数自动读取,无需按回车

2、gechar(),用户按下某个字符时,函数自动读取,需要按回车

3、%%,两个%在格式化字符串中输出一个

4、rand(),获取一个随机数,最大值为32767

5、 count * 1.0,将count变成小数

6、system("cls"),清屏操作

运行结果:

b7ecfdb661a8660ad99b1572dfa78e42.png

欢迎关注公众号:顺便编点程

473acb1d994d930f59dce6486b28f9ac.png

9ca975baaf475bf4425806835556e4fe.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值