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

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

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

游戏介绍:

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

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

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

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

效果图展示

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

配套讲解教程:「链接」

源代码示例:

#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++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论