Advertisement
Guest User

oi <3

a guest
Aug 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. /*
  2. CODE FOR THE SNAKE GAME IN THE CONSOLE WINDOW.
  3. WILL NOT WORK ON SOLOLEARN.
  4. COPY AND PASTE TO YOUR EDITOR (I USE VISUAL STUDIO).
  5. HAVE FUN! ;)
  6. */
  7.  
  8.  
  9. //#include "stdafx.h"
  10. #include <iostream>
  11. #include <conio.h>
  12. #include <string>
  13. #include <Windows.h>
  14. using namespace std;
  15.  
  16. bool gameover;
  17. bool shutDown;
  18. const int width = 20;
  19. const int height = 20;
  20. int x, y, fruitX, fruitY, score;
  21. int tailX[400], tailY[400], nTail;
  22. enum eDirection {STOP, LEFT, RIGHT, UP, DOWN};
  23. eDirection dir;
  24.  
  25. void HideCursor()
  26. {
  27. CONSOLE_CURSOR_INFO cursor = {1, FALSE};
  28. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
  29. }
  30. // cursor
  31.  
  32. void StartScreen()
  33. {
  34. system("cls");
  35. cout << "\nWELCOME TO SNAKE CONSOLE!"
  36. << "\n\nHello, press 's' to Start!"
  37. << "\n\nPress 'q' to Quit"
  38. << "\n\nPress 'i' for Instructions."
  39. << "\n\nYour Previous Score: " << score << endl;
  40. }
  41. void Instructions()
  42. {
  43. HideCursor();
  44. system("cls");
  45. cout << "\nUse the 'w' key to go UP."
  46. << "\n\nUse the 'a' key to turn LEFT."
  47. << "\n\nUse the 's' key to go DOWN."
  48. << "\n\nUse the 'd' key to turn RIGHT."
  49. << "\n\nPress the 'x' key any time during the game to EXIT."
  50. << "\n\nDO NOT touch the borders or you will LOSE."
  51. << "\nDO NOT reverse movement or you will LOSE."
  52. << "\nDO NOT eat your own tail or you will obviously also LOSE!"
  53. << "\n\n\nNow press the 'm' key to go MENU.\n";
  54. string input;
  55. cin >> input;
  56. if(input=="m")
  57. StartScreen();
  58. }
  59. void FruitSpawn()
  60. {
  61. fruitX = rand() % width;
  62. fruitY = rand() % height;
  63. }
  64. void Setup()
  65. {
  66. gameover = false;
  67. shutDown = false;
  68. dir = STOP;
  69. x = width / 2;
  70. y = height / 2;
  71. FruitSpawn();
  72. score = 0;
  73. nTail = 0;
  74. }
  75. void Draw()
  76. {
  77. HideCursor();
  78. COORD coord;
  79. coord.X = 0;
  80. coord.Y = 0;
  81. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  82. for (int i = 0; i < width + 2; i++) {
  83. cout << "#";}
  84. cout << endl;
  85.  
  86. for (int i = 0; i < height; i++)
  87. {
  88. for (int j = 0; j < width; j++)
  89. {
  90. if (j == 0)
  91. cout << "#";
  92.  
  93. if (i == y && j == x)
  94. cout << "O";
  95.  
  96. else if (i == fruitY && j == fruitX)
  97. cout << "F";
  98.  
  99. else {
  100. bool print = false;
  101. for (int k = 0; k < nTail; k++) {
  102. if (tailX[k] == j && tailY[k] == i) {
  103. cout << "o";
  104. print = true;
  105. }
  106. }
  107. if(!print)
  108. cout << " ";
  109. }
  110.  
  111. if (j == width - 1)
  112. cout << "#";
  113. }
  114. cout << endl;
  115. }
  116. for (int i = 0; i < width + 2; i++) {
  117. cout << "#";
  118. }
  119. cout << endl;
  120. cout << "Score: " << score << endl;
  121. }
  122. void Input()
  123. {
  124. if (_kbhit()) {
  125. switch (_getch())
  126. {
  127. case 'a':
  128. dir = LEFT;
  129. break;
  130. case 'w':
  131. dir = UP;
  132. break;
  133. case 's':
  134. dir = DOWN;
  135. break;
  136. case 'd':
  137. dir = RIGHT;
  138. break;
  139. case 'x':
  140. gameover = true;
  141. break;
  142. }
  143. }
  144. }
  145. void Logic()
  146. {
  147. int prevX = tailX[0];
  148. int prevY = tailY[0];
  149. int prev2X, prev2Y;
  150. tailX[0] = x;
  151. tailY[0] = y;
  152.  
  153. for (int i = 1; i < nTail; i++) {
  154. prev2X = tailX[i];
  155. prev2Y = tailY[i];
  156. tailX[i] = prevX;
  157. tailY[i] = prevY;
  158. prevX = prev2X;
  159. prevY = prev2Y;
  160.  
  161. }
  162.  
  163. switch (dir)
  164. {
  165. case LEFT:
  166. x--;
  167. break;
  168. case RIGHT:
  169. x++;
  170. break;
  171. case UP:
  172. y--;
  173. break;
  174. case DOWN:
  175. y++;
  176. break;
  177. }
  178. if (x > width || x < 0 || y > height || y < 0) {
  179. gameover = true;
  180. StartScreen();
  181. }
  182.  
  183. for (int i = 0; i < nTail; i++) {
  184. if (tailX[i] == x && tailY[i] == y)
  185. gameover = true;
  186. }
  187.  
  188. if (x == fruitX && y == fruitY) {
  189. score++;
  190. nTail++;
  191. FruitSpawn();
  192. }
  193. }
  194. int main()
  195. {
  196. HideCursor();
  197. do {
  198. StartScreen();
  199. string input;
  200. cin >> input;
  201. if (input == "s")
  202. {
  203. Setup();
  204. system("cls");
  205. while (!gameover)
  206. {
  207. Draw();
  208. Input();
  209. Logic();
  210. Sleep(100);
  211. }
  212. }
  213. else if (input == "i")
  214. Instructions();
  215. else if (input == "q")
  216. shutDown = true;
  217. } while (shutDown == false);
  218.  
  219. return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement