我的编程空间,编程开发者的网络收藏夹
学习永远不晚

C++游戏编程之模拟实现键盘打字程序

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

C++游戏编程之模拟实现键盘打字程序

程序演示:

程序代码:


#include<graphics.h>
#include<iostream>
#include<conio.h>
#include<time.h>
using namespace std;
 
class KeyBoard
{
public:
	KeyBoard();
	~KeyBoard();
	int randomKeys();//产生1~26的随机值
	void showBoard();//画键盘
	void showText();//显示键值
	void acceptAction();//获取响应
private:
	int randomKey;//随机值
	int Struct;//支撑体
	int keySize;//键块大小
	int x1, y1;//第一行的第一个键块左上角坐标
	int x2, y2;//第二行的第一个键块左上角坐标
	int x3, y3;//第三行的第一个键块左上角坐标
};
 
KeyBoard::KeyBoard()
{
	Struct = 10;
	keySize = 50;
	x1 = 50, y1 = 50;
	x2 = 70, y2 = 110;
	x3 = 90, y3 = 170;
	initgraph(1000, 400);
	showBoard();
	_getch();
}
 
KeyBoard::~KeyBoard()
{
 
}
 
void KeyBoard::showText()
{
	settextcolor(WHITE);
	TCHAR firstRowKeys[100] = _T("Q     W     E     R     T     Y     U     I     O     P");//定义字符数组
	settextstyle(20, 0, _T("楷体"));
	outtextxy(65, 60, firstRowKeys);
	TCHAR secondRowKeys[100] = _T("A     S     D     F     G     H     J     K     L");//定义字符数组
	settextstyle(20, 0, _T("楷体"));
	outtextxy(85, 125, secondRowKeys);
	TCHAR thirdRowKeys[100] = _T("Z     X     C     V     B     N     M");//定义字符数组
	settextstyle(20, 0, _T("楷体"));
	outtextxy(105, 190, thirdRowKeys);
}
 
void KeyBoard::showBoard()
{
	int tx1 = x1,tx2 = x2,tx3 = x3;
	showText();
	for (int i = 0; i < 10; i++)
	{
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		x1 = x1 + keySize + Struct;
	}
	x1 = tx1;
	for (int i = 0; i < 9; i++)
	{
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		x2 = x2 + keySize + Struct;
	}
	x2 = tx2;
	for (int i = 0; i < 7; i++)
	{
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		x3 = x3 + keySize + Struct;
	}
	x3 = tx3;
}
 
int KeyBoard::randomKeys()
{
	srand((unsigned)time(NULL));
	randomKey = rand() % 26 + 1;//1到26
	return randomKey;
}
 
void KeyBoard::acceptAction()
{
	int tx1 = x1, tx2 = x2, tx3 = x3;
	int flag = randomKeys();
	char input;
	switch (flag)
	{
	case 1:
		setlinecolor(GREEN);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'Q' || input == 'q')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'Q' || input == 'q')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		break;
	case 2:
		setlinecolor(GREEN);
		x1 = x1 + keySize + Struct;
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'W' || input == 'w')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'W' || input == 'w')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 3:
		setlinecolor(GREEN);
		x1 = x1 + 2 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'E' || input == 'e')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'E' || input == 'e')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 4:
		setlinecolor(GREEN);
		x1 = x1 + 3 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'R' || input == 'r')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'R' || input == 'r')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 5:
		setlinecolor(GREEN);
		x1 = x1 + 4 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'T' || input == 't')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'T' || input == 't')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 6:
		setlinecolor(GREEN);
		x1 = x1 + 5 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'Y' || input == 'y')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'Y' || input == 'y')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 7:
		setlinecolor(GREEN);
		x1 = x1 + 6 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'U' || input == 'u')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'U' || input == 'u')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 8:
		setlinecolor(GREEN);
		x1 = x1 + 7 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'I' || input == 'i')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'I' || input == 'i')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 9:
		setlinecolor(GREEN);
		x1 = x1 + 8 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'O' || input == 'o')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'O' || input == 'o')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 10:
		setlinecolor(GREEN);
		x1 = x1 + 9 * (keySize + Struct);
		rectangle(x1, y1, x1 + keySize, y1 + keySize);
		input = _getch();
		if (input == 'P' || input == 'p')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x1, y1, x1 + keySize, y1 + keySize);
				input = _getch();
				if (input == 'P' || input == 'p')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x1 = tx1;
		break;
	case 11:
		setlinecolor(GREEN);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'A' || input == 'a')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'A' || input == 'a')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 12:
		setlinecolor(GREEN);
		x2 = x2 + keySize + Struct;
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'S' || input == 's')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'S' || input == 's')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 13:
		setlinecolor(GREEN);
		x2 = x2 + 2 * (keySize + Struct);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'D' || input == 'd')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'D' || input == 'd')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 14:
		setlinecolor(GREEN);
		x2 = x2 + 3 * (keySize + Struct);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'F' || input == 'f')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'F' || input == 'f')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 15:
		setlinecolor(GREEN);
		x2 = x2 + 4 * (keySize + Struct);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'G' || input == 'g')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'G' || input == 'g')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 16:
		setlinecolor(GREEN);
		x2 = x2 + 5 * (keySize + Struct);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'H' || input == 'h')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'H' || input == 'h')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 17:
		setlinecolor(GREEN);
		x2 = x2 + 6 * (keySize + Struct);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'J' || input == 'j')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'J' || input == 'j')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 18:
		setlinecolor(GREEN);
		x2 = x2 + 7 * (keySize + Struct);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'K' || input == 'k')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'K' || input == 'k')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 19:
		setlinecolor(GREEN);
		x2 = x2 + 8 * (keySize + Struct);
		rectangle(x2, y2, x2 + keySize, y2 + keySize);
		input = _getch();
		if (input == 'L' || input == 'l')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x2, y2, x2 + keySize, y2 + keySize);
				input = _getch();
				if (input == 'L' || input == 'l')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x2 = tx2;
		break;
	case 20:
		setlinecolor(GREEN);
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		input = _getch();
		if (input == 'Z' || input == 'z')
		{
			setlinecolor(WHITE);
		}
		else
		{
			rectangle(x3, y3, x3 + keySize, y3 + keySize);
			input = _getch();
			if (input == 'Z' || input == 'z')
			{
				setlinecolor(WHITE);
				break;
			}
		}
		x3 = tx3;
		break;
	case 21:
		setlinecolor(GREEN);
		x3 = x3 + keySize + Struct;
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		input = _getch();
		if (input == 'X' || input == 'x')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x3, y3, x3 + keySize, y3 + keySize);
				input = _getch();
				if (input == 'X' || input == 'x')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x3 = tx3;
		break;
	case 22:
		setlinecolor(GREEN);
		x3 = x3 + 2 * (keySize + Struct);
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		input = _getch();
		if (input == 'C' || input == 'c')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x3, y3, x3 + keySize, y3 + keySize);
				input = _getch();
				if (input == 'C' || input == 'c')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x3 = tx3;
		break;
	case 23:
		setlinecolor(GREEN);
		x3 = x3 + 3 * (keySize + Struct);
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		input = _getch();
		if (input == 'V' || input == 'v')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x3, y3, x3 + keySize, y3 + keySize);
				input = _getch();
				if (input == 'V' || input == 'v')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x3 = tx3;
		break;
	case 24:
		setlinecolor(GREEN);
		x3 = x3 + 4 * (keySize + Struct);
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		input = _getch();
		if (input == 'B' || input == 'b')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x3, y3, x3 + keySize, y3 + keySize);
				input = _getch();
				if (input == 'B' || input == 'b')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x3 = tx3;
		break;
	case 25:
		setlinecolor(GREEN);
		x3 = x3 + 5 * (keySize + Struct);
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		input = _getch();
		if (input == 'N' || input == 'n')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x3, y3, x3 + keySize, y3 + keySize);
				input = _getch();
				if (input == 'N' || input == 'n')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x3 = tx3;
		break;
	case 26:
		setlinecolor(GREEN);
		x3 = x3 + 6 * (keySize + Struct);
		rectangle(x3, y3, x3 + keySize, y3 + keySize);
		input = _getch();
		if (input == 'M' || input == 'm')
		{
			setlinecolor(WHITE);
		}
		else
		{
			while (1)
			{
				rectangle(x3, y3, x3 + keySize, y3 + keySize);
				input = _getch();
				if (input == 'M' || input == 'm')
				{
					setlinecolor(WHITE);
					break;
				}
			}
		}
		x3 = tx3;
		break;
	}
}
 
int main()
{
	KeyBoard KB;
	while (1)
	{
		KB.showBoard();
		KB.acceptAction();
	}
 
	return 0;
}

以上就是C++游戏编程之模拟实现键盘打字程序的详细内容,更多关于C++模拟键盘打字的资料请关注编程网其它相关文章!

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

C++游戏编程之模拟实现键盘打字程序

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

C++如何模拟实现键盘打字程序

小编给大家分享一下C++如何模拟实现键盘打字程序,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!程序演示:程序代码:#include#inc
2023-06-22

Java编程实现打地鼠文字游戏实例代码

控制台输入数字,与随机数匹配,匹配正确则返回“打中了!”匹配错误则返回“太遗憾!没打中!”package hitmouse; import java.util.Random; import java.util.Scanner; public
2023-05-30

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录