fork download
  1. /////////////////////////////////////
  2. // Simple ASCII Console World
  3. // Author: Muhammad Ahmad Tirmazi
  4. // Date: 12 July 2012
  5. // License: BSD 4-Clause
  6. /////////////////////////////////////
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include <vector>
  11.  
  12. #include <cstdlib>
  13. #include <cstdio>
  14.  
  15. #define _WIN32_WINNT 0x0500
  16. #include <windows.h>
  17.  
  18. /* Standard error macro for reporting API errors */
  19. #define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s \
  20.   on line %d\n", __FILE__, GetLastError(), api, __LINE__);}
  21.  
  22. void cls( HANDLE hConsole )
  23. {
  24. COORD coordScreen = { 0, 0 }; /* here's where we'll home the
  25.   cursor */
  26. BOOL bSuccess;
  27. DWORD cCharsWritten;
  28. CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
  29. DWORD dwConSize; /* number of character cells in
  30.   the current buffer */
  31.  
  32. /* get the number of character cells in the current buffer */
  33.  
  34. bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
  35. PERR( bSuccess, "GetConsoleScreenBufferInfo" );
  36. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  37.  
  38. /* fill the entire screen with blanks */
  39.  
  40. bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
  41. dwConSize, coordScreen, &cCharsWritten );
  42. PERR( bSuccess, "FillConsoleOutputCharacter" );
  43.  
  44. /* get the current text attribute */
  45.  
  46. bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
  47. PERR( bSuccess, "ConsoleScreenBufferInfo" );
  48.  
  49. /* now set the buffer's attributes accordingly */
  50.  
  51. bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
  52. dwConSize, coordScreen, &cCharsWritten );
  53. PERR( bSuccess, "FillConsoleOutputAttribute" );
  54.  
  55. /* put the cursor at (0, 0) */
  56.  
  57. bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
  58. PERR( bSuccess, "SetConsoleCursorPosition" );
  59. return;
  60. }
  61.  
  62. // Our Custom Namespace
  63. namespace pro {
  64.  
  65. ///////////////////////////////
  66. // Just a typedef for 'char'
  67. ///////////////////////////////
  68. typedef char GameTile;
  69.  
  70. ///////////////////////////////
  71. // Simple Point Class
  72. // Holds the X and Y values
  73. // of a 2D Object
  74. //////////////////////////////
  75. template< typename T > struct Point
  76. {
  77. Point(void) : X(0), Y(0) {}
  78. Point(T x, T y) : X(x), Y(y) {}
  79. T X;
  80. T Y;
  81. };
  82.  
  83. /////////////////////////////
  84. // Simple GamePlayer Class
  85. // For controlling the
  86. // Main Character
  87. /////////////////////////////
  88. struct GamePlayer
  89. {
  90. Point< int > Position;
  91. GameTile Tile;
  92.  
  93. GamePlayer(std::vector< std::string >& map);
  94.  
  95. void MoveLeft(std::vector< std::string >& map);
  96. void MoveRight(std::vector< std::string >& map);
  97. void MoveUp(std::vector< std::string >& map);
  98. void MoveDown(std::vector< std::string >& map);
  99.  
  100. void Draw(void);
  101. };
  102.  
  103. //////////////////////////////////////////
  104. // Constructor
  105. // Sets the Player's original position
  106. // 1 is the code for a happy face
  107. /////////////////////////////////////////
  108. GamePlayer::GamePlayer(std::vector< std::string >& map)
  109. : Position(2, 2), Tile(1)
  110. {
  111. if (map[Position.X][Position.Y] == ' ')
  112. {
  113. map[Position.X][Position.Y] = Tile;
  114. }
  115. else
  116. {
  117. std::cerr << "Player's Tile is Occupied" << std::endl;
  118. }
  119. }
  120.  
  121. //////////////////////////////////////////
  122. // Moves the Player 1 Tile To the Left
  123. /////////////////////////////////////////
  124. void GamePlayer::MoveLeft(std::vector< std::string >& map)
  125. {
  126. // If the tile is empty
  127. if (map[Position.Y][Position.X - 1] == ' ')
  128. {
  129. // Remove our player's tile from it's original position
  130. map[Position.Y][Position.X] = ' ';
  131.  
  132. Position.X --;
  133. // Fill it with our player's tile
  134. map[Position.Y][Position.X] = Tile;
  135. }
  136. }
  137.  
  138. //////////////////////////////////////////
  139. // Moves the Player 1 Tile To the Right
  140. /////////////////////////////////////////
  141. void GamePlayer::MoveRight(std::vector< std::string >& map)
  142. {
  143. // If the tile is empty
  144. if (map[Position.Y][Position.X + 1] == ' ')
  145. {
  146. // Remove our player's tile from it's original position
  147. map[Position.Y][Position.X] = ' ';
  148.  
  149. Position.X ++;
  150. // Fill it with our player's tile
  151. map[Position.Y][Position.X] = Tile;
  152. }
  153. }
  154.  
  155. //////////////////////////////////////////
  156. // Moves the Player 1 Tile Upwards
  157. /////////////////////////////////////////
  158. void GamePlayer::MoveUp(std::vector< std::string >& map)
  159. {
  160. // If the tile is empty
  161. if (map[Position.Y - 1][Position.X] == ' ')
  162. {
  163. // Remove our player's tile from it's original position
  164. map[Position.Y][Position.X] = ' ';
  165.  
  166. Position.Y --;
  167. // Fill it with our player's tile
  168. map[Position.Y][Position.X] = Tile;
  169. }
  170. }
  171.  
  172. //////////////////////////////////////////
  173. // Moves the Player 1 Tile Downwards
  174. /////////////////////////////////////////
  175. void GamePlayer::MoveDown(std::vector< std::string >& map)
  176. {
  177. // If the tile is empty
  178. if (map[Position.Y + 1][Position.X] == ' ')
  179. {
  180. // Remove our player's tile from it's original position
  181. map[Position.Y][Position.X] = ' ';
  182.  
  183. Position.Y ++;
  184. // Fill it with our player's tile
  185. map[Position.Y][Position.X] = Tile;
  186. }
  187. }
  188.  
  189. } // namespace pro
  190.  
  191. ////////////////////////////////
  192. // A Character Representation
  193. // Of Our Tile Map
  194. ///////////////////////////////
  195. static std::string tileMap[] = {
  196.  
  197. "==========",
  198. "= =",
  199. "= =",
  200. "= =",
  201. "= =",
  202. "= =",
  203. "= =",
  204. "=========="
  205. };
  206.  
  207. /** Not recommended, but works for simple stuff... **/
  208.  
  209. ///////////////////////////////////////////////////
  210. // Calculates the vertical length of the tile map
  211. //////////////////////////////////////////////////
  212. static int mapLenght = sizeof(tileMap) / sizeof(tileMap[0]);
  213.  
  214. ///////////////////////////////////////////////////
  215. // Calculates the horizontal width of the tile map
  216. ///////////////////////////////////////////////////
  217. static int mapWidth = tileMap[0].size();
  218.  
  219. ///////////////////////////////////////////////////////////////
  220. // Vector, to prevent us from dealing with the lousy pointer
  221. // arithmetic that comes with arrays
  222. ///////////////////////////////////////////////////////////////
  223. static std::vector< std::string > tileVector(tileMap, tileMap + mapLenght);
  224.  
  225. //////////////
  226. // The player
  227. //////////////
  228. static pro::GamePlayer player(tileVector);
  229.  
  230. ////////////////////////
  231. // Draws the Tile Map
  232. ///////////////////////
  233. void Draw(void)
  234. {
  235. cls(::GetStdHandle(STD_OUTPUT_HANDLE));
  236.  
  237. for (int y = 0; y < mapLenght; y++)
  238. {
  239. for (int x = 0; x < mapWidth; x++)
  240. {
  241. std::cout << tileVector[y][x];
  242. }
  243.  
  244. std::cout << "\n";
  245. }
  246. }
  247.  
  248. //////////////////////////////////////
  249. // Gets and Responds to user input
  250. // from the keyboard
  251. /////////////////////////////////////
  252. bool GetInput(void)
  253. {
  254. if (GetAsyncKeyState(VK_UP))
  255. {
  256. player.MoveUp(tileVector);
  257. }
  258. else if (GetAsyncKeyState(VK_DOWN))
  259. {
  260. player.MoveDown(tileVector);
  261. }
  262. else if (GetAsyncKeyState(VK_LEFT))
  263. {
  264. player.MoveLeft(tileVector);
  265. }
  266. else if (GetAsyncKeyState(VK_RIGHT))
  267. {
  268. player.MoveRight(tileVector);
  269. }
  270. else
  271. {
  272. return false;
  273. }
  274. return true;
  275. }
  276.  
  277. //////////////////////////////////////////
  278. // Exit when the player presses 'Escape'
  279. /////////////////////////////////////////
  280. bool CheckExit()
  281. {
  282. if (GetAsyncKeyState(VK_ESCAPE))
  283. {
  284. return true;
  285. }
  286.  
  287. return false;
  288. }
  289.  
  290. //////////////////////////////////
  291. // The main entrypoint function
  292. /////////////////////////////////
  293. int main()
  294. {
  295. bool open = true;
  296.  
  297. Draw();
  298.  
  299. /////////////////////////
  300. // The Main Game Loop
  301. ////////////////////////
  302. while (open)
  303. {
  304. ////////////////////////////////////////////
  305. // Redraw if the player's position changes
  306. ////////////////////////////////////////////
  307. if (GetInput()) Draw();
  308.  
  309. if (CheckExit()) open = false;
  310. }
  311.  
  312. return 0;
  313. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty