欢迎来到小居数码网-一家分享数码知识,生活小常识的网站,希望可以帮助到您。

当前位置:生活小常识 > 数码知识 >
优质

c++打字母游戏代码(打字母游戏C语言程序代码)

数码知识

周月如优秀作者

原创内容 来源:小居数码网 时间:2024-07-30 15:55:01 阅读() 收藏:25 分享:78

导读:您正在阅读的是关于【数码知识】的问题,本文由科普作家协会,生活小能手,著名生活达人等整理监督编写。本文有1404个文字,大小约为5KB,预计阅读时间4分钟。

这篇文章主要为大家详细介绍了C语言实现——《打字练习系统》,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下!

打字母游戏C语言程序代码

游戏介绍:

字母游戏》是一款敏捷打字小游戏,游戏大小为468K。背景设定 《字母游戏》是一款有趣的打字游戏,可以提高你的打字速度。操作指南 根据出现的字母,按键盘A-Z键对应的按键即可。游戏加载完毕点击[开始游戏]即可开始游戏。在限定时间内,尽可能地输入正确的字母,挑战高分!

本项目针对C语言学习者,将我们打字母的“字母”置换成了C语言关键字,记在一定时间内及时输出C语言关键字就可以得分!我们一起来看看吧!

本项目编译环境:VS2019/VS2013;

插件:图形库插件easyX,涉及图片素材可以自行百度找也可以关注文末领取;

效果图展示

配套讲解教程:「链接」

源代码示例:

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <time.h>#include <stdlib.h>#include <conio.h>#include <graphics.h>//报错,请先安装#include <mmsystem.h>#pragma comment(lib,"winmm.lib")//数据设计设计//窗口属性:const int WIDTH = 640;const int HEIGHT = 500;//游戏正确率和错误率int right = 0;int error = 0;//下坠文字的结构体struct TARGET{//每一个字符串的x,y坐标int x;int y;char *str;//保存字符串};//用户输入的值struct USRKEY{int x;int y;char str[20];}userkey = {320,500-30,""};//在指定位置输出整数void outtextxy_int(int x, int y, char *format, int num){char str[20] = "";//printf;sprintf(str, format, num);outtextxy(x, y, str);}//在指定位置输出浮点数void outtextxy_double(int x, int y, char *format, double num){char str[20] = "";sprintf(str, format, num);outtextxy(x, y, str);}void divWindow(){line(WIDTH - 100, 0, WIDTH - 100, HEIGHT - 40);line(0, HEIGHT - 40, WIDTH + 50, HEIGHT - 40);line(WIDTH - 100, 130, WIDTH + 50, 130);}void initTarget(struct TARGET words[], int n){static char str[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;}void drawScore(){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语言 除法会取整double sum = right + error;outtextxy_double(WIDTH - 90, 285 + 285 - 225 + 25, "%.2lf%%", right / sum * 100);}}int main(){srand((unsigned int)time(NULL));mciSendString("open 1.mp3 alias music", 0, 0, 0);initgraph(WIDTH+50, HEIGHT);struct TARGET words[3];//随机产生掉落的字符串for (int n = 0; n < 3; n++){initTarget(words, n);words[n].y = -15 - n * 30; //形成不登高}BeginBatchDraw();int i = 0;while (1){cleardevice();divWindow();//碰线处理for (int n = 0; n < 3; n++){words[n].y += 2;if (words[n].y>(HEIGHT - 40 - textheight(words[n].str))){initTarget(words, n);}}//打印文字for (int n = 0; n < 3; n++){settextcolor(RED); outtextxy(words[n].x, words[n].y, words[n].str);}if (_kbhit())//kbhit 检测键盘,有按键返回非零{//字符串变为字符处理char target;//接受用户的值if ((target = _getch()) != 'r'){userkey.str[i++] = target;}else{int flagError = 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();return 0;}

写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!

上面就是小居数码小编今天给大家介绍的关于(打字母游戏C语言程序代码)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。

94%的朋友还想知道的:

(391)个朋友认为回复得到帮助。

部分文章信息来源于以及网友投稿,转载请说明出处。

本文标题:c++打字母游戏代码(打字母游戏C语言程序代码):http://sjzlt.cn/shuma/152919.html

猜你喜欢