1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include <iostream> #include <conio.h> #include <cstring> #include <windows.h> using namespace std; string map1[10] = {"####################", "#Y #", "# #", "# #", "# #", "# #", "# #", "# #", "# X#", "####################"}; string map2[13] = {"##################################", "#Y#X #", "# ############################## #", "# # #", "############################## # #", "# # # #", "# # ############### #", "# # V# #", "# # ############### #", "# V# #", "# ############################## #", "# #", "##################################"}; void setpos(int x, int y) { COORD pos; pos.X = x,pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); return; } void print(string maps[], int size, int x, int y) { setpos(0, 0); for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(i == y && j == x) { cout << "Y"; } else { if(maps[i][j] == 'V') { cout << "Y"; } else { cout << maps[i][j]; } } } cout << endl; } return; } void move(string maps[], int size) { system("cls"); int x, y, old_x, old_y; for(int i = 0; i < size; i++) { for(int j = 0; j < maps[i].size(); j++) { if(maps[i][j] == 'Y') { y = i; x = j; old_x = i; old_y = j; maps[i][j] = ' '; break; } } } while(true) { print(maps, size, x, y); char a = getch(); if(a == 'w' || a == 'W') { if(maps[y - 1][x] != '#') { y--; } } else if(a == 's' || a == 'S') { if(maps[y + 1][x] != '#') { y++; } } else if(a == 'a' || a == 'A') { if(maps[y][x - 1] != '#') { x--; } } else if(a == 'd' || a == 'D') { if(maps[y][x + 1] != '#') { x++; } } if(maps[y][x] == 'X') { system("cls"); cout << "你赢了!" << endl; system("pause"); return; } if(maps[y][x] == 'V') { system("cls"); cout << "你死了!" << endl; cout << "呃,这个出口可能是陷阱……" << endl; system("pause"); x = old_x; y = old_y; } } return; } int main() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); move(map1, 10); move(map2, 13); return 0; }
|