效果:当按下左方向键时输出 “LLL”, 当按下右方向键时输出 “RRR”, 当按下上方向键时输出 “UUU”, 当按下下方向键时输出 “DDD”。
#include <stdio.h>#include <windows.h> //控制界面,获取坐标位置#include <stdlib.h>//宏文件#include <conio.h> //接受键盘的输入输出#define U 1#define D 2#define L 3#define R 4int key1,key2;intstatus;void snakemove(){if(status == L){printf("LLLLLLLLLLL");}if(status == R){printf("RRRRRRRRRRR");}if(status == U){printf("UUUUUUUUUUU");}if(status == D){printf("DDDDDDDDDDD");}}void keyboardControl(){status=R;snakemove();//RRRprintf("\n");while(1){if (key1 = _getch()){key2 = _getch();}if(key2==72)//方法一,up代表72,down代表80,left代表77,right代表75{status = U;//UUUsnakemove();printf("\n");}if(GetAsyncKeyState(VK_DOWN)!=0)//方法二{status = D;//DDDsnakemove();printf("\n");}if(GetAsyncKeyState(VK_LEFT)!=0){status = L;//LLLsnakemove();printf("\n");}if(GetAsyncKeyState(VK_RIGHT)!=0){status = R;//RRRsnakemove();printf("\n");}}}int main(){keyboardControl();return 0;}