Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. //Binary logic
  5. #define TRUE 1
  6. #define FALSE 0
  7.  
  8. //Snake directions
  9. #define STOP 0 //snake is not moving (in the beginning or when game is paused)
  10. #define LEFT 1 //snake is moving left
  11. #define RIGHT 2 //snake is moving right
  12. #define UP 3 //snake is moving up
  13. #define DOWN 4 //snake is moving down
  14.  
  15. //Snake, Fruit, Boundary
  16. #define SNAKE_HEAD "O" //printf(SNAKE_HEAD) will draw the snake head
  17. #define SNAKE_TAIL "x" //printf(SNAKE_TAIL) will draw a segment of the snake tail
  18. #define FRUIT "F" //printf(FRUIT) will draw the fruit
  19. #define MOUSE "M" //printf(FRUIT) will draw the fruit
  20.  
  21. #define BOUNDARY "#" //printf(BOUNDARY) will draw the boundary
  22. #define SPACE " " //printf(SPACE) will draw an empty space
  23.  
  24. //Escape key
  25. #define ESC_KEY 27 //ASCII code for escape ESC key
  26.  
  27. // global variables
  28. int gameOver; //to indicate game over
  29. int mapWidth = 20; //width of map
  30. int mapHeight = 20; //height of map
  31. int snakeHeadX, snakeHeadY; //X, Y coordinates of snake head
  32. int fruitX, fruitY; //X, Y coordinates of fruit
  33. int mouseX, mouseY; //X, Y coordinates of fruit
  34.  
  35. int dir; //direction of snake
  36. int tailX[400], tailY[400]; //snake tail X Y coordinates arrays ; maximum length of snake is 400
  37. int tailLength; //length of snake tail
  38.  
  39. int lives = 0;
  40. bool gameclose;
  41. int score;
  42. int scoremod;
  43. bool pause;
  44.  
  45. // function prototypes
  46. void displayWelcome(); //display welcome screen with welcome message and instructions
  47. void initializeGame(); //initialize the snake game
  48. void drawScreen(); //draw display screen with map boundary, snake and fruit
  49. void readInput(); //read input from keyboard
  50. void updateSnake(); //update snake head and tail positions to move by one step
  51. void checkConditions(); //check if snake hit boundary, bites its own tail or eats the fruit
  52. void displayGameOver(); //display game over screen
  53.  
  54. void main()
  55. {
  56. gameclose = false;
  57. pause = false;
  58. while (!gameclose)
  59. {
  60. system("cls");
  61. fseek(stdin, 0, SEEK_END);
  62. displayWelcome(); //display welcome screen with welcome message and instructions
  63. initializeGame(); //initialize the snake game
  64. while (!gameOver) { //if not yet game over
  65. if (pause)
  66. {
  67. if (getch() == 'p')
  68. pause = false;
  69. continue;
  70. }
  71. drawScreen(); //draw display screen with map boundary, snake and fruit
  72. readInput(); //read input from keyboard
  73. updateSnake();//update snake head and tail positions to move by one step
  74. checkConditions();//check if snake hit boundary, bites its own tail or eats the fruit
  75. Sleep(100); //delay by 50ms to slow down the snake's movement
  76. }
  77. displayGameOver(); //display game over screen
  78. }
  79. }
  80.  
  81. void displayWelcome()
  82. {
  83. // Part 1 : Complete the welcome screen
  84. printf("\n\t\t\t\t\t Welcome to snake game\n\n\n");
  85. printf("\t\t Control the snake to eat the fruits that appear at random locations\n\n");
  86. printf("\n\n\t\t\t The snake will grow longer when it has eaten a fruit");
  87. printf("\n\n\n\n\t\t\t\t\t GOOD LUCK!!!\n\n\n");
  88. printf("Directions:\n");
  89. printf("Left- Press'a'\n");
  90. printf("Right- Press'd'\n");
  91. printf("Up- Press'w'\n");
  92. printf("Down- Press's'\n");
  93. printf("Quit- Press'Esc'\n\n");
  94. printf("Press any key to start.");
  95. fseek(stdin, 0, SEEK_END);
  96. while (true)
  97. {
  98. if(getch())
  99. {
  100. break;
  101. }
  102. }
  103. Sleep(1000);
  104. fseek(stdin, 0, SEEK_END);
  105.  
  106. }
  107.  
  108. void initializeGame() {
  109. gameOver = FALSE; // gameOver is false at beginning of the game
  110. srand(NULL); // initialize seed for random number generator
  111. dir = STOP; // snake has not yet started moving
  112.  
  113. // Part 2: initialize the X and Y coordinates of snake head and fruit
  114.  
  115.  
  116.  
  117. // place the head of the snake at the centre of the map at the beginning of the game
  118. // use variables : snakeX, snakeY
  119. snakeHeadX = mapWidth / 2;
  120. snakeHeadY = mapHeight / 2;
  121.  
  122. score = 0;
  123. scoremod = 5;
  124. lives = 3;
  125.  
  126. // place the fruit at random location inside the map
  127. // use variables : fruitX, fruitY and rand() function
  128. fruitX = rand() % (mapWidth - 1);
  129. fruitY = rand() % (mapHeight - 1);
  130. while (fruitX == 0) {
  131. fruitX = rand() % (mapWidth - 1);
  132. }
  133. while (fruitY == 0) {
  134. fruitY = rand() % (mapHeight - 1);
  135. }
  136.  
  137. mouseX = rand() % (mapWidth - 1);
  138. mouseY = rand() % (mapHeight - 1);
  139. while (mouseX == 0) {
  140. mouseX = rand() % (mapWidth - 1);
  141. }
  142. while (mouseY == 0) {
  143. mouseY = rand() % (mapHeight - 1);
  144. }
  145.  
  146. }
  147.  
  148. void drawScreen() {
  149. //This function draws display screen with map boundary, snake and fruit
  150. int x; // loop counter for x axis
  151. int y; // loop counter for y axis
  152. int k; // loop counter to count length of snake tail
  153. int drawTail = FALSE; //draw is set to TRUE when a tail is drawn at the coordinates
  154.  
  155. system("cls"); // clears the entire screen
  156. printf("\n");
  157.  
  158. //draw left and right boundary, top and bottom boundary, snake and fruit
  159.  
  160. // Part 3: Complete the nested for loops
  161. for (y = 0; y < mapHeight; y++) { //loop from top to bottom, y=0 to y=mapHeight-1
  162. for (x = 0; x < mapWidth; x++) { //loop from left to right, x=0 to x=mapWidth-1
  163. // Insert your code below
  164. if (y == 0 || y == mapHeight - 1)
  165. {
  166. printf(BOUNDARY);
  167. }
  168. else if (x == 0 || x == mapWidth - 1)
  169. {
  170. printf(BOUNDARY);
  171. }
  172. else if (x == snakeHeadX && y == snakeHeadY)
  173. {
  174. switch(dir)
  175. {
  176. case RIGHT:
  177. printf(">");
  178. break;
  179. case LEFT:
  180. printf("<");
  181. break;
  182. case UP:
  183. printf("^");
  184. break;
  185. case DOWN:
  186. printf("v");
  187. break;
  188. default:
  189. printf(SNAKE_HEAD);
  190. break;
  191. }
  192. }
  193. else if (x == fruitX && y == fruitY)
  194. {
  195. printf(FRUIT);
  196. }
  197. else if (x == mouseX && y == mouseY)
  198. {
  199. printf(MOUSE);
  200. }
  201. else
  202. {
  203. drawTail = FALSE;
  204. for (k = tailLength; k >= 0; k--)
  205. {
  206. if (x == tailX[k] && y == tailY[k])
  207. {
  208. printf(SNAKE_TAIL);
  209. drawTail = TRUE;
  210. }
  211.  
  212. }
  213. if (drawTail == FALSE)
  214. {
  215. printf(SPACE);
  216. }
  217. }
  218. }//end of inner for loop
  219. printf("\n");
  220.  
  221. }//end of outer for loop
  222. printf("Score: %d\n", score);
  223. printf("Lives: %d\n", lives);
  224.  
  225. printf("Press 'p' to pause/unpause\n");
  226. }
  227.  
  228. void readInput() {
  229. // Part 4: Complete the switch-case structure
  230. // This function reads input from keyboard and determine direction of snake's movement
  231. if (kbhit()) { //detect whether a key is pressed
  232. switch (getch()) { //read in a character and determine the direction of the snake
  233. case 'a':
  234. {
  235. if (dir == RIGHT)
  236. break;
  237. dir = LEFT;
  238. break;
  239. }
  240. case 'd':
  241. {
  242. if (dir == LEFT)
  243. break;
  244. dir = RIGHT;
  245. break;
  246. }
  247. case 'w':
  248. {
  249. if (dir == DOWN)
  250. break;
  251. dir = UP;
  252. break;
  253. }
  254. case 's':
  255. {
  256. if (dir == UP)
  257. break;
  258. dir = DOWN;
  259. break;
  260. }
  261. case 'p':
  262. pause = true;
  263. break;
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274. }
  275. }
  276. }
  277.  
  278. void updateSnake() {
  279. //This function update the coordinates of the snake head, and snake tail segments
  280. //Update from the final segment of the snake to the snake head
  281.  
  282. int i; //loop counter
  283.  
  284. //loop to the move all the segments of the snake tail
  285. //The final segment of the tail will move to the 2nd last segment of the tail
  286. //The 2nd last segment of the tail will move to the 3rd last segment of the tail
  287. //...and so on
  288. //The second segment of the tail will move to the first segment of the tail
  289. for (i = tailLength - 1; i >0; i--) {
  290. tailX[i] = tailX[i - 1];
  291. tailY[i] = tailY[i - 1];
  292. }
  293.  
  294. // move the first segment of tail to the snake head's position
  295. tailX[0] = snakeHeadX;
  296. tailY[0] = snakeHeadY;
  297.  
  298. //check snake direction and update coordinates of snake head
  299. switch (dir) {
  300. case LEFT: //if snake moves to the left
  301. snakeHeadX--; //x coordinate decreases by 1; no change in y coordinates
  302. break;
  303. case RIGHT: //if snake moves to the right
  304. snakeHeadX++; //x coordinate increases by 1; no change in y coordinates
  305. break;
  306. case UP: //if snake moves up
  307. snakeHeadY--; //y coordinate decreases by 1; no change in x coordinates
  308. break;
  309. case DOWN: //if snake moves down
  310. snakeHeadY++; //y coordinate increases by 1; no change in x coordinates
  311. break;
  312. default:
  313. break;
  314. }
  315. static int counter = 0;
  316. ++counter;
  317. if (counter > 5)
  318. {
  319. //check snake direction and update coordinates of snake head
  320. switch (rand() % 5) {
  321. case LEFT: //if snake moves to the left
  322. if (mouseX <= 1)
  323. break;
  324. mouseX--; //x coordinate decreases by 1; no change in y coordinates
  325. break;
  326. case RIGHT: //if snake moves to the right
  327. if (mouseX >= 17)
  328. break;
  329. mouseX++; //x coordinate increases by 1; no change in y coordinates
  330. break;
  331. case UP: //if snake moves up
  332. if (mouseY <= 1)
  333. break;
  334. mouseY--; //y coordinate decreases by 1; no change in x coordinates
  335. break;
  336. case DOWN: //if snake moves down
  337. if (mouseY >= 17)
  338. break;
  339. mouseY++; //y coordinate increases by 1; no change in x coordinates
  340. break;
  341. default:
  342. break;
  343. }
  344. counter = 0;
  345. }
  346. }
  347.  
  348. void checkConditions()
  349. {
  350. // Part 5 : Complete the if statements
  351. //This function checks whether the snake hits the boundary, bites its own tail or eats the fruit
  352. // if the snake head hits or crosses the boundary
  353. // game over !
  354. bool hit = false;
  355. int i;
  356. if (snakeHeadX <= 0 || snakeHeadX > 18 || snakeHeadY <= 0 || snakeHeadY >= 18)
  357. hit = true;
  358.  
  359.  
  360. // if the snake head bites any part of its tail
  361. // game over !
  362. for (i = tailLength; i>0; i--) {
  363. if (snakeHeadX == tailX[i] && snakeHeadY == tailY[i])
  364. hit = false;
  365. }
  366.  
  367.  
  368. // if the snake eats the fruit
  369. // then regenerates a new fruit at random locations
  370. if (snakeHeadX == fruitX && snakeHeadY == fruitY) {
  371. score += scoremod;
  372. tailLength++;
  373. fruitX = rand() % (mapWidth - 1);
  374. fruitX = rand() % (mapHeight - 1);
  375. while (fruitX == 0) {
  376. fruitX = rand() % (mapWidth - 1);
  377. }
  378. while (fruitY == 0) {
  379. fruitY = rand() % (mapHeight - 1);
  380. }
  381. }
  382.  
  383. if (snakeHeadX == mouseX && snakeHeadY == mouseY) {
  384. score += scoremod;
  385. tailLength++;
  386. mouseX = rand() % (mapWidth - 1);
  387. mouseY = rand() % (mapHeight - 1);
  388. while (mouseX == 0) {
  389. mouseX = rand() % (mapWidth - 1);
  390. }
  391. while (mouseY == 0) {
  392. mouseY = rand() % (mapHeight - 1);
  393. }
  394. }
  395. if (hit)
  396. {
  397. tailLength = 0;
  398. lives--;
  399. snakeHeadX = mapWidth / 2;
  400. snakeHeadY = mapHeight / 2;
  401. for (int i = 0; i < 400; ++i)
  402. {
  403. tailX[i] = tailY[i] = 0;
  404. }
  405. }
  406.  
  407. if (lives < 0)
  408. {
  409. gameOver = true;
  410. }
  411. }
  412. void displayGameOver()
  413. {
  414. fseek(stdin,0,SEEK_END);
  415. //This function discplays the game over screen
  416. system("cls"); //clear screen
  417. printf("\n\n\t\t\t Game Over! \n\n\n\n"); //display Game Over message
  418. printf("\n \n \n \n Press any key to return to main menu.");// prompt user to press a key
  419. printf("\n \n \n \n Press any 'X' to End.");// prompt user to press a key
  420.  
  421. while (!gameclose)
  422. {
  423. if (kbhit())
  424. {
  425. char key = getch();
  426. if (key == 'x')
  427. gameclose = true;
  428. else
  429. break;
  430. }
  431. }
  432. fseek(stdin,0,SEEK_END);
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement