1. 程式人生 > 實用技巧 >【轉載】C/走迷宮程式碼

【轉載】C/走迷宮程式碼

  1 #include<iostream>
  2 #include<windows.h>
  3 #include"GotoXY.h"
  4 #include <conio.h>
  5 #include <stdlib.h>
  6 #include <time.h>
  7 using namespace std;
  8 void help();
  9 void migong();
 10 void m();
 11 void help()
 12 {
 13  char ch2;
 14  system("cls");
15 cout<<"操作說明:"<<endl; 16 cout<<endl; 17 cout<<"移動說明:"<<endl; 18 cout<<"w-向上 s-向下 a-向左 d-向右"<<endl; 19 cout<<endl; 20 cout<<"標識說明:"<<endl; 21 cout<<"&- @-玩家 #-牆 $-鑰匙 ^-地刺 %-傳送門 0-可以被炸開的牆"<<endl; 22 cout<<endl;
23 cout<<"操作說明:"<<endl; 24 cout<<"x-爆破(需要) 移動至物品上方-撿起物品 擁有鑰匙後接近傳送門附近-傳送(按任意鍵繼續)"<<endl; 25 cout<<endl; 26 cout<<"注意事項:"<<endl; 27 cout<<"踩到地刺會s哦,請小心"<<endl; 28 cout<<"按任意鍵返回"<<endl; 29 ch2=_getch(); 30 m(); 31 } 32
void migong() 33 { 34 system("cls"); 35 char ch; 36 srand(time(NULL)); 37 int k; 38 int z=0; 39 int key=0; 40 int zx[100]; 41 int zy[100]; 42 int gq=0; 43 GotoXY(60,5); 44 cout<<"TNT x 0"; 45 while (1) 46 { 47 int map[10][6][10]={ 48 { 49 {1,1,1,1,1,1,1,1,1,1}, 50 {1,0,3,0,4,0,1,1,0,1}, 51 {1,0,0,0,0,1,1,1,2,1}, 52 {1,0,0,0,0,1,1,1,0,1}, 53 {1,0,0,0,0,0,0,0,0,1}, 54 {1,1,1,1,1,1,1,1,1,1} 55 }, 56 { 57 {1,1,1,1,1,1,1,1,1,1}, 58 {1,0,0,0,0,0,1,0,2,1}, 59 {1,0,1,0,1,1,1,0,0,1}, 60 {1,0,1,0,0,0,0,0,0,1}, 61 {1,3,1,0,0,0,0,4,0,1}, 62 {1,1,1,1,1,1,1,1,1,1} 63 }, 64 { 65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{1,0,0,0,7,7,0,0,4,1}, 117 {1,0,0,0,0,0,0,1,0,1}, 118 {1,1,1,1,1,1,1,1,1,1} 119 }, 120 { 121 {1,1,1,1,1,1,1,1,1,1}, 122 {1,7,7,2,0,0,0,7,7,1}, 123 {1,5,7,7,0,0,7,7,5,1}, 124 {1,5,7,7,0,0,7,7,5,1}, 125 {1,7,3,0,0,0,0,4,7,1}, 126 {1,1,1,1,1,1,1,1,1,1} 127 } 128 }; 129 k=rand()%10; 130 while (1) 131 { 132 int e=1; 133 int keyx,keyy; 134 int x; 135 int y; 136 int dx; 137 int dy; 138 GotoXY(0,0); 139 for (int i=0;i<6;i++) 140 { 141 for (int j=0;j<10;j++) 142 { 143 if (map[k][i][j]==1) 144 { 145 cout<<"#"; 146 } 147 if(map[k][i][j]==0) 148 { 149 cout<<" "; 150 } 151 if(map[k][i][j]==2) 152 { 153 cout<<"$"; 154 keyx=i; 155 keyy=j; 156 } 157 if (map[k][i][j]==4) 158 { 159 cout<<"%"; 160 dx=i; 161 dy=j; 162 } 163 if (map[k][i][j]==3) 164 { 165 cout<<"@"; 166 x=i; 167 y=j; 168 } 169 if (map[k][i][j]==5) 170 { 171 cout<<"&"; 172 zx[e]=i; 173 zy[e]=j; 174 e++; 175 } 176 if (map[k][i][j]==6) 177 { 178 cout<<"O"; 179 } 180 if (map[k][i][j]==7) 181 { 182 cout<<"^"; 183 } 184 } 185 cout<<endl; 186 } 187 if(x==keyx&&y==keyy) 188 { 189 key=1; 190 GotoXY(60,4); 191 cout<<"YOU GET THE KEY!!! "; 192 } 193 else if (x==dx-1&&y==dy||x==dx+1&&y==dy||x==dx&&y==dy+1||x==dx&&y==dy-1) 194 { 195 if (key==1) 196 { 197 GotoXY(60,4); 198 cout<<"YOU WIN!!! <<<按任意鍵繼續>>> "; 199 ch=_getch(); 200 key=0; 201 break; 202 } 203 else 204 { 205 GotoXY(60,4); 206 cout<<"NO KEY!!! "; 207 } 208 } 209 for (int i=1;i<=5;i++) 210 { 211 if (x==zx[i]&&y==zy[i]) 212 { 213 z+=1; 214 zx[i]=100; 215 zy[i]=100; 216 GotoXY(60,4); 217 cout<<"YOU GET A BAG OF TNT!!! "; 218 GotoXY(66,5); 219 cout<<z; 220 } 221 } 222 GotoXY(60,3); 223 cout<<""<<gq<<"" ; 224 GotoXY(0,6); 225 ch=_getch(); 226 switch (ch) 227 { 228 case 'w': 229 if (map[k][x-1][y]==1||map[k][x-1][y]==4||map[k][x-1][y]==6) 230 { 231 break; 232 } 233 else if (map[k][x-1][y]==7) 234 { 235 GotoXY(y,x-1); 236 cout<<"@"; 237 GotoXY(y,x); 238 cout<<" "; 239 GotoXY(60,4); 240 cout<<"YOU LOSE!!! "; 241 ch=_getch(); 242 system("cls"); 243 m(); 244 } 245 else 246 { 247 map[k][x-1][y]=3; 248 map[k][x][y]=0; 249 } 250 break; 251 case 's': 252 if (map[k][x+1][y]==1||map[k][x+1][y]==4||map[k][x+1][y]==6) 253 { 254 break; 255 } 256 else if (map[k][x+1][y]==7) 257 { 258 GotoXY(y,x+1); 259 cout<<"@"; 260 GotoXY(y,x); 261 cout<<" "; 262 GotoXY(60,4); 263 cout<<"YOU LOSE!!! "; 264 ch=_getch(); 265 system("cls"); 266 m(); 267 } 268 else 269 { 270 map[k][x][y]=0; 271 map[k][x+1][y]=3; 272 } 273 break; 274 case 'a': 275 if (map[k][x][y-1]==1||map[k][x][y-1]==4||map[k][x][y-1]==6) 276 { 277 break; 278 } 279 else if (map[k][x][y-1]==7) 280 { 281 GotoXY(y-1,x); 282 cout<<"@"; 283 GotoXY(y,x); 284 cout<<" "; 285 GotoXY(60,4); 286 cout<<"YOU LOSE!!! "; 287 ch=_getch(); 288 system("cls"); 289 m(); 290 } 291 else 292 { 293 map[k][x][y]=0; 294 map[k][x][y-1]=3; 295 } 296 break; 297 case 'd': 298 if (map[k][x][y+1]==1||map[k][x][y+1]==4||map[k][x][y+1]==6) 299 { 300 break; 301 } 302 else if (map[k][x][y+1]==7) 303 { 304 GotoXY(y+1,x); 305 cout<<"@"; 306 GotoXY(y,x); 307 cout<<" "; 308 GotoXY(60,4); 309 cout<<"YOU LOSE!!! "; 310 ch=_getch(); 311 system("cls"); 312 m(); 313 } 314 else 315 { 316 map[k][x][y]=0; 317 map[k][x][y+1]=3; 318 } 319 break; 320 case 'x': 321 if (z>0) 322 { 323 z--; 324 GotoXY(66,5); 325 cout<<z<<" "; 326 for (int m=x-1;m<=x+1;m++) 327 { 328 for (int d=y-1;d<=y+1;d++) 329 { 330 if (map[k][m][d]==6) 331 { 332 map[k][m][d]=0; 333 } 334 } 335 } 336 } 337 else 338 { 339 GotoXY(60,4); 340 cout<<"YOU DON'T HAVE ANY TNT!!! "; 341 } 342 break; 343 } 344 } 345 GotoXY(0,7); 346 gq++; 347 key=0; 348 } 349 } 350 void m() 351 { 352 system("cls"); 353 char ch1; 354 system("title 迷宮-作者S_JH330206"); 355 cout<<" 迷宮遊戲"<<endl; 356 cout<<"----------------------------------------------------------------"<<endl; 357 cout<<endl; 358 cout<<" [h]幫助"<<endl; 359 cout<<" [m]遊戲"<<endl; 360 cout<<endl; 361 cout<<"----------------------------------------------------------------"<<endl; 362 ch1=_getch(); 363 if (ch1=='m') 364 { 365 migong(); 366 } 367 if (ch1=='h') 368 { 369 help(); 370 } 371 else 372 { 373 m(); 374 } 375 } 376 int main() 377 { 378 m(); 379 return 0; 380 }

這個是指令碼的自定義標頭檔案:

1 #include<iostream>
2 #include<windows.h>
3 void GotoXY(int x, int y)  // 移動 
4 {
5  HANDLE hout;  //螢幕尺寸 變數 
6  COORD coord={x,y};  //游標座標 變數  游標x標  游標y標 
7  hout=GetStdHandle(STD_OUTPUT_HANDLE);  //獲得螢幕尺寸 
8  SetConsoleCursorPosition(hout,coord);  //移動游標 
9 }