• c++输入的高级操作


    不等待回车直接输入一个字符

    仅在Linux下有效

    #include <termio.h>
    int getcharl() {
    	struct termios new_settings;
    	struct termios stored_settings;
    	tcgetattr(0,&stored_settings);
    	new_settings = stored_settings;
    	new_settings.c_lflag &= (~ICANON);
    	new_settings.c_cc[VTIME] = 0;
    	new_settings.c_cc[VMIN] = 1;
    	tcsetattr(0, TCSANOW, &new_settings);
    	int c = getchar();
    	tcsetattr(0, TCSANOW, &stored_settings);
    	return c;
    }
    

    输入不回显

    仅在Linux下有效

    #include <cstdlib>
    system("stty -echo"); //输入不回显
    system("stty echo"); // 输入回显
    

    根据这个搞出来的迷惑操作

    方向键控制

    #include <cstdio>                     
    #include <termio.h>
    #include <cstdlib>
    #include <queue>
    #include <utility>
    #include <ctime>
    #define UP 65
    #define LEFT 68
    #define DOWN 66
    #define RIGHT 67
    using namespace std;
    int getcharl() {
    	struct termios new_settings;
    	struct termios stored_settings;
    	tcgetattr(0,&stored_settings);
    	new_settings = stored_settings;
    	new_settings.c_lflag &= (~ICANON);
    	new_settings.c_cc[VTIME] = 0;
    	new_settings.c_cc[VMIN] = 1;
    	tcsetattr(0, TCSANOW, &new_settings);
    	int c = getchar();
    	tcsetattr(0, TCSANOW, &stored_settings);
    	return c;
    }
    void gt(int x, int y) {
    	printf("33[%d;%dH", x, y);
    }
    int main() {
    	srand(time(0));
    	printf("33c33[?25l");
    	int x = 10, y = 10, lx, ly, len = 20;
    	system("stty -echo");
    	queue<pair<int, int> > q;
    	int px = rand() % 27 + 1, py = rand() % 77 + 1;
    	while(1) {
    		gt(px, py), printf("33[32;7m 33[0m");
    		while(q.size() > len) {
    			gt(q.front().first, q.front().second);
    			printf("  ");
    			q.pop();
    		}
    		gt(x, y), printf("33[31;7m 33[0m");
    		lx = x, ly = y;
    		int c = getcharl();
    		if (c == UP) x--;
    		else if(c == DOWN) x++;
    		else if(c == LEFT) y--;
    		else if(c == RIGHT) y++;
    		if (x < 1) x = 30;
    		if (y < 1) y = 80;
    		if (x > 30) x = 1;
    		if (y > 80) y = 1;
    		if (px == x && py == y) len+=10, px = rand() % 27 + 1, py = rand() % 77 + 1;
    		q.push(make_pair(x,y));
    		gt(lx, ly);
    	}
    	system("stty echo");
    	return 0;
    }
    
    
  • 相关阅读:
    es5预览本地文件、es6练习代码演示案例
    Java实现 LeetCode 838 推多米诺(暴力模拟)
    Java实现 LeetCode 838 推多米诺(暴力模拟)
    Java实现 LeetCode 838 推多米诺(暴力模拟)
    Java实现 LeetCode 837 新21点(DP)
    Java实现 LeetCode 837 新21点(DP)
    Java实现 LeetCode 837 新21点(DP)
    Java实现 LeetCode 836 矩形重叠(暴力)
    Subversion under Linux [Reprint]
    Subversion how[Reprint]
  • 原文地址:https://www.cnblogs.com/youxam/p/input.html
Copyright © 2020-2023  润新知