1 #include "stdio.h" 2 #include "time.h" 3 #include <windows.h> 4 5 // 6 // 7 // Title: C語言實現小人移動 V1.0 8 // Author: 鄒陽 9 // Date : 2020/05/28 10 // 11 ...
1 #include "stdio.h" 2 #include "time.h" 3 #include <windows.h> 4 5 //=================================================== 6 // 7 // Title: C語言實現小人移動 V1.0 8 // Author: 鄒陽 9 // Date : 2020/05/28 10 // 11 //=================================================== 12 13 14 15 void main(){ 16 int i,j; 17 18 int P_X = 2; //身體位置X坐標 19 int P_Y = 7; //身體位置Y坐標,以腳為準 20 int HEIGHT = 12; //高度 21 int WEIGHT = 12; //寬度 22 int P_SPEED = 1; //移動速度 23 char WARNING[10] = " "; //警告內容 24 25 char people_head = '0'; //胳臂 26 char people_arm = 'L'; //胳臂 27 char people_leg = 'X'; //腿 28 29 char graph[HEIGHT][WEIGHT]; //地圖數組 30 31 while(1){ 32 for(i=0;i<HEIGHT;i++){ //生成地圖 33 for(j=0;j<WEIGHT;j++){ 34 if(i==8){ 35 graph[i][j] = '='; //陸地 36 } 37 else{ 38 graph[i][j] = ' '; //空氣 39 } 40 } 41 } 42 43 if(GetAsyncKeyState(VK_SPACE)){ //跳躍 44 45 } 46 else if(GetAsyncKeyState(VK_LEFT)){ //向左 47 if(P_X>0 && P_X<=11){ 48 P_X-=P_SPEED; 49 strcpy(WARNING," "); 50 } 51 else{ 52 strcpy(WARNING,"不能移動!"); 53 P_X+=1; 54 } 55 } 56 else if(GetAsyncKeyState(VK_RIGHT)){ //向右 57 if(P_X>0 && P_X<=11){ 58 P_X+=P_SPEED; 59 strcpy(WARNING," "); 60 } 61 else{ 62 strcpy(WARNING,"不能移動!"); 63 P_X-=1; 64 } 65 } 66 67 graph[P_Y-2][P_X] = people_head; //放置人 68 graph[P_Y-1][P_X] = people_arm; 69 graph[P_Y][P_X] = people_leg; 70 71 for(i=0;i<HEIGHT;i++){ //顯示圖像 72 for(j=0;j<WEIGHT;j++){ 73 printf("%c ",graph[i][j]); 74 } 75 printf("\n"); 76 } 77 printf("\n人的 X 坐標:%d",P_X); 78 printf("\n人的 Y 坐標:%d",P_Y); 79 printf("\n信息:%s",WARNING); 80 sleep(1); //刷屏延時 81 system("cls"); //刷新屏幕 82 } 83 84 }
展示效果: