- 相關(guān)推薦
C語言中g(shù)etch()函數(shù)詳解(附實例)
C語言中g(shù)etch()函數(shù)是一個不回顯函數(shù),以下是百分網(wǎng)小編搜索整理的關(guān)于C語言中g(shù)etch()函數(shù)詳解(附實例),供參考學習,希望對大家有所幫助!想了解更多相關(guān)信息請持續(xù)關(guān)注我們應屆畢業(yè)生考試網(wǎng)!
前言:
這個函數(shù)是一個不回顯函數(shù),當用戶按下某個字符時,函數(shù)自動讀取,無需按回車,有的C語言命令行程序會用到此函數(shù)做游戲,但是這個函數(shù)并非標準函數(shù),要注意移植性!
所以有這樣的一個接口,那就很牛逼了,至少可以做個游戲來玩下,結(jié)合ASCII碼,很容易寫個方向鍵控制的2048或者貪吃蛇等等有趣的游戲出來。
以下是以一個簡單的例子:
你會發(fā)現(xiàn)當你按下對應的按鍵的時候就會打印相應的語句。
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <conio.h>
#define ESC 0x1B
#define ENTER 0x0D
#define SPACE 0x20
#define KEY_UP 72 //上
#define KEY_DOWN 80 //下
#define KEY_LEFT 75 //左
#define KEY_RIGHT 77 //右
int KEY_EXIT_STATU = 0 ;
int KEY_ENTER_STATU = 0 ;
int KEY_SPACE_STATU = 0 ;
int KEY_UP_STATU = 0 ;
int KEY_DOWN_STATU = 0 ;
int KEY_LEFT_STATU = 0 ;
int KEY_RIGHT_STATU = 0 ;
char ch ;
int get_value() ;
int main(void)
{
int i = 0;
while(1)
{
get_value();
}
return 0 ;
}
int get_value()
{
ch = getch() ;
system("cls");
switch(ch)
{
case ESC : KEY_EXIT_STATU = 1 ;
printf("退出\n") ; break ;
case ENTER :KEY_ENTER_STATU = 1 ;
printf("回車\n") ; break ;
case SPACE :
KEY_SPACE_STATU = 1 ;
printf("空格\n") ; break ;
case KEY_UP:case 'w' :
KEY_UP_STATU = 1 ;
printf("上\n") ; break ;
case KEY_DOWN:case 's' :
KEY_DOWN_STATU = 1 ;
printf("下\n") ; break ;
case KEY_LEFT:case 'a' :
KEY_LEFT_STATU = 1 ;
printf("左\n") ; break ;
case KEY_RIGHT:case 'd':
KEY_RIGHT_STATU = 1 ;
printf("右\n") ; break ;
}
}
【C語言中g(shù)etch()函數(shù)詳解(附實例)】相關(guān)文章:
c語言中g(shù)etch的用法06-01
C語言中Swift函數(shù)調(diào)用實例09-25
c語言中有關(guān)隨機函數(shù)的使用詳解07-23
C語言中指針變量作為函數(shù)參數(shù)詳解07-01
C語言中函數(shù)的區(qū)分08-30
c語言中time函數(shù)的用法08-27