Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <conio.h>
  2. #include <windows.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8. bool gameover;
  9. // Buffer size
  10. const char WIDTH = 120,
  11. HEIGHT = 30,
  12.  
  13. //Dirrections:
  14. STOP = 0,
  15. UP = 1,
  16. RIGHT = 2,
  17. DOWN = 3,
  18. LEFT = 4,
  19.  
  20. //Other settings
  21. MAX_TAIL_LENGTH = 100;
  22.  
  23. void sleep(int time)
  24. {
  25. for (int i = 0; i < time * 10000; ++i)
  26. {
  27. cos(i);
  28. }
  29.  
  30. }
  31.  
  32. typedef struct
  33. {
  34. char x,
  35. y;
  36. }Point;
  37.  
  38. inline void printBorder(char* screenBuffer)
  39. {
  40. int score = 0;
  41.  
  42. for (int i = 0; i < 61; ++i)
  43. {
  44. screenBuffer[i] = 178;
  45.  
  46. }
  47. for (int i = 0; i < HEIGHT * WIDTH; i += 120)
  48. {
  49. screenBuffer[i + 60] = screenBuffer[i] = 178;
  50.  
  51. }
  52. for (int i = (HEIGHT * WIDTH)-120; i <( HEIGHT * WIDTH)-60; i++)
  53. {
  54. screenBuffer[i] =178;
  55. }
  56.  
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62. char* screenBuffer = new char[HEIGHT * WIDTH];
  63.  
  64. Point tail[MAX_TAIL_LENGTH];
  65.  
  66. unsigned short tailLength = 0;
  67.  
  68. char xPos = 20,
  69. yPos = 15,
  70. xFruitPos = rand() % 59,
  71. yFruitPos = rand() % 29;
  72.  
  73. int score = 0;
  74.  
  75. DWORD dword = 0;
  76. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  77.  
  78. char dirrection = STOP;
  79.  
  80. CONSOLE_CURSOR_INFO cursorInfo;
  81. GetConsoleCursorInfo(out, &cursorInfo);
  82. cursorInfo.bVisible = false;
  83. SetConsoleCursorInfo(out, &cursorInfo);
  84.  
  85. while (!gameover)
  86. {
  87. for (int i = 0; i < HEIGHT * WIDTH; ++i)
  88. {
  89. screenBuffer[i] = '\0';
  90. }
  91. screenBuffer[yFruitPos * 120 + xFruitPos] = 'O';
  92. screenBuffer[yPos * 120 + xPos] = 'S';
  93. //cout << "Score:" << score << endl;
  94. printBorder(screenBuffer);
  95.  
  96.  
  97. for (int i = 0; i < tailLength; ++i)
  98. {
  99. screenBuffer[tail[i].y * 120 + tail[i].x] = 's';
  100. }
  101. WriteConsoleOutputCharacterA(out, screenBuffer, WIDTH * HEIGHT, { 0,0 }, &dword);
  102.  
  103. if (yFruitPos == yPos && xFruitPos == xPos)
  104. {
  105. xFruitPos = rand() % 59,
  106. yFruitPos = rand() % 29;
  107. score +=5;
  108. tail[tailLength].x = xPos;
  109. tail[tailLength].y = yPos;
  110. ++tailLength;
  111.  
  112. }
  113. else
  114. {
  115. for (int i = 0; i < tailLength - 1; ++i)
  116. {
  117. tail[i] = tail[i + 1];
  118. }
  119. tail[tailLength - 1].x = xPos;
  120. tail[tailLength - 1].y = yPos;
  121. }
  122.  
  123. if (_kbhit())
  124. {
  125. char key = _getch();
  126. switch (key)
  127. {
  128. case 97:
  129. dirrection = LEFT;
  130. break;
  131. case 100:
  132. dirrection = RIGHT;
  133. break;
  134. case 115:
  135. dirrection = DOWN;
  136. break;
  137. case 119:
  138. dirrection = UP;
  139. break;
  140. }
  141. }
  142.  
  143. switch (dirrection)
  144. {
  145.  
  146. case LEFT:
  147. if (xPos > 0) --xPos;
  148. break;
  149. case RIGHT:
  150. if (xPos < 60) ++xPos;
  151. break;
  152. case DOWN:
  153. if (yPos < 29) ++yPos;
  154. break;
  155. case UP:
  156. if (yPos > 0) --yPos;
  157. break;
  158.  
  159. }
  160. if (xPos < 1)
  161. {
  162. xPos = 58;
  163. }
  164. else if (xPos > 59)
  165. {
  166. xPos = 1;
  167. }
  168.  
  169. if (yPos < 1)
  170. {
  171. yPos = 27;
  172. }
  173. else if (yPos >28)
  174. {
  175. yPos = 1;
  176. }
  177.  
  178. for (int i = 0; i < tailLength - 1; ++i)
  179. {
  180. if (tail[tailLength].x == xPos && tail[tailLength].y == yPos)
  181. gameover = true;
  182. }
  183.  
  184. sleep(120);
  185. }
  186.  
  187. getchar();
  188. return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement