在linux下面实现检测按键----实现Linux 下的kbhit函数

突然有朋友问起来在linux下怎么实现c的非阻塞情况监测按键,所以想到vc的kbhit函数,通过IO监听能实现

比较懒,借鉴了网友的代码,验证可行!

 //用非阻塞io
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main(void)
{
while(!kbhit())
puts("Press a key!");
printf("You pressed '%c'!/n", getchar());
return 0;
}


参考: http://blog.csdn.net/lxh1230119/article/details/7784914

首先,你需要在EasyX的官网上下载并安装EasyX图形库。然后,你可以按照以下步骤来实现五子棋的基本功能: 1. 在程序中包含EasyX头文件和C++标准库头文件: ```cpp #include <graphics.h> #include <iostream> #include <conio.h> #include <time.h> using namespace std; ``` 2. 初始化图形窗口和棋盘: ```cpp int main() { initgraph(640, 480); // 初始化窗口 setbkcolor(WHITE); // 设置背景颜色为白色 cleardevice(); // 清空窗口 // 绘制棋盘 setlinecolor(BLACK); setlinestyle(PS_SOLID, 2); for (int i = 0; i < 15; i++) { line(40, 40 + i * 30, 430, 40 + i * 30); line(40 + i * 30, 40, 40 + i * 30, 430); } getch(); // 等待用户按下任意键 closegraph(); // 关闭图形窗口 return 0; } ``` 3. 实现落子功能: ```cpp int main() { initgraph(640, 480); setbkcolor(WHITE); cleardevice(); // 绘制棋盘 setlinecolor(BLACK); setlinestyle(PS_SOLID, 2); for (int i = 0; i < 15; i++) { line(40, 40 + i * 30, 430, 40 + i * 30); line(40 + i * 30, 40, 40 + i * 30, 430); } int x, y; // 落子的坐标 while (true) { if (_kbhit()) { // 检测是否有按键按下 char ch = _getch(); if (ch == 27) break; // 如果按下ESC键,退出游戏 else if (ch == ' ') { // 如果按下空格键,落子 x = (mousex() - 40) / 30; // 计算落子的列 y = (mousey() - 40) / 30; // 计算落子的行 if (x >= 0 && x < 15 && y >= 0 && y < 15) { // 判断落子是否在棋盘内 setfillcolor(BLACK); solidcircle(40 + x * 30, 40 + y * 30, 13); // 绘制黑子 } } } } getch(); closegraph(); return 0; } ``` 4. 实现判断胜负功能: ```cpp int main() { initgraph(640, 480); setbkcolor(WHITE); cleardevice(); // 绘制棋盘 setlinecolor(BLACK); setlinestyle(PS_SOLID, 2); for (int i = 0; i < 15; i++) { line(40, 40 + i * 30, 430, 40 + i * 30); line(40 + i * 30, 40, 40 + i * 30, 430); } int board[15][15] = { 0 }; // 棋盘数组,0表示空格,1表示黑子,2表示白子 int x, y; // 落子的坐标 int player = 1; // 当玩家,1表示黑方,2表示白方 while (true) { if (_kbhit()) { // 检测是否有按键按下 char ch = _getch(); if (ch == 27) break; // 如果按下ESC键,退出游戏 else if (ch == ' ') { // 如果按下空格键,落子 x = (mousex() - 40) / 30; // 计算落子的列 y = (mousey() - 40) / 30; // 计算落子的行 if (x >= 0 && x < 15 && y >= 0 && y < 15 && board[y][x] == 0) { // 判断落子是否在棋盘内且该位置没有棋子 if (player == 1) { setfillcolor(BLACK); solidcircle(40 + x * 30, 40 + y * 30, 13); // 绘制黑子 board[y][x] = 1; // 在棋盘数组中标记为黑子 player = 2; // 下一步轮到白方 } else { setfillcolor(WHITE); solidcircle(40 + x * 30, 40 + y * 30, 13); // 绘制白子 board[y][x] = 2; // 在棋盘数组中标记为白子 player = 1; // 下一步轮到黑方 } if (checkWin(board, x, y) == 1) { // 检查是否有一方胜利 if (player == 1) cout << "黑方胜利!" << endl; else cout << "白方胜利!" << endl; break; } } } } } getch(); closegraph(); return 0; } int checkWin(int board[][15], int x, int y) { int player = board[y][x]; int count = 0; // 判断横向是否有五子相连 for (int i = x - 4; i <= x; i++) { if (i >= 0 && i + 4 < 15 && board[y][i] == player && board[y][i + 1] == player && board[y][i + 2] == player && board[y][i + 3] == player && board[y][i + 4] == player) { return 1; } } // 判断纵向是否有五子相连 for (int i = y - 4; i <= y; i++) { if (i >= 0 && i + 4 < 15 && board[i][x] == player && board[i + 1][x] == player && board[i + 2][x] == player && board[i + 3][x] == player && board[i + 4][x] == player) { return 1; } } // 判断正斜线是否有五子相连 for (int i = x - 4, j = y - 4; i <= x && j <= y; i++, j++) { if (i >= 0 && i + 4 < 15 && j >= 0 && j + 4 < 15 && board[j][i] == player && board[j + 1][i + 1] == player && board[j + 2][i + 2] == player && board[j + 3][i + 3] == player && board[j + 4][i + 4] == player) { return 1; } } // 判断反斜线是否有五子相连 for (int i = x + 4, j = y - 4; i >= x && j <= y; i--, j++) { if (i >= 0 && i - 4 < 15 && j >= 0 && j + 4 < 15 && board[j][i] == player && board[j + 1][i - 1] == player && board[j + 2][i - 2] == player && board[j + 3][i - 3] == player && board[j + 4][i - 4] == player) { return 1; } } return 0; } ``` 这样,你就可以用EasyX工具实现五子棋的基本功能了。当然,这只是一个简单的示例,你可以根据需要进行更改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值