使用curses.h函数库实现字符动画 No.2

*******************************************

*                                 书接上回                                     *

*******************************************

 

 

从键盘获取输入

 

 

几个函数:

 

 

int cbreak(void)       打开中断模式

 

 

int nocbreak(void)     关闭中断模式

 

 

crmode()    似与cbreak()功能相同

 

 

int echo(void)    打开回显

 

 

int noecho(void)       关闭回显

 

 

int getch(void)

 

 

int getstr(char *string)

 

 

int getnstr(char *string,int number_of_characters)

 

 

int scanw(char *format,......)     scanf()类似

 

 

*********************************************************************************************************************

一段小程序,实现的功能是常用的输入用户名和密码:

 

 

/*passwordtest.c*/

#include <unistd.h>

#include <stdio.h>

#include <curses.h>

#include <string.h>

 

 

int main(int argc,char* argv[])

{

char name[20];

char password[20];

char *real_password="123456";

initscr();

move(5,10);

addstr("User name:");

getnstr(name,sizeof(name));/*获得输入*/

 

 

move(7,10);

addstr("Password:");

refresh();

 

 

cbreak();/*关闭中断*/

noecho();/*关闭回显*/

memset(password,0,sizeof(password));/*内存申请,并赋初值为空*/

//printw("/n%s",password);

 

 

int len = sizeof(password);

for(int i=0;i<len;i++)

{

    password[i]=getch();

    move(7,20+i);

    addch('*');

    refresh();

    if(password[i]=='/n')

       break;

    if(strcmp(password,real_password)==0)

       break;

}

 

 

echo();/*打开回显*/

nocbreak();

 

 

move(9,10);

if(strcmp(password,real_password)==0)

    addstr("Correct");

else

    addstr("Wrong");

//printw("/n%s",password);

refresh();

sleep(10);

endwin();

 

 

 

 

return 0;

}

 

 

编译gcc passwordtest.c -o passwordtest.o -lcurses -std=c99

 

 

注意:for循环在C99模式下才可使用,编译需要加上参数-std=c99

 

 

 

 

试试看~

 

 

 

 

*******************************************************************************************************************

 

 

在屏幕上显示一个字符,用户按上下左右键使字母移动

 

 

函数:

 

 

int keypad(WINDOW *window_ptr,bool keypad_on)        keypad_on参数设置为true,然后调用keypad()函数来启用keypad模式

 

 

 

 

程序:

/*keypadtest.c*/

#include <curses.h>

#define MIN(a,b) a<b?a:b

#define MAX(a,b) a>b?a:b

 

 

int main(int argc,char* argv[])

{

int x=10;

int y=10;

char ch='A';

 

 

initscr();

crmode();

noecho();

clear();

keypad(stdscr,TRUE);

mvaddch(y,x,ch);

chtype input;

while((input=getch())&&input!=ERR &&input!='q')

{

    if((input>='A'&&input<='Z')||(input>='a'&&input<='z'))

       ch = input;

    else{

       switch(input){

           case KEY_LEFT:

              x=MAX(x-1,0);

              break;

           case KEY_RIGHT:

              x=MIN(x+1,COLS);

              break;

           case KEY_UP:

              y=MAX(y-1,0);

              break;

           case KEY_DOWN:

              y=MIN(y+1,LINES);

              break;

           }

    }

    clear();

    mvaddch(y,x,ch);

    refresh();

}

endwin();

return 0;

}

 

 

编译 gcc keypadtest.c -o keypadtest.o -lcurses

 

 

 

 

**************************************************************************************************************************

 

 

by Naturalsd

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值