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