zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    Spark源码分析之Sort-Based Shuffle读写流程
    浅谈Spark2.x中的Structured Streaming
    Spark应用提交
    js面向对象插件的做法框架new goBuy('.cakeItem',{ add:'.add', reduce:'.reduce' },[1,0.7,0.6]);
    dubbo 运行过程
    linux 监控命令
    DUBBO Thread pool is EXHAUSTED!
    线程池深入(li)
    高性能、高流量Java Web站点打造的22条建议
    Maven 打胖jar
  • 原文地址:https://www.cnblogs.com/youxam/p/input.html
Copyright © 2011-2022 走看看