字符界面看起来是一种死板的界面方式,但是我们仍旧可以通过改变字符的背景和前景颜色来达到伪图形化界面的效果。
一些重要的函数:
SetConsoleCursorPosition(Handle,COORD);
其中COORD为一个XY二元组,这个函数的作用是把某个窗口的光标移到某个位置,这个位置不是指屏幕像素,而是指字符位置,如第二行第三个字符位置。
SetConsoleTextAttribute(Handle,short);后面的变量代表颜色值,4个字节表示前景,4个字节表示背景。可以用BACKGROUND_BLUE等来达到效果。一共有BLUE,RED,GREEN
三种颜色,但是可以用“|”组合一些颜色,因为这三种颜色代表只有一个二进制位为1,如可以用RED|GREEN得到橙色。总共的颜色也不多,照着需求用就行了。
下面粘贴代码:
1 #include <fstream> 2 #include <cstdio> 3 #include <cstring> 4 #include <ctime> 5 #include <conio.h> 6 #include <windows.h> 7 #include <algorithm> 8 #include <string> 9 #include <iostream> 10 #include <process.h> 11 #include <deque> 12 #include <cctype> 13 14 using namespace std; 15 16 17 #define FOREGROUND_WHITE FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE 18 #define FOREGROUND_BLACK 0 19 #define BACKGROUND_WHITE BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE 20 #define BACKGROUND_BLACK 0 21 #define FOREGROUND_YELLOW FOREGROUND_RED|FOREGROUND_GREEN 22 #define FOREGROUND_PERPLE FOREGROUND_BLUE|FOREGROUND_GREEN 23 #define FOREGROUND_ORRANGE FOREGROUND_BLUE|FOREGROUND_RED 24 #define BACKGROUND_YELLOW BACKGROUND_RED|BACKGROUND_GREEN 25 #define BACKGROUND_PERPLE BACKGROUND_BLUE|BACKGROUND_GREEN 26 #define BACKGROUND_ORRANGE BACKGROUND_BLUE|BACKGROUND_RED 27 DWORD dl; 28 CONSOLE_SCREEN_BUFFER_INFO csbi; 29 const HANDLE co=GetStdHandle(STD_OUTPUT_HANDLE); 30 COORD po; 31 const char heng=‘_‘; 32 const char shu=‘|‘; 33 const char zs=‘[‘,zx=‘[‘,ys=‘]‘,yx=‘]‘; 34 const char zh=‘+‘,yh=‘+‘; 35 const char cross=‘+‘; 36 const CONSOLE_CURSOR_INFO cci = {1, 0}; 37 struct appear 38 { 39 char show; 40 int color; //1==yellow,2==blue,3==red,4==black,5==white,6==green,7==perple,8==orrange,9==BackWhite; 41 appear(char ch=‘ ‘,int c=4) 42 { 43 show=ch; 44 color=c; 45 } 46 }; 47 appear Empty; 48 struct twopoint 49 { 50 int x,y; 51 52 twopoint(int xx=0,int yy=0) 53 { 54 x=xx; 55 y=yy; 56 } 57 }; 58 59 class API 60 { 61 private: 62 appear show[150][75]; 63 appear LastT[150][75]; 64 twopoint type; 65 66 public: 67 API()//Init API NULL 68 { 69 type.x=0; 70 type.y=0; 71 for (int i=0;i<150;i++) 72 { 73 for (int j=0;j<75;j++) 74 { 75 show[i][j].show=0; 76 show[i][j].color=4; 77 } 78 } 79 return; 80 } 81 82 API(int x,int y)//Init API with type 83 { 84 type.x=x; 85 type.y=y; 86 for (int i=0;i<x;i++) 87 { 88 for (int j=0;j<y;j++) 89 { 90 show[i][j].show=0; 91 show[i][j].color=4; 92 } 93 } 94 return; 95 } 96 97 void ShowAll()//Print all the API on the screen 98 { 99 po.X=0; 100 po.Y=0; 101 SetConsoleCursorPosition(co,po); 102 for (int j=1;j<=type.y;j++) 103 { 104 for (int i=1;i<=type.x;i++) 105 {//1==yellow,2==blue,3==red,4==black,5==white,6==green,7==perple,8==orrange,9==BackWhite,10==BackYellow,11=BackBlue,12=BackRed,13=Backgreen,14=BackPerple,15=BackOrrange; 106 int m=show[i][j].color; 107 if (m==1) SetConsoleTextAttribute(co, FOREGROUND_YELLOW|BACKGROUND_BLACK); 108 if (m==2) SetConsoleTextAttribute(co, FOREGROUND_BLUE|BACKGROUND_BLACK); 109 if (m==3) SetConsoleTextAttribute(co, FOREGROUND_RED|BACKGROUND_BLACK); 110 if (m==4) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_BLACK); 111 if (m==5) SetConsoleTextAttribute(co, FOREGROUND_WHITE|BACKGROUND_BLACK); 112 if (m==6) SetConsoleTextAttribute(co, FOREGROUND_GREEN|BACKGROUND_BLACK); 113 if (m==7) SetConsoleTextAttribute(co, FOREGROUND_PERPLE|BACKGROUND_BLACK); 114 if (m==8) SetConsoleTextAttribute(co, FOREGROUND_ORRANGE|BACKGROUND_BLACK); 115 if (m==9) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_WHITE); 116 if (m==10) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_YELLOW); 117 if (m==11) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_BLUE); 118 if (m==12) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_RED); 119 if (m==13) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_GREEN); 120 if (m==14) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_PERPLE); 121 if (m==15) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_ORRANGE); 122 printf("%c",show[i][j].show); 123 } 124 printf("\n"); 125 } 126 } 127 128 void Cls(bool clear=true,HANDLE hConsole=co)//Clean all the screen 129 { 130 COORD coordScreen = {0, 0}; 131 DWORD dwConSize; 132 GetConsoleScreenBufferInfo(hConsole, &csbi); 133 dwConSize = csbi.dwSize.X*csbi.dwSize.Y; 134 FillConsoleOutputCharacter(hConsole, ‘ ‘, dwConSize, coordScreen, &dl); 135 FillConsoleOutputAttribute(hConsole, FOREGROUND_WHITE|BACKGROUND_BLACK, dwConSize, coordScreen, &dl); 136 SetConsoleTextAttribute(co, FOREGROUND_WHITE|BACKGROUND_BLACK); 137 SetConsoleCursorPosition(hConsole, coordScreen); 138 if (clear) 139 { 140 for (int i=1;i<=type.x;++i) {for (int j=1;j<=type.y;j++) show[i][j]=Empty;} 141 } 142 } 143 144 145 void Clean(int LUx,int LUy,int RDx,int RDy)//Clean the screen from (LUx,LUy) to (RDx,RDy) 146 { 147 po.X=LUx-1; 148 po.Y=LUy-1; 149 SetConsoleTextAttribute(co, FOREGROUND_WHITE|BACKGROUND_BLACK); 150 SetConsoleCursorPosition(co,po); 151 for (int i=LUy;i<=RDy;i++) 152 { 153 for (int j=LUx;j<=RDx;j++) 154 { 155 show[i][j]=Empty; 156 printf(" "); 157 } 158 po.Y++; 159 SetConsoleCursorPosition(co,po); 160 } 161 } 162 163 void ShowPart(int LUx,int LUy,int RDx,int RDy)//Show the API from (LUx,LUy) to (RDx,RDy) 164 { 165 po.X=LUx; 166 po.Y=LUy; 167 SetConsoleCursorPosition(co,po); 168 for (int i=LUy;i<=RDy;i++) 169 { 170 for (int j=LUx;j<=RDx;j++) 171 {//1==yellow,2==blue,3==red,4==black,5==white,6==green,7==perple,8==orrange,9==BackWhite,10==BackYellow,11=BackBlue,12=BackRed,13=Backgreen,14=BackPerple,15=BackOrrange; 172 int m=show[j][i].color; 173 if (m==1) SetConsoleTextAttribute(co, FOREGROUND_YELLOW|BACKGROUND_BLACK); 174 if (m==2) SetConsoleTextAttribute(co, FOREGROUND_BLUE|BACKGROUND_BLACK); 175 if (m==3) SetConsoleTextAttribute(co, FOREGROUND_RED|BACKGROUND_BLACK); 176 if (m==4) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_BLACK); 177 if (m==5) SetConsoleTextAttribute(co, FOREGROUND_WHITE|BACKGROUND_BLACK); 178 if (m==6) SetConsoleTextAttribute(co, FOREGROUND_GREEN|BACKGROUND_BLACK); 179 if (m==7) SetConsoleTextAttribute(co, FOREGROUND_PERPLE|BACKGROUND_BLACK); 180 if (m==8) SetConsoleTextAttribute(co, FOREGROUND_ORRANGE|BACKGROUND_BLACK); 181 if (m==9) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_WHITE); 182 if (m==10) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_YELLOW); 183 if (m==11) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_BLUE); 184 if (m==12) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_RED); 185 if (m==13) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_GREEN); 186 if (m==14) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_PERPLE); 187 if (m==15) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_ORRANGE); 188 printf("%c",show[j][i].show); 189 } 190 po.Y++; 191 SetConsoleCursorPosition(co,po); 192 } 193 } 194 195 void ChangeOnePoint(int mux,int muy) 196 { 197 po.X=mux; 198 po.Y=muy; 199 SetConsoleCursorPosition(co,po); 200 int i=mux; 201 int j=muy; 202 int m=show[i][j].color; 203 if (m==1) SetConsoleTextAttribute(co, FOREGROUND_YELLOW|BACKGROUND_BLACK); 204 if (m==2) SetConsoleTextAttribute(co, FOREGROUND_BLUE|BACKGROUND_BLACK); 205 if (m==3) SetConsoleTextAttribute(co, FOREGROUND_RED|BACKGROUND_BLACK); 206 if (m==4) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_BLACK); 207 if (m==5) SetConsoleTextAttribute(co, FOREGROUND_WHITE|BACKGROUND_BLACK); 208 if (m==6) SetConsoleTextAttribute(co, FOREGROUND_GREEN|BACKGROUND_BLACK); 209 if (m==7) SetConsoleTextAttribute(co, FOREGROUND_PERPLE|BACKGROUND_BLACK); 210 if (m==8) SetConsoleTextAttribute(co, FOREGROUND_ORRANGE|BACKGROUND_BLACK); 211 if (m==9) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_WHITE); 212 if (m==10) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_YELLOW); 213 if (m==11) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_BLUE); 214 if (m==12) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_RED); 215 if (m==13) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_GREEN); 216 if (m==14) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_PERPLE); 217 if (m==15) SetConsoleTextAttribute(co, FOREGROUND_BLACK|BACKGROUND_ORRANGE); 218 printf("%c",show[i][j].show); 219 } 220 221 void ChangeSinglePoint(int X,int Y,appear NewPoint) 222 { 223 show[X][Y]=NewPoint; 224 }; 225 }Init,Game; 226 //map cell=(1*2)*(2*2); 227 //total=25; 228 class Box 229 { 230 private: 231 int Posx,Posy; 232 public: 233 Box(int x=0,int y=0) 234 { 235 Posx=x; 236 Posy=y; 237 } 238 239 twopoint GetPos() 240 { 241 return twopoint(Posx,Posy); 242 } 243 244 appear GetIcon() 245 { 246 return appear(‘ ‘,13); 247 } 248 249 bool PutOnAPI() 250 { 251 int RealPosx=Posy*4+1; 252 int RealPosy=Posx*2+1; 253 appear Icon=this->GetIcon(); 254 for (int i=0;i<4;++i) 255 { 256 for (int j=0;j<2;++j) 257 { 258 Game.ChangeSinglePoint(RealPosx+i,RealPosy+j,Icon); 259 } 260 } 261 return true; 262 } 263 264 bool CleanOnAPI() 265 { 266 int RealPosx=Posy*4+1; 267 int RealPosy=Posx*2+1; 268 for (int i=0;i<4;++i) 269 { 270 for (int j=0;j<2;++j) 271 { 272 Game.ChangeSinglePoint(RealPosx+i,RealPosy+j,Empty); 273 } 274 } 275 return true; 276 } 277 278 bool ShowOnScreen() 279 { 280 int RealPosx=Posy*4+1; 281 int RealPosy=Posx*2+1; 282 this->PutOnAPI(); 283 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 284 return true; 285 } 286 287 bool CleanOnScreen() 288 { 289 int RealPosx=Posy*4+1; 290 int RealPosy=Posx*2+1; 291 this->CleanOnAPI(); 292 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 293 return true; 294 } 295 296 bool Move(int toward=1)//1.2.3.4==^ > v <; 297 { 298 if (toward==1) 299 { 300 Posx--; 301 } 302 if (toward==2) 303 { 304 Posy++; 305 } 306 if (toward==3) 307 { 308 Posx++; 309 } 310 if (toward==4) 311 { 312 Posy--; 313 } 314 return true; 315 } 316 }; 317 318 class Person 319 { 320 private: 321 int Posx,Posy; 322 public: 323 Person(int x=0,int y=0) 324 { 325 Posx=x; 326 Posy=y; 327 } 328 329 twopoint GetPos() 330 { 331 return twopoint(Posx,Posy); 332 } 333 334 bool PutOnAPI() 335 { 336 //Face: @ @ 337 // .VV. 338 int RealPosx=Posy*4+1; 339 int RealPosy=Posx*2+1; 340 appear eye(‘@‘,7); 341 appear mouse(‘v‘,7); 342 appear face(‘.‘,7); 343 Game.ChangeSinglePoint(RealPosx,RealPosy,eye); 344 Game.ChangeSinglePoint(RealPosx+1,RealPosy,Empty); 345 Game.ChangeSinglePoint(RealPosx+2,RealPosy,Empty); 346 Game.ChangeSinglePoint(RealPosx+3,RealPosy,eye); 347 Game.ChangeSinglePoint(RealPosx,RealPosy+1,face); 348 Game.ChangeSinglePoint(RealPosx+1,RealPosy+1,mouse); 349 Game.ChangeSinglePoint(RealPosx+2,RealPosy+1,mouse); 350 Game.ChangeSinglePoint(RealPosx+3,RealPosy+1,face); 351 return true; 352 } 353 354 bool CleanOnAPI() 355 { 356 int RealPosx=Posy*4+1; 357 int RealPosy=Posx*2+1; 358 for (int i=0;i<4;++i) 359 { 360 for (int j=0;j<2;++j) 361 { 362 Game.ChangeSinglePoint(RealPosx+i,RealPosy+j,Empty); 363 } 364 } 365 return true; 366 } 367 368 bool ShowOnScreen() 369 { 370 int RealPosx=Posy*4+1; 371 int RealPosy=Posx*2+1; 372 this->PutOnAPI(); 373 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 374 return true; 375 } 376 377 bool CleanOnScreen() 378 { 379 int RealPosx=Posy*4+1; 380 int RealPosy=Posx*2+1; 381 this->CleanOnAPI(); 382 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 383 return true; 384 } 385 386 bool Move(int toward=1)//1.2.3.4==^ > v <; 387 { 388 if (toward==1) 389 { 390 Posx--; 391 } 392 if (toward==2) 393 { 394 Posy++; 395 } 396 if (toward==3) 397 { 398 Posx++; 399 } 400 if (toward==4) 401 { 402 Posy--; 403 } 404 return true; 405 } 406 }; 407 408 class wall 409 { 410 private: 411 int Posx,Posy; 412 public: 413 wall(int x=0,int y=0) 414 { 415 Posx=x; 416 Posy=y; 417 } 418 419 twopoint GetPos() 420 { 421 return twopoint(Posx,Posy); 422 } 423 424 appear GetIcon() 425 { 426 return appear(‘ ‘,9); 427 } 428 429 bool PutOnAPI() 430 { 431 int RealPosx=Posy*4+1; 432 int RealPosy=Posx*2+1; 433 appear Icon(‘ ‘,9); 434 for (int i=0;i<4;++i) 435 { 436 for (int j=0;j<2;++j) 437 { 438 Game.ChangeSinglePoint(RealPosx+i,RealPosy+j,Icon); 439 } 440 } 441 return true; 442 } 443 444 bool CleanOnAPI() 445 { 446 int RealPosx=Posy*4+1; 447 int RealPosy=Posx*2+1; 448 for (int i=0;i<4;++i) 449 { 450 for (int j=0;j<2;++j) 451 { 452 Game.ChangeSinglePoint(RealPosx+i,RealPosy+j,Empty); 453 } 454 } 455 return true; 456 } 457 458 bool ShowOnScreen() 459 { 460 int RealPosx=Posy*4+1; 461 int RealPosy=Posx*2+1; 462 this->PutOnAPI(); 463 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 464 return true; 465 } 466 467 bool CleanOnScreen() 468 { 469 int RealPosx=Posy*4+1; 470 int RealPosy=Posx*2+1; 471 this->CleanOnAPI(); 472 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 473 return true; 474 } 475 }; 476 477 class target 478 { 479 private: 480 int Posx,Posy; 481 public: 482 target(int x=0,int y=0) 483 { 484 Posx=x; 485 Posy=y; 486 } 487 488 twopoint GetPos() 489 { 490 return twopoint(Posx,Posy); 491 } 492 493 bool PutOnAPI() 494 { 495 //target: [][] 496 // [][] 497 int RealPosx=Posy*4+1; 498 int RealPosy=Posx*2+1; 499 appear left(‘[‘,7); 500 appear right(‘]‘,7); 501 Game.ChangeSinglePoint(RealPosx,RealPosy,left); 502 Game.ChangeSinglePoint(RealPosx+1,RealPosy,right); 503 Game.ChangeSinglePoint(RealPosx+2,RealPosy,left); 504 Game.ChangeSinglePoint(RealPosx+3,RealPosy,right); 505 Game.ChangeSinglePoint(RealPosx,RealPosy+1,left); 506 Game.ChangeSinglePoint(RealPosx+1,RealPosy+1,right); 507 Game.ChangeSinglePoint(RealPosx+2,RealPosy+1,left); 508 Game.ChangeSinglePoint(RealPosx+3,RealPosy+1,right); 509 return true; 510 } 511 512 bool CleanOnAPI() 513 { 514 int RealPosx=Posy*4+1; 515 int RealPosy=Posx*2+1; 516 for (int i=0;i<4;++i) 517 { 518 for (int j=0;j<2;++j) 519 { 520 Game.ChangeSinglePoint(RealPosx+i,RealPosy+j,Empty); 521 } 522 } 523 return true; 524 } 525 526 bool ShowOnScreen() 527 { 528 int RealPosx=Posy*4+1; 529 int RealPosy=Posx*2+1; 530 this->PutOnAPI(); 531 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 532 return true; 533 } 534 535 bool CleanOnScreen() 536 { 537 int RealPosx=Posy*4+1; 538 int RealPosy=Posx*2+1; 539 this->CleanOnAPI(); 540 Game.ShowPart(RealPosx,RealPosy,RealPosx+3,RealPosy+1); 541 return true; 542 } 543 }; 544 545 FILE* gamein; 546 547 class Map 548 { 549 private: 550 int maptype[26][26];//0==road 1== wall; 551 target Trgt[30]; 552 int Trgtnum; 553 bool full[30]; 554 Box Bx[30]; 555 Person man; 556 public: 557 Map() 558 { 559 if (gamein==NULL) return; 560 int sizex=0,sizey=0; 561 fscanf(gamein,"%d%d",&sizex,&sizey); 562 Trgtnum=0; 563 for (int i=1;i<=29;++i) full[i]=false; 564 for (int i=1;i<=25;++i) 565 for (int j=1;j<=25;j++) maptype[i][j]=0; 566 for (int i=1;i<=sizex;++i) 567 { 568 for (int j=1;j<=sizey;j++) 569 { 570 int num; 571 fscanf(gamein,"%d",&num); 572 maptype[i][j]=num; 573 } 574 } 575 fscanf(gamein,"%d",&Trgtnum); 576 for (int i=1;i<=Trgtnum;++i) 577 { 578 int x,y; 579 fscanf(gamein,"%d%d",&x,&y); 580 Trgt[i]=target(x,y); 581 maptype[x][y]=0; 582 } 583 for (int i=1;i<=Trgtnum;++i) 584 { 585 int x,y; 586 fscanf(gamein,"%d%d",&x,&y); 587 maptype[x][y]=0; 588 Bx[i]=Box(x,y); 589 } 590 int x,y; 591 fscanf(gamein,"%d%d",&x,&y); 592 man=Person(x,y); 593 } 594 595 bool show() 596 { 597 for (int i=1;i<=Trgtnum;++i) 598 { 599 Bx[i].ShowOnScreen(); 600 Trgt[i].ShowOnScreen(); 601 } 602 man.ShowOnScreen(); 603 for (int i=1;i<26;++i) 604 { 605 for (int j=1;j<26;j++) 606 { 607 if (maptype[i][j]==1) 608 { 609 wall wa(i,j); 610 wa.ShowOnScreen(); 611 } 612 } 613 } 614 return true; 615 } 616 617 bool Win() 618 { 619 for (int i=1;i<=Trgtnum;++i) 620 { 621 bool b=false; 622 for (int j=1;j<=Trgtnum;++j) 623 { 624 if (Trgt[i].GetPos().x==Bx[j].GetPos().x&&Trgt[i].GetPos().y==Bx[j].GetPos().y) 625 { 626 b=true; 627 break; 628 } 629 } 630 if (!b) return false; 631 } 632 return true; 633 } 634 635 bool GoOnePos(int to=1) 636 { 637 int dx=0,dy=0; 638 if (to==1){dx=-1;dy=0;} 639 if (to==4){dx=0;dy=-1;} 640 if (to==3){dx=1;dy=0;} 641 if (to==2){dx=0;dy=1;} 642 twopoint m=man.GetPos(); 643 if (maptype[m.x+dx][m.y+dy]!=1) 644 { 645 for (int i=1;i<=Trgtnum;++i) 646 { 647 if (Bx[i].GetPos().y==twopoint(m.x+dx,m.y+dy).y&&Bx[i].GetPos().x==twopoint(m.x+dx,m.y+dy).x) 648 { 649 twopoint c=Bx[i].GetPos(); 650 if(maptype[c.x+dx][c.y+dy]!=1) 651 { 652 for (int j=1;j<=Trgtnum;++j) 653 { 654 if (Bx[j].GetPos().y==twopoint(c.x+dx,c.y+dy).y&&Bx[j].GetPos().x==twopoint(c.x+dx,c.y+dy).x) 655 return false; 656 } 657 } 658 else 659 return false; 660 Bx[i].CleanOnScreen(); 661 for (int j=1;j<=Trgtnum;j++) 662 { 663 if (Trgt[j].GetPos().x==Bx[i].GetPos().x&&Trgt[j].GetPos().y==Bx[i].GetPos().y) 664 { 665 Trgt[j].ShowOnScreen(); 666 break; 667 } 668 } 669 Bx[i].Move(to); 670 Bx[i].ShowOnScreen(); 671 break; 672 } 673 } 674 man.CleanOnScreen(); 675 for (int j=1;j<=Trgtnum;j++) 676 { 677 if (Trgt[j].GetPos().x==man.GetPos().x&&Trgt[j].GetPos().y==man.GetPos().y) 678 { 679 Trgt[j].ShowOnScreen(); 680 break; 681 } 682 } 683 man.Move(to); 684 man.ShowOnScreen(); 685 } 686 else 687 return false; 688 return true; 689 } 690 }; 691 692 693 class game 694 { 695 private: 696 Map GameMap; 697 public: 698 game (char* filename="defult.bpmp") 699 { 700 gamein=fopen(filename,"r"); 701 GameMap=Map(); 702 fclose(gamein); 703 }; 704 705 bool Shown() 706 { 707 GameMap.show(); 708 return true; 709 }; 710 711 bool Win() 712 { 713 return GameMap.Win(); 714 }; 715 716 bool ingame() 717 { 718 GameMap.show(); 719 while (!GameMap.Win()) 720 { 721 char ch=getch(); 722 if (ch!=‘x‘) 723 this->GoOnePos(ch); 724 else 725 return false; 726 } 727 return true; 728 }; 729 730 bool GoOnePos(char ch) 731 { 732 int to=0; 733 if (ch==‘w‘||ch==‘W‘){to=1;} 734 if (ch==‘a‘||ch==‘A‘){to=4;} 735 if (ch==‘s‘||ch==‘S‘){to=3;} 736 if (ch==‘d‘||ch==‘D‘){to=2;} 737 if (to!=0) 738 GameMap.GoOnePos(to); 739 return this->Win(); 740 }; 741 742 }; 743 744 void Chanlenge() 745 { 746 char filename[255]="map\\map1"; 747 bool b=true; 748 char allinfo[255]="map\\info.log"; 749 FILE* fl=fopen(allinfo,"r"); 750 int Number; 751 int hinumber; 752 fscanf(fl,"%d",&Number); 753 fscanf(fl,"%d",&hinumber); 754 fclose(fl); 755 int sum=1; 756 while (b) 757 { 758 Game.Cls(); 759 game Gam(filename); 760 b=Gam.ingame(); 761 if (!b) 762 { 763 po.X=20; 764 po.Y=17; 765 SetConsoleCursorPosition(co,po); 766 printf("\D6\D8\D0?\AA?\A3\BF\A3\A8Y/N\A3\A9"); 767 char ch; 768 while (ch=getch()) 769 { 770 if (ch==‘Y‘||ch==‘y‘) 771 { 772 b=true; 773 break; 774 } 775 else 776 { 777 if (ch==‘n‘||ch==‘N‘) 778 break; 779 } 780 } 781 } 782 else 783 { 784 sum++; 785 char NewName[30]; 786 int now=sum; 787 int fi=1; 788 while (now/10>0) 789 { 790 NewName[fi++]=now%10+‘0‘; 791 now/=10; 792 } 793 NewName[fi]=now+‘0‘; 794 for (int j=fi;j>=1;j--) 795 { 796 filename[7+fi-j]=NewName[j]; 797 } 798 if (sum>Number) 799 { 800 po.X=20; 801 po.Y=17; 802 SetConsoleCursorPosition(co,po); 803 printf("\CD\DB~~~?\B9\D8\C1\CB? "); 804 po.Y++; 805 SetConsoleCursorPosition(co,po); 806 printf("\D7\EE\B8?\D8\CA\FD %d",max(hinumber,sum-1)); 807 if (hinumber<sum) 808 { 809 fl=fopen(allinfo,"w"); 810 fprintf(fl,"%d\n%d",Number,sum); 811 fclose(fl); 812 } 813 getchar(); 814 Game.Cls(); 815 break; 816 } 817 else 818 { 819 po.X=20; 820 po.Y=17; 821 SetConsoleCursorPosition(co,po); 822 printf("\CD\DB~~~\B9\FD\B9\D8\C1\CB?~\\(\A8R\A8\8C\A8Q)/"); 823 po.Y++; 824 SetConsoleCursorPosition(co,po); 825 getch(); 826 } 827 } 828 } 829 po.X=20; 830 po.Y=17; 831 SetConsoleCursorPosition(co,po); 832 printf(" \D4?\FB~\\(\A8R\A8\8C\A8Q)/"); 833 Sleep(1000); 834 return; 835 } 836 837 int main() 838 { 839 SetConsoleCursorInfo(co, &cci); 840 Chanlenge(); 841 return 0; 842 }
这份代码使用了Windows的库,故只能在Windows下编译运行。由于发文电脑是Linux虚拟机。。。所以如果有乱码那是汉字请见谅~~
由于是第一次写比较全面的面向对象(高中OI党一般不写面向对象的。。。)有些丑的代码和冗余代码。。
编译后效果