Advertisement
Guest User

Untitled

a guest
Sep 4th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 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. HANDLE colour = GetStdHandle(STD_OUTPUT_HANDLE);
  9.  
  10. //cursor position
  11. void gotoxy(int x, int y)
  12. {
  13. COORD c;
  14. c.X=x-1;
  15. c.Y=y-1;
  16. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
  17. }
  18.  
  19. //Map dimension
  20. #define MAP_WIDTH 20
  21. #define MAP_HEIGHT 15
  22.  
  23. //Tile Types
  24. #define TILE_FLOOR 0
  25. #define TILE_WALL 1
  26.  
  27. // Map declaration
  28. int nMapArray[MAP_HEIGHT][MAP_WIDTH] =
  29. {
  30. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  31. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  32. { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
  33. { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  34. { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  35. { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  36. { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  37. { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
  38. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  39. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  40. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  41. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  42. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  43. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  44. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  45. };
  46. //Draw map
  47. void DrawMap (void)
  48. {
  49. for(int y=0; y < MAP_HEIGHT; y++)
  50. {
  51. gotoxy( 0, y);
  52.  
  53. for(int x=0; x < MAP_WIDTH; x++)
  54. {
  55. //Draw the tile
  56. switch (nMapArray[y][x])
  57. {
  58. case TILE_FLOOR:
  59. {
  60. SetConsoleTextAttribute(colour, 7);
  61. cout<<".";
  62. SetConsoleTextAttribute(colour, 15);
  63. break;
  64. }
  65. case TILE_WALL:
  66. {
  67. SetConsoleTextAttribute(colour, 8);
  68. cout<<"#";
  69. SetConsoleTextAttribute(colour, 15);
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. }
  76.  
  77. int main()
  78. {
  79. char choose;
  80.  
  81. while(choose='2')
  82. {
  83. menu:
  84. system("cls");
  85. SetConsoleTextAttribute(colour, 15);
  86. cout<<"=========================== Why hello there! ========================" <<endl;
  87. SetConsoleTextAttribute(colour, 14);
  88. cout<<" 1. Let's play the game"<<endl;
  89. cout<<" 2. I'm quitting"<<endl;
  90. SetConsoleTextAttribute(colour, 15);
  91. cout<<"=====================================================================" <<endl;
  92. choose = getch();
  93.  
  94. switch(choose)
  95. {
  96. case '1':
  97. {
  98. //Declare the player's position
  99. int nPlayerX=4, nPlayerY=4;
  100.  
  101. //Main game loop
  102. while (true)
  103. {
  104. //Map drawing
  105. system("cls");
  106. DrawMap();
  107.  
  108. //Draw the player to the screen
  109. gotoxy( nPlayerX, nPlayerY);
  110. SetConsoleTextAttribute(colour, 14);
  111. cout<<"@"<<"("<<nPlayerX<<","<<nPlayerY<<")";
  112. SetConsoleTextAttribute(colour, 15);
  113.  
  114. //Input
  115. char input = getch();
  116.  
  117. //Processing
  118.  
  119. switch(input)
  120. {
  121. //Move up
  122. case 'w':
  123. if(nPlayerY<=1)
  124. break;
  125. else nPlayerY--;
  126. break;
  127.  
  128. //Move left
  129. case 'a':
  130. if(nPlayerX<=1)
  131. break;
  132. else nPlayerX--;
  133. break;
  134.  
  135. //Mode down
  136. case 's':
  137. if(nPlayerY>=14)
  138. break;
  139. else nPlayerY++;
  140. break;
  141.  
  142. //Move right
  143. case 'd':
  144. if(nPlayerX>=29)
  145. break;
  146. else nPlayerX++;
  147. break;
  148.  
  149. //Main Menu
  150. case '0':
  151. goto menu;
  152.  
  153. //Ignore
  154. default:
  155. break;
  156. }
  157. }
  158. system("pause");
  159. }
  160. break;
  161. //exit
  162. case '2':
  163.  
  164. cerr<<endl<<"That's not very nice of you."<<endl;
  165. cout<<endl;
  166. system("pause");
  167. exit(0);
  168.  
  169. break;
  170. //error
  171. default:
  172. cerr<<endl<<"There is no such option. Please try again, it's not that complicated."<<endl;
  173. cout<<endl;
  174. system("pause");
  175. cout<<endl;
  176. break;
  177.  
  178. }
  179. }
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement