C++游戲編程之模擬實(shí)現(xiàn)鍵盤打字程序
更新時(shí)間:2021年12月27日 11:18:39 作者:代碼騎士
這篇文章主要介紹了通過(guò)C++模擬實(shí)現(xiàn)鍵盤打字的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下
程序演示:

程序代碼:
#include<graphics.h>
#include<iostream>
#include<conio.h>
#include<time.h>
using namespace std;
class KeyBoard
{
public:
KeyBoard();
~KeyBoard();
int randomKeys();//產(chǎn)生1~26的隨機(jī)值
void showBoard();//畫鍵盤
void showText();//顯示鍵值
void acceptAction();//獲取響應(yīng)
private:
int randomKey;//隨機(jī)值
int Struct;//支撐體
int keySize;//鍵塊大小
int x1, y1;//第一行的第一個(gè)鍵塊左上角坐標(biāo)
int x2, y2;//第二行的第一個(gè)鍵塊左上角坐標(biāo)
int x3, y3;//第三行的第一個(gè)鍵塊左上角坐標(biāo)
};
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");//定義字符數(shù)組
settextstyle(20, 0, _T("楷體"));
outtextxy(65, 60, firstRowKeys);
TCHAR secondRowKeys[100] = _T("A S D F G H J K L");//定義字符數(shù)組
settextstyle(20, 0, _T("楷體"));
outtextxy(85, 125, secondRowKeys);
TCHAR thirdRowKeys[100] = _T("Z X C V B N M");//定義字符數(shù)組
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++游戲編程之模擬實(shí)現(xiàn)鍵盤打字程序的詳細(xì)內(nèi)容,更多關(guān)于C++模擬鍵盤打字的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)紅黑樹詳細(xì)步驟+代碼
大家好,本篇文章主要講的是C語(yǔ)言實(shí)現(xiàn)紅黑樹詳細(xì)步驟+代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
海量數(shù)據(jù)處理系列之:用C++實(shí)現(xiàn)Bitmap算法
本篇文章是對(duì)用C++實(shí)現(xiàn)Bitmap算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實(shí)現(xiàn)LeetCode(123.買股票的最佳時(shí)間之三)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(123.買股票的最佳時(shí)間之三),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言實(shí)現(xiàn)單鏈表的基本功能詳解
鏈表是一個(gè)結(jié)構(gòu)體實(shí)現(xiàn)的一種線性表,它只能從前往后,不可以從后往前,在實(shí)現(xiàn)單鏈表的操作時(shí),需要用指針來(lái)操作。本文主要介紹了實(shí)現(xiàn)單鏈表的基本功能的代碼示例,具有一定價(jià)值,感興趣的同學(xué)可以學(xué)習(xí)一下2021-11-11
實(shí)例詳解C/C++中extern關(guān)鍵字
這篇文章主要介紹了C/C++中extern關(guān)鍵字詳解 的相關(guān)資料,需要的朋友可以參考下2016-04-04
c語(yǔ)言實(shí)現(xiàn)php的trim標(biāo)簽
本文給大家介紹的是使用C語(yǔ)言實(shí)現(xiàn)php的trim標(biāo)簽功能的代碼,非常的實(shí)用,其主要作用是清除字符串開頭結(jié)尾除空白,有需要的小伙伴可以參考下。2016-01-01

