Guest User

Untitled

a guest
Nov 12th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. #include <graphics.h>
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<process.h>
  6. #include<dos.h>
  7.  
  8. #define UP_KEY 'H'
  9. #define RIGHT_KEY 'M'
  10. #define DOWN_KEY 'P'
  11. #define LEFT_KEY 'K'
  12.  
  13. #define UP_DIRECTION 3
  14. #define RIGHT_DIRECTION 1
  15. #define DOWN_DIRECTION 4
  16. #define LEFT_DIRECTION 2
  17.  
  18. #define True 1
  19. #define False 0
  20.  
  21. typedef struct node
  22. {
  23. int x,y,radius;
  24. struct node *next;
  25. }node;
  26.  
  27. typedef struct queue
  28. {
  29. node *rear,*front;
  30. }queue;
  31.  
  32. void init(queue *t)
  33. {
  34. t->rear=t->front=NULL;
  35. }
  36.  
  37. int empty(queue *t)
  38. {
  39. if(t->rear==NULL)
  40. return 0;
  41. return 1;
  42. }
  43.  
  44. void insert(queue *t,int x1,int y1,int r1)
  45. {
  46. node *p;
  47. p=(node*)malloc(sizeof(node));
  48. p->x=x1;
  49. p->y=y1;
  50. p->radius=r1;
  51. p->next=NULL;
  52.  
  53. if(t->rear==NULL)
  54. {
  55. t->front=t->rear=p;
  56. }
  57. else
  58. {
  59. (t->rear)->next=p;
  60. t->rear=p;
  61. }
  62. }
  63.  
  64. int delete(queue *t)
  65. {
  66. int x;
  67. node *u;
  68. u=t->front;
  69.  
  70. if(t->rear==t->front)
  71. {
  72. init(t);
  73. }
  74. else
  75. {
  76. t->front=(t->front)->next;
  77. }
  78. free(u);
  79. return x;
  80. }
  81.  
  82. void display_snake(queue *t)
  83. {
  84. node *s;
  85. s=t->front;
  86. while(s!=NULL)
  87. {
  88. circle(s->x,s->y,s->radius);
  89. s=s->next;
  90. }
  91. }
  92.  
  93. void init_snake(queue *t)
  94. {
  95. insert(t,6,204,3);
  96. insert(t,12,204,3);
  97. insert(t,18,204,3);
  98. insert(t,24,204,3);
  99. insert(t,30,204,3);
  100. // so now next node will be insert(t,36,204,3)
  101. // check variable snake_head_x_cord and snake_head_y_cord
  102. // in main function
  103. }
  104.  
  105. void main()
  106. {
  107. queue p,*t=&p;
  108. node *s;
  109. int snake_head_x_cord=36,snake_head_y_cord=204,dir=RIGHT_DIRECTION,draw_food=True,i,score=0,food_x_cord,food_y_cord,gdriver = DETECT, gmode;
  110. char key_pressed;
  111. initgraph(&gdriver, &gmode, "");
  112. init(t);
  113. init_snake(t);
  114. printf("\n\n\n\n\n\n\t\tUse Arrow Keys To Controll Snake\n\t\t\tPress 'q' to Quit\n\t\t\tPress Enter to Play");
  115. getch();
  116. cleardevice();
  117. display_snake(t);
  118. rectangle(1,7,485,455);
  119. rectangle(3,9,483,453);
  120. gotoxy(65,10);
  121. printf("score:{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",score);
  122.  
  123. // Loop forever until snake collide with walls or himself
  124. while(True)
  125. {
  126. if(draw_food==True)
  127. {
  128. // find co-ordinates for food
  129. while(True)
  130. {
  131. food_x_cord=random(480);
  132. food_y_cord=random(450);
  133.  
  134. // new food generated should have correct x and y co-ordinate, so
  135. // that it will match with x&y co-ordinates of head of snake to occur
  136. // collision
  137. if(food_x_cord{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}6==0&&food_y_cord{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}6==0&&food_x_cord>5&&food_y_cord>11)
  138. break;
  139. }
  140. }
  141. // draw food
  142. circle(food_x_cord,food_y_cord,3);
  143. draw_food=False;
  144.  
  145. // if snake collide with food
  146. if((t->rear)->x==food_x_cord&&(t->rear)->y==food_y_cord)
  147. {
  148. score++;
  149. draw_food=True;
  150. sound(11000);
  151. delay(20);
  152. nosound();
  153.  
  154. // increase length of snake by 1,
  155. // i.e. add new node in front of head node
  156. if(dir==RIGHT_DIRECTION)
  157. insert(t,snake_head_x_cord+4,snake_head_y_cord,5);
  158. if(dir==LEFT_DIRECTION)
  159. insert(t,snake_head_x_cord-4,snake_head_y_cord,5);
  160. if(dir==UP_DIRECTION)
  161. insert(t,snake_head_x_cord,snake_head_y_cord-4,5);
  162. if(dir==4)
  163. insert(t,snake_head_x_cord,snake_head_y_cord+4,5);
  164.  
  165. }
  166.  
  167. // read key pressed by user
  168. if(kbhit())
  169. {
  170. key_pressed=getch();
  171. if(key_pressed=='q')
  172. exit(0);
  173. key_pressed=getch();
  174. }
  175.  
  176. // set the direction of snake
  177. if(key_pressed==LEFT_KEY&&dir!=RIGHT_DIRECTION)
  178. dir=LEFT_DIRECTION;
  179. if(key_pressed==RIGHT_KEY&&dir!=LEFT_DIRECTION)
  180. dir=RIGHT_DIRECTION;
  181. if(key_pressed==UP_KEY&&dir!=DOWN_DIRECTION)
  182. dir=UP_DIRECTION;
  183. if(key_pressed==DOWN_KEY&&dir!=UP_DIRECTION)
  184. dir=DOWN_DIRECTION;
  185.  
  186. // turn and move snake according to key pressed by user
  187. if(dir==RIGHT_DIRECTION)
  188. {
  189. snake_head_x_cord=snake_head_x_cord+6;
  190. display_snake(t);
  191. circle(food_x_cord,food_y_cord,3);
  192. // Delete node from tail
  193. delete(t);
  194. // insert node next to head
  195. insert(t,snake_head_x_cord,snake_head_y_cord,3);
  196. rectangle(1,7,485,455);
  197. rectangle(3,9,483,453);
  198. gotoxy(65,10);
  199. printf("score:{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",score);
  200. delay(50);
  201. cleardevice();
  202. }
  203.  
  204. if(dir==LEFT_DIRECTION)
  205. {
  206. snake_head_x_cord=snake_head_x_cord-6;
  207. display_snake(t);
  208. circle(food_x_cord,food_y_cord,3);
  209. // Delete node from tail
  210. delete(t);
  211. // insert node next to head
  212. insert(t,snake_head_x_cord,snake_head_y_cord,3);
  213. rectangle(1,7,485,455);
  214. rectangle(3,9,483,453);
  215. gotoxy(65,10);
  216. printf("score:{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",score);
  217. delay(50);
  218. cleardevice();
  219. }
  220.  
  221. if(dir==UP_DIRECTION)
  222. {
  223. snake_head_y_cord=snake_head_y_cord-6;
  224. display_snake(t);
  225. circle(food_x_cord,food_y_cord,3);
  226. // Delete node from tail
  227. delete(t);
  228. // insert node next to head
  229. insert(t,snake_head_x_cord,snake_head_y_cord,3);
  230. rectangle(1,7,485,455);
  231. rectangle(3,9,483,453);
  232. gotoxy(65,10);
  233. printf("score:{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",score);
  234. delay(50);
  235. cleardevice();
  236. }
  237.  
  238. if(dir==DOWN_DIRECTION)
  239. {
  240. snake_head_y_cord=snake_head_y_cord+6;
  241. display_snake(t);
  242. circle(food_x_cord,food_y_cord,3);
  243. // Delete node from tail
  244. delete(t);
  245. // insert node next to head
  246. insert(t,snake_head_x_cord,snake_head_y_cord,3);
  247. rectangle(1,7,485,455);
  248. rectangle(3,9,483,453);
  249. gotoxy(65,10);
  250. printf("score:{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",score);
  251. delay(50);
  252. cleardevice();
  253. }
  254.  
  255. s=t->front;
  256.  
  257. while(s->next->next!=NULL)
  258. {
  259. // condtion to Check if snake hit the walls or himself
  260. if(s->x==(t->rear)->x&&s->y==(t->rear)->y||(t->rear)->x<6||(t->rear)->y<12||(t->rear)->x>480||(t->rear)->y>450)
  261. {
  262. // blick the screen if snake hits wall or himself
  263. for(i=0;i<5;i++)
  264. {
  265. sound(4000);
  266. delay(20);
  267. nosound();
  268. gotoxy(65,10);
  269. printf("score:{b15f91eef52e7c6d40fd3fe45227e26332e4c02be027e4e72c6ecf7adacefb97}d",score);
  270. rectangle(1,7,485,455);
  271. rectangle(3,9,483,453);
  272. circle(food_x_cord,food_y_cord,3);
  273. delay(280);
  274. display_snake(t);
  275. delay(300);
  276. cleardevice();
  277. }
  278. goto end;
  279. }
  280. s=s->next;
  281. }
  282. }
  283. end:
  284. printf("\n\n\n\n\n\t\t\t\tEnd");
  285. getch();
  286. }
Add Comment
Please, Sign In to add comment