这篇文章主要为大家详细介绍了C语言实现——《打字练习系统》,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下!
游戏介绍:
《字母游戏》是一款敏捷打字小游戏,游戏大小为468K。背景设定 《字母游戏》是一款有趣的打字游戏,可以提高你的打字速度。操作指南 根据出现的字母,按键盘A-Z键对应的按键即可。游戏加载完毕点击[开始游戏]即可开始游戏。在限定时间内,尽可能地输入正确的字母,挑战高分!
本项目针对C语言学习者,将我们打字母的字母置换成了C语言关键字,记在一定时间内及时输出C语言关键字就可以得分!我们一起来看看吧!
本项目编译环境:VS2019/VS2013;
插件:图形库插件easyX,涉及图片素材可以自行百度找也可以关注文末领取;
效果图展示
配套讲解教程:「链接」
源代码示例:
define_CRT_SECURE_NO_WARNINGSinclude include include include include //报错,请先安装 include pragmacomment(lib,"winmm.lib")//数据设计设计//窗口属性:constintWIDTH =640;constintHEIGHT =500;//游戏正确率和错误率intright =0;interror =0;//下坠文字的结构体structTARGET{//每一个字符串的x,y坐标intx;inty;char*str;//保存字符串};//用户输入的值structUSRKEY{intx;inty;charstr[20];
}userkey = {320,500-30,""};//在指定位置输出整数voidouttextxy_int(intx,inty,char*format,intnum){charstr[20] ="";//printf;sprintf(str, format, num);
outtextxy(x, y, str);
}//在指定位置输出浮点数voidouttextxy_double(intx,inty,char*format,doublenum){charstr[20] ="";sprintf(str, format, num);
outtextxy(x, y, str);
}voiddivWindow(){
line(WIDTH -100,0, WIDTH -100, HEIGHT -40);
line(0, HEIGHT -40, WIDTH +50, HEIGHT -40);
line(WIDTH -100,130, WIDTH +50,130);
}voidinitTarget(struct TARGET words[],intn){staticcharstr[29][10] = {"main","include","void","while","for","true","false","break","int","char","float","double","switch","case","static","if","else","short","unsigned","signed","sizeof","continue","struct","union","enum","register","default","long","return"};//0-28//随机产生words[n].str = str[rand() %29];//0 1 2//判断重复,如果重复,就重新生成while(words[n].str == words[(n +1) %3].str || words[n].str == words[(n +2) %3].str)
{
words[n].str = str[rand() %29];
}
words[n].x = rand() % (WIDTH-200);
words[n].y =-20;
}voiddrawScore(){
settextcolor(LIGHTBLUE);
settextstyle(25,0,"字魂24号-镇魂手书");//软件信息输出outtextxy(WIDTH -90,25,"顿开教育");
outtextxy(WIDTH -90,25+25,"程序员专属");
outtextxy(WIDTH -90,25+25+25,"打字游戏");//游戏状态栏输出outtextxy(WIDTH -90,225,"正确数");
outtextxy_int(WIDTH -90,225+25,"%d", right);
outtextxy(WIDTH -90,285,"错误数");
outtextxy_int(WIDTH -90,285+25,"%d", error);
outtextxy(WIDTH -90,285+285-225,"正确率");//分类讨论if(right + error ==0)
{
outtextxy_double(WIDTH -90,285+285-225+25,"%.2lf%%",0.00);
}else{//C语言 除法会取整doublesum = right + error;
outtextxy_double(WIDTH -90,285+285-225+25,"%.2lf%%", right / sum *100);
}
}intmain(){
srand((unsignedint)time(NULL));
mciSendString("open 1.mp3 alias music",0,0,0);
initgraph(WIDTH+50, HEIGHT);structTARGETwords[3];//随机产生掉落的字符串for(intn =0; n <3; n++)
{
initTarget(words, n);
words[n].y =-15- n *30;//形成不登高}
BeginBatchDraw();inti =0;while(1)
{
cleardevice();
divWindow();//碰线处理for(intn =0; n <3; n++)
{
words[n].y +=2;if(words[n].y>(HEIGHT -40- textheight(words[n].str)))
{
initTarget(words, n);
}
}//打印文字for(intn =0; n <3; n++)
{
settextcolor(RED);
outtextxy(words[n].x, words[n].y, words[n].str);
}if(_kbhit())//kbhit 检测键盘,有按键返回非零{//字符串变为字符处理chartarget;//接受用户的值if((target = _getch()) !=\r)
{
userkey.str[i++] = target;
}else{intflagError =0;//干掉输入正确的字符for(i =0; i <3; i++)
{if(strcmp(userkey.str, words[i].str) ==0)
{
initTarget(words, i);
right++;
flagError =1;
mciSendString("play music",0,0,0);
}
}if(flagError ==0)
{
error++;
}//习惯很重要:边写边测试i =0;
userkey.x =320;memset(userkey.str,0,20);
}
}
outtextxy(userkey.x, userkey.y, userkey.str);
drawScore();
FlushBatchDraw();
Sleep(100);
}
getchar();
closegraph();return0;
}
写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
编程学习书籍分享:
编程学习视频分享:
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!
原文链接:https://www.w1ym.com/83141/,转载请注明出处~~~
评论0