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;
    }
    
    
  • 相关阅读:
    Virtualbox + centos7 实现网络互ping
    什么?https://start.spring.io访问不了,本地搭建一个不就行了
    关系型数据库设计三大范式到底是什么?
    ClickHouse集群搭建(二)
    ClickHouse集群搭建(一)
    适合初学者入门Java程序
    ffmpeg 修改视频封面
    重温于娟对癌症的认知
    Ubuntu18.04 安装jdk1.8
    提交本地代码到github (commit to remote repo)
  • 原文地址:https://www.cnblogs.com/youxam/p/input.html
Copyright © 2011-2022 走看看