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 // 2020.06.04 1.改善了行走地圖限制的問題,優化演算法為 P_X±P_SPEED大于或者小于地圖邊界時,位置設定為地圖邊界以防止小人越界, 11 // 2.增加了持槍功能(目前沒什么用處……) 12 // 13 //=================================================== 14 15 void main(){ 16 typedef enum{false, true}bool; 17 18 int i,j; 19 20 int P_X = 2; //身體位置X坐標 21 int P_Y = 7; //身體位置Y坐標,以腳為準 22 int HEIGHT = 12; //高度 23 int WEIGHT = 12; //寬度 24 int P_SPEED = 4; //移動速度 25 char WARNING[10] = " "; //警告內容 26 27 char people_head = '0'; //胳臂 28 char people_arm = 'L'; //胳臂 29 char people_leg = 'X'; //腿 30 31 char bullet = '-'; //子彈 32 bool b_sign = false; //判斷開槍 33 34 char graph[HEIGHT][WEIGHT]; //地圖陣列 35 36 while(1){ 37 for(i=0;i<HEIGHT;i++){ //生成地圖 38 for(j=0;j<WEIGHT;j++){ 39 if(i==8){ 40 graph[i][j] = '='; //陸地 41 } 42 else{ 43 graph[i][j] = ' '; //空氣 44 } 45 } 46 } 47 48 if(GetAsyncKeyState(VK_SPACE)){ //跳躍 49 50 } 51 if(GetAsyncKeyState(VK_LEFT)){ //向左 52 if(P_X==0 || P_X-P_SPEED<0){ 53 P_X=0; 54 strcpy(WARNING,"不能移動!"); 55 } 56 else{ 57 P_X-=P_SPEED; 58 strcpy(WARNING," "); 59 } 60 } 61 if(GetAsyncKeyState(VK_RIGHT)){ //向右 62 if(P_X==11 || P_X+P_SPEED>11){ 63 P_X=11; 64 strcpy(WARNING,"不能移動!"); 65 } 66 else{ 67 P_X+=P_SPEED; 68 strcpy(WARNING," "); 69 } 70 } 71 if(GetAsyncKeyState('Z')){ //發射子彈 72 b_sign = !b_sign; 73 } 74 75 graph[P_Y-2][P_X] = people_head; //放置人 76 graph[P_Y-1][P_X] = people_arm; 77 graph[P_Y][P_X] = people_leg; 78 if(b_sign==true) 79 graph[P_Y-1][P_X+1] = bullet; 80 81 for(i=0;i<HEIGHT;i++){ //顯示影像 82 for(j=0;j<WEIGHT;j++){ 83 printf("%c ",graph[i][j]); 84 } 85 printf("\n"); 86 } 87 printf("\n人的 X 坐標:%d",P_X); 88 printf("\n人的 Y 坐標:%d",P_Y); 89 printf("\n資訊:%s",WARNING); 90 sleep(1); //刷屏延時 91 system("cls"); //重繪螢屏 92 } 93 94 }
展示效果:

更新效果:
:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/24363.html
標籤:C
上一篇:中斷模式佇列
