Advertisement
Guest User

Untitled

a guest
Sep 6th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. //////////////////// Constants //////////////////////
  9.  
  10. //Map dimension
  11. #define MAP_WIDTH 30
  12. #define MAP_HEIGHT 17
  13.  
  14. //Tile Types
  15. #define TILE_GRASS 0
  16. #define TILE_WALL 1
  17. #define TILE_FLOOR 2
  18. #define TILE_TREE 3
  19. #define TILE_OPENDOOR 4
  20. #define TILE_CLOSEDDOOR 5
  21. #define TILE_WATER 6
  22.  
  23. /////////////////// Data Structures //////////////////
  24.  
  25. //Tile Types
  26. struct TILE_TYPE
  27. {
  28. char nCharacter; //ASCII character for this tile type
  29. short nColourCode; //Colour code for this used in the game
  30. bool bPassable; //TRUE if you can walk on this
  31. };
  32.  
  33. ////////////////// Globals /////////////////////////////
  34.  
  35. //Declare the player's position
  36. int nPlayerX=4, nPlayerY=4;
  37.  
  38. //Colour
  39. HANDLE colour = GetStdHandle(STD_OUTPUT_HANDLE);
  40.  
  41. // Map declaration
  42. int nMapArray[MAP_HEIGHT][MAP_WIDTH] =
  43. {
  44. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3, 3 },
  45. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3, 3 },
  46. { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3 },
  47. { 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 1, 2, 2, 2, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,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0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 3 },
  59. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 3, 3, 3, 3, 3, 3, 3 },
  60. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }
  61. };
  62.  
  63. //Tile definition
  64. TILE_TYPE sTileIndex [] = {
  65. {',', 10, true}, // (0) - TILE_GRASS
  66. {'#', 7, false}, // (1) - TILE_WALL
  67. {'.', 7, true}, // (2) - TILE_FLOOR
  68. {'T', 2, false}, // (3) - TILE_TREE
  69. {'/', 6, true}, // (4) - TILE_OPENDOOR
  70. {'_', 6, false}, // (5) - TILE_CLOSEDDOOR
  71. {'~', 9, false}, // (6) - TILE_WATER
  72.  
  73. };
  74.  
  75. ////////////////// Functions /////////////////////////
  76.  
  77. //cursor position
  78. void gotoxy(int x, int y)
  79. {
  80. COORD c;
  81. c.X=x;
  82. c.Y=y;
  83. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
  84. }
  85.  
  86. //Pauli's exclusion
  87. bool isPassable(int nMapX, int nMapY)
  88. {
  89. //Are the coordinates valid?
  90. if ( nMapX <0 || nMapX >= MAP_WIDTH || nMapY <0 || nMapY >= MAP_HEIGHT)
  91. return false;
  92.  
  93. // Number of specified tile
  94. int nTileValue = nMapArray[nMapY][nMapX];
  95.  
  96. // Return True if it's passable
  97. return sTileIndex[nTileValue].bPassable;
  98. }
  99.  
  100. //Draw Tile
  101. void DrawTile ( int x, int y)
  102. {
  103. void gotoxy(int x, int y);
  104. int nType = nMapArray[y][x];
  105. SetConsoleTextAttribute(colour, sTileIndex[nType].nColourCode);
  106. cout<<sTileIndex[nType].nCharacter;
  107. }
  108.  
  109. //Draw map
  110. void DrawMap (void)
  111. {
  112. for(int y=0; y < MAP_HEIGHT; y++)
  113. {
  114. gotoxy( 0, y);
  115.  
  116. for(int x=0; x < MAP_WIDTH; x++)
  117. {
  118. DrawTile(x, y);
  119. }
  120. }
  121. }
  122.  
  123. //Door management
  124. void OpenDoorCommand( void )
  125. {
  126. // Notification
  127. gotoxy(2, 18);
  128. SetConsoleTextAttribute(colour, 14);
  129. cout<< "Opening the door...which direction? (w,a,s,d)";
  130. SetConsoleTextAttribute(colour, 15);
  131.  
  132. // Decision
  133. char input;
  134. input = getch();
  135.  
  136. int nDeltaX = 0;
  137. int nDeltaY = 0;
  138.  
  139. switch(input)
  140. {
  141. //NORTH
  142. case 'w':
  143. nDeltaX =0;
  144. nDeltaY= -1;
  145. break;
  146.  
  147. //WEST
  148. case 'a':
  149. nDeltaX = -1;
  150. nDeltaY= 0;
  151. break;
  152.  
  153. //SOUTH
  154. case 's':
  155. nDeltaX = 0;
  156. nDeltaY= 1;
  157. break;
  158.  
  159. //EAST
  160. case 'd':
  161. nDeltaX = 1;
  162. nDeltaY= 0;
  163. break;
  164. //Not a valid direction
  165. default:
  166. return;
  167. }
  168. // Is there a closed door present in the direction specified?
  169. if( nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] == TILE_CLOSEDDOOR)
  170. {
  171. //If there is a closed door, change it to an open one
  172. nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] = TILE_OPENDOOR;
  173.  
  174. }
  175. system("cls");
  176. }
  177. void CloseDoorCommand (void)
  178. {
  179.  
  180. // Notification
  181. gotoxy(2, 18);
  182. SetConsoleTextAttribute(colour, 14);
  183. cout<< "Closing the door...which direction? (w,a,s,d)";
  184. SetConsoleTextAttribute(colour, 15);
  185.  
  186. // Decision
  187. char input;
  188. input = getch();
  189.  
  190. int nDeltaX = 0;
  191. int nDeltaY = 0;
  192.  
  193. switch(input)
  194. {
  195. //NORTH
  196. case 'w':
  197. nDeltaX =0;
  198. nDeltaY= -1;
  199. break;
  200.  
  201. //WEST
  202. case 'a':
  203. nDeltaX = -1;
  204. nDeltaY= 0;
  205. break;
  206.  
  207. //SOUTH
  208. case 's':
  209. nDeltaX = 0;
  210. nDeltaY= 1;
  211. break;
  212.  
  213. //EAST
  214. case 'd':
  215. nDeltaX = 1;
  216. nDeltaY= 0;
  217. break;
  218. //Not a valid direction
  219. default:
  220. return;
  221. }
  222. // Is there a closed door present in the direction specified?
  223. if( nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] == TILE_OPENDOOR)
  224. {
  225. //If there is a closed door, change it to an open one
  226. nMapArray[nPlayerY + nDeltaY][nPlayerX + nDeltaX] = TILE_CLOSEDDOOR;
  227.  
  228. }
  229. system("cls");
  230.  
  231. }
  232.  
  233. ///////////////// Main function ///////////////////////
  234.  
  235. int main()
  236. {
  237. char choose;
  238.  
  239. while(choose='2')
  240. {
  241. menu:
  242. system("cls");
  243. SetConsoleTextAttribute(colour, 15);
  244. cout<<"=========================== Why hello there! ========================" <<endl;
  245. SetConsoleTextAttribute(colour, 14);
  246. cout<<" 1. Let's play the game"<<endl;
  247. cout<<" 2. I'm quitting"<<endl;
  248. SetConsoleTextAttribute(colour, 15);
  249. cout<<"=====================================================================" <<endl;
  250. cout<<endl;
  251. SetConsoleTextAttribute(colour, 14);
  252. cout<<" pPPPPPPpp rRRRRRRrr cCCCCCc Ll "<<endl;
  253. cout<<" PP Pp RR Rr cCc cCc LL "<<endl;
  254. cout<<" PPp Pp RRr Rr cCc LL "<<endl;
  255. cout<<" PPPPPPPPP RRRRRRRRR Cc LL "<<endl;
  256. cout<<" PPp RRRr Cc LL "<<endl;
  257. cout<<" PP RR RRr Cc LL "<<endl;
  258. cout<<" PP RR RRr cCc LL "<<endl;
  259. cout<<" PP RR RRr cCc cCc LLl "<<endl;
  260. cout<<" PP RR RRr cCCCCCc LLLLLLLLLLL "<<endl;
  261. SetConsoleTextAttribute(colour, 15);
  262. cout<<endl;
  263. cout<<"====================================================================="<<endl;
  264. choose = getch();
  265.  
  266. switch(choose)
  267. {
  268. case '1':
  269. {
  270. //Main game loop
  271. while (true)
  272. {
  273. //Map drawing
  274. system("cls");
  275. DrawMap();
  276.  
  277. //Sleep
  278. Sleep(100);
  279.  
  280. //Draw the player to the screen
  281. gotoxy( nPlayerX, nPlayerY);
  282. SetConsoleTextAttribute(colour, 14);
  283. cout<<"@"<<"("<<nPlayerX<<","<<nPlayerY<<")";
  284. SetConsoleTextAttribute(colour, 15);
  285.  
  286. //Input
  287. char input = getch();
  288.  
  289. //Processing
  290.  
  291. int nDeltaX;
  292. int nDeltaY;
  293.  
  294. switch(input)
  295. {
  296. //Move up
  297. case 'w':
  298. nDeltaX = 0;
  299. nDeltaY = -1;
  300. break;
  301.  
  302. //Move left
  303. case 'a':
  304. nDeltaX = -1;
  305. nDeltaY = 0;
  306. break;
  307.  
  308. //Mode down
  309. case 's':
  310. nDeltaX = 0;
  311. nDeltaY = 1;
  312. break;
  313.  
  314. //Move right
  315. case 'd':
  316. nDeltaX = 1;
  317. nDeltaY = 0;
  318. break;
  319.  
  320. //Open door
  321. case 'o':
  322. OpenDoorCommand();
  323. break;
  324.  
  325. //Close door
  326. case 'c':
  327. CloseDoorCommand();
  328. break;
  329.  
  330. //Main Menu
  331. case '0':
  332. goto menu;
  333.  
  334. //Ignore
  335. default:
  336. break;
  337. }
  338. //Check and see if we're allowed to move in the specified direction
  339. if(isPassable(nPlayerX + nDeltaX, nPlayerY + nDeltaY))
  340. { //if allowed, move in the direction specified
  341. nPlayerX += nDeltaX;
  342. nPlayerY += nDeltaY;
  343. }
  344. }
  345. system("pause");
  346. }
  347.  
  348. break;
  349. //exit
  350. case '2':
  351. {
  352. cerr<<endl<<"That's not very nice of you."<<endl;
  353. cout<<endl;
  354. system("pause");
  355. exit(0);
  356. }
  357. break;
  358. //error
  359. default:
  360. {
  361. cerr<<endl<<"There is no such option. Please try again, it's not that complicated."<<endl;
  362. cout<<endl;
  363. system("pause");
  364. cout<<endl;
  365. }
  366. break;
  367.  
  368. }
  369. }
  370. return 0;
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement