Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #include "pch.h"
  2. #include "snake.h"
  3.  
  4. //#define DEBUG
  5.  
  6. //////////////////////////////////////DEFAULT SETTINGS, STRUCTURE
  7. #define MAPSIZE 50
  8.  
  9. using namespace std;
  10.  
  11. bool Game = TRUE;
  12.  
  13. const int Coord_X = MAPSIZE;
  14. const int Coord_Y = Coord_X / 2;
  15.  
  16. int CharCoord_X = 0;
  17. int CharCoord_Y = 0;
  18.  
  19. int XS1, XS2, YS1, YS2;
  20.  
  21. int Score = 0;
  22.  
  23. #ifdef DEBUG
  24. int *BonusCoord_X = new int();
  25. int *BonusCoord_Y = new int();
  26. #else
  27. int BonusCoord_X = 0;
  28. int BonusCoord_Y = 0;
  29. #endif
  30.  
  31. bool TOP, BOTTOM, LEFT, RIGHT;
  32.  
  33. //////////////////////////////////////CONSTRUCTOR
  34. Snake::Snake()
  35. {
  36. srand(time(0));
  37. }
  38.  
  39. //////////////////////////////////////DESTRUCTOR
  40. Snake::~Snake()
  41. {
  42. #ifdef DEBUG
  43. delete BonusCoord_X, BonusCoord_Y;
  44. BonusCoord_X = NULL;
  45. BonusCoord_Y = NULL;
  46. #endif
  47. }
  48.  
  49. //////////////////////////////////////Random()
  50. void Snake::Random()
  51. {
  52. BonusCoord_X = rand() % (Coord_X - 10) + 5;
  53. BonusCoord_Y = rand() % (Coord_Y - 10) + 5;
  54. }
  55.  
  56. //////////////////////////////////////GameOver()
  57. void Snake::GameOver()
  58. {
  59. Game = FALSE;
  60. cout << "#############[GAME OVER]#############" << endl << "[Enter - New Game]" << endl << "SCORE: " << Score << endl;
  61. cin.get();
  62. TOP = FALSE;
  63. BOTTOM = FALSE;
  64. LEFT = FALSE;
  65. RIGHT = FALSE;
  66. CharCoord_X = Coord_X / 2;
  67. CharCoord_Y = Coord_Y / 2;
  68. #ifdef DEBUG
  69. *BonusCoord_X = rand() % (Coord_X - 2) + 2;
  70. *BonusCoord_Y = rand() % (Coord_Y - 2) + 2;
  71. #else
  72. Random();
  73. #endif
  74. Score = 0;
  75. Game = TRUE;
  76. }
  77.  
  78. //////////////////////////////////////Keyboard()
  79. void Snake::Keyboard()
  80. {
  81. while (_kbhit())
  82. {
  83. char arrows = _getch();
  84. switch (arrows)
  85. {
  86. case 'w':
  87. if (!(BOTTOM))
  88. {
  89. TOP = TRUE;
  90. BOTTOM = FALSE;
  91. LEFT = FALSE;
  92. RIGHT = FALSE;
  93. }
  94. break;
  95. case 's':
  96. if (!(TOP))
  97. {
  98. TOP = FALSE;
  99. BOTTOM = TRUE;
  100. LEFT = FALSE;
  101. RIGHT = FALSE;
  102. }
  103. break;
  104. case 'a':
  105. if (!(RIGHT))
  106. {
  107. TOP = FALSE;
  108. BOTTOM = FALSE;
  109. LEFT = TRUE;
  110. RIGHT = FALSE;
  111. }
  112. break;
  113. case 'd':
  114. if (!(LEFT))
  115. {
  116. TOP = FALSE;
  117. BOTTOM = FALSE;
  118. LEFT = FALSE;
  119. RIGHT = TRUE;
  120. }
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. if (TOP)
  127. if ((CharCoord_Y > 1))
  128. CharCoord_Y -= 1;
  129. else
  130. GameOver();
  131.  
  132. if (BOTTOM)
  133. if ((CharCoord_Y < Coord_Y - 2))
  134. CharCoord_Y += 1;
  135. else
  136. GameOver();
  137.  
  138. if (LEFT)
  139. if ((CharCoord_X > 1))
  140. CharCoord_X -= 1;
  141. else
  142. GameOver();
  143.  
  144. if (RIGHT)
  145. if ((CharCoord_X < Coord_X - 2))
  146. CharCoord_X += 1;
  147. else
  148. GameOver();
  149. }
  150.  
  151. //////////////////////////////////////CoordStore()
  152. void Snake::CoordStore(int opt, int x, int y)
  153. {
  154. switch (opt)
  155. {
  156. case 0:
  157. {
  158. XS1 = x;
  159. YS1 = y;
  160. }
  161. break;
  162. case 1:
  163. {
  164. XS2 = x;
  165. YS2 = y;
  166. if (XS1 == XS2 && YS1 == YS2)
  167. {
  168. Random();
  169. Score += 5;
  170. }
  171. }
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177.  
  178. //////////////////////////////////////InitGame()
  179. void Snake::InitGame(const int x, const int y)
  180. {
  181. CharCoord_X = Coord_X / 2;
  182. CharCoord_Y = Coord_Y / 2;
  183.  
  184. #ifdef DEBUG
  185. *BonusCoord_X = rand() % (Coord_X - 2) + 2;
  186. *BonusCoord_Y = rand() % (Coord_Y - 2) + 2;
  187. #else
  188. Random();
  189. #endif
  190.  
  191. while (Game)
  192. {
  193. if (Score > 4*4 && Score < 10*4)
  194. {
  195. system("color 0a");
  196. }
  197. else if (Score > 9*4 && Score < 14*4)
  198. {
  199. system("color 0c");
  200. }
  201. Keyboard();
  202. CoordStore(0, BonusCoord_X, BonusCoord_Y);
  203. cout << "Score: " << Score << endl;
  204. for (int i = 0; i < y; i++)
  205. {
  206. for (int j = 0; j < x; j++)
  207. {
  208. if (i < 1)
  209. {
  210. cout << "#";
  211. }
  212. else if (i > 0 && i < Coord_Y - 1)
  213. {
  214. if (j > 0 && j < Coord_X - 1)
  215. {
  216. if (j == CharCoord_X && i == CharCoord_Y)
  217. {
  218. CoordStore(1, j, i);
  219. cout << "O";
  220. }
  221. #ifdef DEBUG
  222. else if (j == *BonusCoord_X && i == *BonusCoord_Y)
  223. {
  224. CoordStore(1, *BonusCoord_X, *BonusCoord_Y);
  225. cout << "F";
  226. }
  227. #else
  228. else if (j == BonusCoord_X && i == BonusCoord_Y)
  229. {
  230. cout << "F";
  231. }
  232. #endif
  233. else
  234. {
  235. cout << " ";
  236. }
  237. }
  238. else
  239. {
  240. cout << "#";
  241. }
  242. }
  243. else
  244. {
  245. cout << "#";
  246. }
  247. }
  248. cout << endl;
  249. }
  250. system("cls");
  251. Sleep(30);
  252. }
  253. }
  254.  
  255. //////////////////////////////////////main()
  256. int main()
  257. {
  258. Snake _Snake;
  259. _Snake.InitGame(Coord_X, Coord_Y);
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement