Advertisement
Felanpro

setCursorPosition() with spawnEnemy()

Mar 3rd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <Windows.h>
  5. #include <conio.h> //Gives us infamous getch()
  6. #include <time.h>
  7.  
  8. using namespace std;
  9.  
  10. char sheet[10][10];
  11. int ending = 0;
  12.  
  13. void cls()
  14. {
  15. // Get the Win32 handle representing standard output.
  16. // This generally only has to be done once, so we make it static.
  17. static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  18.  
  19. CONSOLE_SCREEN_BUFFER_INFO csbi;
  20. COORD topLeft = { 0, 0 };
  21.  
  22. // std::cout uses a buffer to batch writes to the underlying console.
  23. // We need to flush that to the console because we're circumventing
  24. // std::cout entirely; after we clear the console, we don't want
  25. // stale buffered text to randomly be written out.
  26. std::cout.flush();
  27.  
  28. // Figure out the current width and height of the console window
  29. if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
  30. // TODO: Handle failure!
  31. abort();
  32. }
  33. DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
  34.  
  35. DWORD written;
  36.  
  37. // Flood-fill the console with spaces to clear it
  38. FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
  39.  
  40. // Reset the attributes of every character to the default.
  41. // This clears all background colour formatting, if any.
  42. FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
  43.  
  44. // Move the cursor back to the top left for the next sequence of writes
  45. SetConsoleCursorPosition(hOut, topLeft);
  46. }
  47.  
  48. void setCursorPosition(int x, int y)
  49. {
  50. static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  51. std::cout.flush();
  52. COORD coord = { (SHORT)x, (SHORT)y };
  53. SetConsoleCursorPosition(hOut, coord);
  54. }
  55.  
  56. void initialize()
  57. {
  58. for(int x = 0; x <10; x++)
  59. {
  60. for(int y = 0; y < 10; y++)
  61. {
  62. sheet[x][y] = '*';
  63. }
  64. }
  65.  
  66. sheet[5][5] = 'X'; //This is where the player starts
  67. }
  68.  
  69. void draw()
  70. {
  71. for(int xx = 0; xx < 10; xx++)
  72. {
  73. for(int yy = 0; yy < 10; yy++)
  74. {
  75. cout << sheet[xx][yy];
  76. }
  77. cout << endl;
  78. }
  79. }
  80.  
  81. void input_and_update()
  82. {
  83.  
  84. //Get row and column (position) of player
  85.  
  86. int c;
  87. int p;
  88. int breaking = 0;
  89. for(c = 0; c < 10; c++)
  90. {
  91. for(p = 0; p < 10; p++)
  92. {
  93. if(sheet[c][p] == 'X')
  94. {
  95. breaking = 1;
  96. break;
  97. }
  98. }
  99. if(breaking == 1)
  100. break;
  101. }
  102.  
  103. breaking = 0;
  104.  
  105. char variable;
  106.  
  107. //77 = right arrow
  108. //72 = up arrow
  109. //80 down arrow
  110. //75 = left arrow
  111. // c = row, p = column
  112. while(breaking == 0)
  113. {
  114. variable = getch();
  115.  
  116. if(variable == 75)
  117. {
  118. if(p - 1 < 0)
  119. {
  120. ;
  121. }
  122. else if(sheet[c][p - 1] == 'O')
  123. {
  124. ending = 1;
  125. breaking = 1;
  126. }
  127. else
  128. {
  129. sheet[c][p] = '*';
  130. setCursorPosition(p, c);
  131. cout << "*";
  132. sheet[c][p - 1] = 'X';
  133. setCursorPosition((p - 1), c);
  134. cout << "X";
  135. setCursorPosition(0, 11);
  136. breaking = 1;
  137. }
  138. }
  139. else if(variable == 72)
  140. {
  141. if(c - 1 < 0)
  142. {
  143. ;
  144. }
  145. else if(sheet[c - 1][p] == 'O')
  146. {
  147. ending = 1;
  148. breaking = 1;
  149. }
  150. else
  151. {
  152. sheet[c][p] = '*';
  153. setCursorPosition(p, c);
  154. cout << "*";
  155. sheet[c - 1][p] = 'X';
  156. setCursorPosition(p, c - 1);
  157. cout << "X";
  158. setCursorPosition(0, 11);
  159. breaking = 1;
  160. }
  161. }
  162. else if(variable == 77)
  163. {
  164. if(p + 1 > 9)
  165. {
  166. ;
  167. }
  168. else if(sheet[c][p + 1] == 'O')
  169. {
  170. ending = 1;
  171. breaking = 1;
  172. }
  173. else
  174. {
  175. sheet[c][p] = '*';
  176. setCursorPosition(p, c);
  177. cout << "*";
  178. sheet[c][p + 1] = 'X';
  179. setCursorPosition((p + 1), c);
  180. cout << "X";
  181. setCursorPosition(0, 11);
  182. breaking = 1;
  183. }
  184. }
  185. else if(variable == 80)
  186. {
  187. if(c + 1 > 9)
  188. {
  189. ;
  190. }
  191. else if(sheet[c + 1][p] == 'O')
  192. {
  193. ending = 1;
  194. breaking = 1;
  195. }
  196. else
  197. {
  198. sheet[c][p] = '*';
  199. setCursorPosition(p, c);
  200. cout << "*";
  201. sheet[c + 1][p] = 'X';
  202. setCursorPosition(p, c + 1);
  203. cout << "X";
  204. setCursorPosition(0, 11);
  205. breaking = 1;
  206. }
  207. }
  208. }
  209. }
  210.  
  211. void spawnEnemy()
  212. {
  213. int break_loop = 0;
  214.  
  215. while(break_loop == 0)
  216. {
  217. srand(time(NULL));
  218. int random_number_one = rand() % 10;
  219. int random_number_two = rand() % 10;
  220.  
  221. if(random_number_one == 5 && random_number_two == 5)
  222. {
  223. ;
  224. }
  225. else
  226. {
  227. sheet[random_number_one][random_number_two] = 'O';
  228. setCursorPosition(random_number_two, random_number_one);
  229. cout << "O";
  230. setCursorPosition(0, 11);
  231. break_loop = 1;
  232. }
  233. }
  234. }
  235.  
  236. int main()
  237. {
  238. initialize(); // Create play sheet
  239. draw();
  240. while(ending == 0)
  241. {
  242. spawnEnemy();
  243. input_and_update();
  244. }
  245. setCursorPosition(0, 0);
  246. cls();
  247.  
  248. cout << "You died!" << endl;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement