C++游戏编程--模拟键盘打字程序
编译环境:Mictosoft Visual Studio 2022, EasyX_2021
作 者:代码骑士<1696297834@qq.com>
最后修改:2021-12-28
程序演示:
程序代码:
各位看官,喜欢本程序就点个赞呗,创作不易,拒绝白嫖!
#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到26return 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++游戏编程--模拟键盘打字程序相关推荐
- 仿金山打字通游戏,模拟键盘(java swing),提示输入,导入本地txt文件
一.设计思路 1.用一个主窗体,在主窗体上放置两个子窗体,一个是文本面板,一个是模拟键盘面板. 2.文本面板分两个子面板,一个是已有文本,一个是输入文本,在带输入文本面板上添加文件监听器,动态改变下一 ...
- C++游戏编程--模拟红绿灯程序
编译环境:Mictosoft Visual Studio 2022, EasyX_2021 作 者:代码骑士<1696297834@qq.com> 最后修改:2021-12-29 程序演示 ...
- windows游戏编程_2020年适合程序员编程的笔记本电脑推荐
在购买编程笔记本电脑时,一套智能的基准规格包括至少 8GB 的内存.像样的 SSD.强大的集成 GPU 和一个 i5 或 i7 处理器.虽然这些基准配置很好,但它们不足以帮助您找到最好的笔记本电脑.在 ...
- 游戏编程大师技巧—windows程序的基本构造
空余时间拿出来学习游戏开发时间其乐无穷的事情有木有,当然不能被导师抓住,我是专心做学术的好孩子.. 任何一个程序的人生应该是从一句dos控制台的hello word 开始的,而windows的程序应该 ...
- python模拟键盘打字_python模拟鼠标点击和键盘输入的操作
所有代码都是网上百度出来的,通过个人实践找到适合自己的. 采用的python 库是 pymouse.pykeyboard 安装时直接pip安装的,pip install PyUserInput 实现了 ...
- windows模拟键盘鼠标事件DirectX游戏中
有一些使用DirectX接口的游戏程序,它们在读取键盘操作时绕过了windows的消息机制,而使用DirectInput.这是因为有些游戏对实时性控制的要求比较高,比如赛车游戏,要求以最快速度响应键盘 ...
- .netframework游戏编程入门——模拟魔兽学院永远的羁绊
相信有很多和我一样摸索着编游戏的自学者,自学是辛苦的,一没明确的引导,二没正确的工具,我就谈一下自己的经验,也许可以让你少走一点弯路.3D 的先不谈,2D作为基础还是有必要研究,然后把地图模型一换(换 ...
- 游戏编程入门(5):使用键盘和鼠标控制游戏
接上文 游戏编程入门(4):绘制图形图像 本文内容包括: 如何有效地检测和响应键盘输入 如何处理鼠标输入 如何开发带有动画图形对象的程序,并且可以使用键盘和鼠标来控制动画图形对象 用户输入设备 输入设 ...
- c语言编程模拟机械钟表行走,C语言课程设计报告-模拟时钟转动程序
1. 课程设计报告题 目 课 程 名 称 结构化程序设计课程设计 院 部 名 称 专 业 班 级 学 生 姓 名 王蕾 学 号 课程设计地点 课程设计学时 指 导 教 师 金陵科技学院教务处制 程序设 ...
最新文章
- Delphi中的消息截获
- Eclipse Maven 编译错误 Dynamic Web Module 3.0 requires Java 1.6 or newer 解决方案
- 洛谷 T61816 代数式的最值
- TQ210裸机编程(4)——按键(中断法)
- 全民大数据时代已来 阿里数加平台详解
- Windows 7下实现×××连接自动创建
- 为什么俄罗斯不怕芯片卡脖子?
- linux中Grep常用的15个例子,Linux中Grep惯用的15个例子
- 如何用html构建ios应用,使用HTML5构建iOS原生APP(5)
- JZOJ5371 组合数问题
- visual studio 2010解决无法使用framework2.0、3.0、3.5方案
- Java基础,不需要使用复杂语句,实现出计算机功能,Java计算机,Java计算器
- H3C如何配置raid1
- PR曲线以及ROC曲线的绘制
- 紫光输入法linux,紫光拼音输入法下载_紫光拼音输入法最新版下载-太平洋下载中心...
- 技术主管和技术总监的区别_技术主管–责任圈
- 几种反函数和差角公式的推导
- Word文献右上角标注以及自动更新——以Word2016为例
- 开心--开始--开发--开心
- 前端容易忽略的 debugger 调试技巧
热门文章
- 第7章第29节:四图排版:四张图片并列排版 [PowerPoint精美幻灯片实战教程]
- 关于如何连接网络打印机
- 富文本wangEditor插件层级问题
- 程序员初入职场,如何规划好自己的职业生涯?
- 微信小程序摄像头监控_微信小程序读取摄像头 微信调用摄像头
- uni.getLocation(Object)获取经纬度和当前中文地址
- php imap 安装_php7安装imap扩展
- SpringBoot 接口防止恶意刷新和暴力请求
- UNI-APP_uni-ap自动获取状态栏高度,自定义导航栏组件
- 2023年最热门的网络安全岗位分析