文章详情

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

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

程序演示:

程序代码:


#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

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯