making mobile phone snake game using C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 1
Reputation: hunkadarsh is an unknown quantity at this point 
Solved Threads: 0
hunkadarsh hunkadarsh is offline Offline
Newbie Poster

making mobile phone snake game using C

 
0
  #1
Dec 14th, 2004
i guess i need real helpp for my mini project at college,please help me executing this problem.
Also thr is an .cpp attachment file used in Turbo C ,
Please help

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<bios.h>
  5. #include<stdlib.h>
  6. int gd=DETECT,gm,ctrl=-30,x_location,y_location,rear=-1,maxx,maxy;
  7. int in_key,ctr[1500],nc,xr=0,yr=0,sp=15,err=0,scr=0,i=0,front=0;
  8. int size,x=0,y=0,dx_location,xctr[1500],yctr[1500];
  9. char pattern={1};
  10. main()
  11. {
  12. /*function to initialise the graphics mode*/
  13. grapini();
  14. /*function for intro graphics*/
  15. intro();
  16. /*function to draw boundaries*/
  17. snakeboundary();
  18. /*function to initialise game and generate first dot*/
  19. intgame();
  20. /*condition to check if key pressed is 'q'*/
  21. nc=in_key=bioskey(0);
  22. while(in_key!=4209)
  23. {
  24. /*condition to check if any key is pressed*/
  25. while(!kbhit())
  26. {
  27. /*case to increment front end of snake*/
  28. switch(in_key)
  29. {
  30. case 0x4800:y_location-=1;break;
  31. case 0x5000:y_location+=1;break;
  32. case 0x4b00:x_location-=1;break;
  33. case 0x4d00:x_location+=1;break;
  34. }
  35. putpixel(x_location,y_location,15);
  36. /*condition to check if the snake touches the boundary*/
  37. if(err) break;
  38. if(((y_location==dy_location)&&(x_location==dx_location))||\
  39. (x_location>=maxx-13)||(y_location<=13)||(y_location>=maxy-63))
  40. {
  41. err=1;
  42. break;
  43. }
  44. /*delay to vary speed*/
  45. delay(sp);
  46. /*ctrl variable to allow initial length of snake*/
  47. if(ctrl>0)
  48. {
  49. if((dx_location==xctr[front]&&(dy_location==yctr[front]))
  50. nc=ctr[front++];
  51. if(((dx_location==xr-1)||(dx_location==xr)||(dx_location==xr+1))&&\
  52. ((dy_location==yr-1)||(dy_location==yr)||(dy_location==yr+1)))
  53. /*function for random dot generation*/
  54. randdot();
  55. /*case to increment back end of snake*/
  56. switch(nc)
  57. {
  58. case 0x4800:dy_location-=1;break;
  59. case 0x5000:dy_location+=1;break;
  60. case 0x4b00:dx_location-=1;break;
  61. case 0x4d00:dx_location+=1;break;
  62. }
  63. putpixel(dx_location,dy_location,0);
  64. }
  65. ctrl++;
  66. }
  67. /*stats for storing the new direction and position of snake*/
  68. in_key=bioskey(0);
  69. rear+=1;
  70. ctr[rear]=in_key;
  71. xctr[rear]=x_location;
  72. yctr[rear]=y_location;
  73. }
  74. /*condition to display score after end of game*/
  75. if(err)
  76. {
  77. settextstyle(7,0,5);
  78. setcolor(150;
  79. printf("your score is %d",scr);
  80. outtextxy(200,100,"your game is over");
  81. getch();
  82. }
  83. closegraph();
  84. }
  85. /*end of main*/
  86.  
  87. /*function to initialise graphics mode*/
  88. grapini()
  89. {
  90. intgraph(&gd,&gm,"c:\\turboc3\\turboc3");
  91. setgraphmode(VGAHI);
  92. maxx=getmaxx();
  93. maxy=getmaxy();
  94. moveto(x_location=maxx/2,y_location=maxy/2);
  95. }
  96. /*function for intro graphics*/
  97. intro()
  98. {
  99. int locy=0,locx=0,locy1=0,locy2=0;
  100. settextstyle(7,0,10);
  101. locx=maxx/4-15;
  102. locy1=locy2=locy=maxy/2;
  103. /*condition to display demo till key pressed*/
  104. while(!kbhit())
  105. {
  106. for(i=1;i<=50;i++)
  107. {
  108. setcolor(14);
  109. outtextxy(locx,locy2-maxy/2,"g");
  110. outtextxy(locx+65,locy1-maxy/2,"a");
  111. outtextxy(locx+150,locy2-maxy/2,"m");
  112. outtextxy(locx+295,locy1-maxy/2,"e");
  113. outtextxy(locx,locy1,"s");
  114. outtextxy(locx+60,locy2,"n");
  115. outtextxy(locx+150,locy1,"a");
  116. outtextxy(locx+225,locy2,"k");
  117. outtextxy(locx+300,locy1,"e");
  118. delay(50);
  119. locy1++;
  120. locy2--;
  121. setcolor(0);
  122. outtextxy(locx,locy2-maxy/2+1,"j");
  123. outtextxy(locx+65,locy1-maxy/2-1,"p");
  124. outtextxy(locx+150,locy2-maxy/2+1,"'");
  125. outtextxy(locx+295,locy-maxy/2-1,"s");
  126. outtextxy(locx,locy1-1,"s");
  127. outtextxy(locx+60,locy2+1,"n");
  128. outtextxy(locx+150,locy1-1,"a");
  129. outtextxy(locx+225,locy2+1,"k");
  130. outtextxy(locx+300,locy1-1,"e");
  131. }
  132. }
  133. getch();
  134. }
  135. /*function to draw boundaries*/
  136. snake boundary()
  137. {
  138. setcolor(10);
  139. line(10,10,10,maxy-60);line(13,13,13,maxy-63);
  140. line(10,10,maxx-10,10);line(13,13,maxx-13,13);
  141. line(maxx-10,10,maxx-10,maxy-60);line(maxx-13,13,maxx-13,maxy-63);
  142. line(10,maxy-60,maxx-10,maxy-60);line(13,maxy-63,maxx-13,maxy-63);
  143. }
  144. /*function to initialise game and generate first dot*/
  145. intgame()
  146. {
  147. randomize();
  148. xr=random(maxx-13);
  149. if(xr<=13)
  150. xr+=13;
  151. yr=random(maxy-63);
  152. if(yr<=13)
  153. yr+=13;
  154. bar(xr-1,yr+1,xr+1,yr-1);
  155. setfillstyle(pattern,15);
  156. dx_location=maxx/2;
  157. dy_location=maxy/2;
  158. }
  159. /*function for random dot generation*/
  160. randdot()
  161. {
  162. scr++;
  163. setfillstyle(pattern,0);
  164. bar(xr-2,yr+2,xr+2,yr-2);
  165. sp=sp-1;
  166. xr=random(maxx-13);
  167. if(xr<=13)
  168. xr+=13;
  169. yr=random(maxy-63);
  170. if(yr<=13)
  171. yr+=13;
  172. bar(xr-1,yr+1,xr+1,yr-1);
  173. ctrl=-30;
  174. setfillstyle(pattern,15);
  175. bar(xr-1,yr+1,xr+1,yr-1);
  176. }
Last edited by alc6379; Dec 14th, 2004 at 3:07 pm.
Attached Files
File Type: cpp Snake.cpp (4.1 KB, 34 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC