C语言实现迷宫小游戏
代码如下,时间太晚,有空补注释:
1 #include<stdio.h> 2 #include<string.h> 3 #include<time.h> 4 #include<stdlib.h> 5 #include<windows.h> 6 #define n 21 7 #define m 37 8 #define up 1 9 #define down 2 10 #define left 3 11 #define right 4 12 #define Key_Up 72 13 #define Key_Down 80 14 #define Key_Right 77 15 #define Key_Left 75 16 typedef struct Point 17 { 18 int x; 19 int y; 20 int pos; 21 }point; 22 HANDLE h_out_buf;//缓冲技术解决闪屏 23 COORD coord={0,0}; 24 DWORD bytes=0; 25 char data[n+2][m*2+4]; 26 int maze[n+2][m+2]; 27 point p[10000]; 28 int l=0; 29 point start,end; 30 void add(point t) 31 { 32 p[l]=t; 33 l++; 34 } 35 void add_neibor(point t)//将当前点周围的邻墙加入数组中 36 { 37 point q; 38 if(t.x+1<=n) 39 { 40 q=t; 41 q.x=t.x+1; 42 q.pos=down; 43 add(q); 44 } 45 if(t.x-1>=1) 46 { 47 q=t; 48 q.x=t.x-1; 49 q.pos=up; 50 add(q); 51 } 52 if(t.y+1<=m) 53 { 54 q=t; 55 q.y=t.y+1; 56 q.pos=right; 57 add(q); 58 } 59 if(t.y-1>=1) 60 { 61 q=t; 62 q.y=t.y-1; 63 q.pos=left; 64 add(q); 65 } 66 } 67 void del(int index) 68 { 69 int i; 70 for(i=index;i<l-1;i++) 71 p[i]=p[i+1]; 72 l--; 73 } 74 void show_maze() 75 { 76 int i,j; 77 for(i=0;i<n+2;i++) 78 { 79 for(j=0;j<m+2;j++) 80 { 81 if(!maze[i][j]) 82 { 83 data[i][2*j]='*'; 84 data[i][2*j+1]=' '; 85 } 86 else if(maze[i][j]==1) 87 { 88 data[i][2*j]=' '; 89 data[i][2*j+1]=' '; 90 } 91 else if(maze[i][j]==2) 92 { 93 data[i][2*j]=2; 94 data[i][2*j+1]=' '; 95 } 96 else 97 { 98 data[i][2*j]=1; 99 data[i][2*j+1]=' '; 100 } 101 } 102 } 103 for(i=0;i<n+2;i++) 104 { 105 coord.Y=i; 106 WriteConsoleOutputCharacterA(h_out_buf, data[i], 2*j, coord, &bytes); 107 } 108 SetConsoleActiveScreenBuffer(h_out_buf); 109 } 110 void creat_maze() 111 { 112 int i,j,x,y; 113 point q; 114 memset(maze,0,sizeof(maze)); 115 srand((unsigned)time(NULL)); 116 start.x=1; 117 start.y=1; 118 end.x=n; 119 end.y=m; 120 maze[1][1]=1; 121 add_neibor(start); 122 while(l) 123 { 124 int r=rand()%l; 125 if(p[r].pos==up) 126 { 127 x=p[r].x-1; 128 y=p[r].y; 129 } 130 if(p[r].pos==down) 131 { 132 x=p[r].x+1; 133 y=p[r].y; 134 } 135 if(p[r].pos==left) 136 { 137 x=p[r].x; 138 y=p[r].y-1; 139 } 140 if(p[r].pos==right) 141 { 142 x=p[r].x; 143 y=p[r].y+1; 144 } 145 if(maze[x][y]==0) 146 { 147 maze[p[r].x][p[r].y]=1; 148 maze[x][y]=1; 149 q.x=x; 150 q.y=y; 151 add_neibor(q); 152 } 153 del(r); 154 } 155 maze[start.x][start.y]=2; 156 maze[end.x][end.y]=3; 157 } 158 void win() 159 { 160 int i,j; 161 char a[30]="CONGRATULATIONS!"; 162 char b[30]="YOU WIN THIS GAME!"; 163 char c[30]="AUTHOR:天道铸魂"; 164 char d[30]="按任意键结束游戏"; 165 memset(data,0,sizeof(data)); 166 for(i=0;i<n+2;i++) 167 { 168 for(j=0;j<m+2;j++) 169 { 170 data[i][2*j]=' '; 171 data[i][2*j+1]=' '; 172 } 173 } 174 strcpy(data[7]+31,a); 175 strcpy(data[9]+30,b); 176 strcpy(data[11]+31,c); 177 strcpy(data[18]+30,d); 178 for(i=0;i<n+2;i++) 179 { 180 coord.Y=i; 181 WriteConsoleOutputCharacterA(h_out_buf, data[i], 2*j, coord, &bytes); 182 } 183 SetConsoleActiveScreenBuffer(h_out_buf); 184 } 185 void play_game() 186 { 187 int key1,key; 188 point now; 189 now.x=now.y=1; 190 while(1) 191 { 192 show_maze(); 193 if(now.x==n&&now.y==m) 194 { 195 system("cls"); 196 win(); 197 break; 198 } 199 key1=getch(); 200 key=getch(); 201 if(key==Key_Up) 202 { 203 if(now.x-1>0) 204 { 205 if(maze[now.x-1][now.y]) 206 { 207 maze[now.x][now.y]=1; 208 now.x--; 209 maze[now.x][now.y]=2; 210 } 211 } 212 } 213 else if(key==Key_Down) 214 { 215 if(now.x+1<=n) 216 { 217 if(maze[now.x+1][now.y]) 218 { 219 maze[now.x][now.y]=1; 220 now.x++; 221 maze[now.x][now.y]=2; 222 } 223 } 224 } 225 else if(key==Key_Left) 226 { 227 if(now.y-1>0) 228 { 229 if(maze[now.x][now.y-1]) 230 { 231 maze[now.x][now.y]=1; 232 now.y--; 233 maze[now.x][now.y]=2; 234 } 235 } 236 237 } 238 else if(key==Key_Right) 239 { 240 if(now.y+1>0) 241 { 242 if(maze[now.x][now.y+1]) 243 { 244 maze[now.x][now.y]=1; 245 now.y++; 246 maze[now.x][now.y]=2; 247 } 248 } 249 } 250 } 251 } 252 void start_game() 253 { 254 creat_maze(); 255 play_game(); 256 } 257 int main() 258 { 259 h_out_buf = CreateConsoleScreenBuffer(GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CONSOLE_TEXTMODE_BUFFER,NULL); 260 CONSOLE_CURSOR_INFO cci; 261 cci.bVisible = 0; 262 cci.dwSize = 1; 263 SetConsoleCursorInfo(h_out_buf, &cci); 264 start_game(); 265 return 0; 266 }