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(" "); 86 } 87 printf(" 人的 X 坐标:%d",P_X); 88 printf(" 人的 Y 坐标:%d",P_Y); 89 printf(" 信息:%s",WARNING); 90 sleep(1); //刷屏延时 91 system("cls"); //刷新屏幕 92 } 93 94 }
展示效果:
更新效果:
: