fork download
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <dos.h>
  6. #include <iostream.h>
  7. #include <fstream.h>
  8. #include <string.h>
  9.  
  10.  
  11. void main(void)
  12. {
  13. int gdriver = DETECT, gmode, errorcode;
  14. void
  15. *body,*food,*tail1,*tail2,*tail3,*tail4,*head1,*head2,*head3,*head4;
  16. int x, y,X[5000],Y[5000],i=3,
  17. maxx,maxy,speed=100,bo=10,t[10],score=0,hscore=20;
  18. unsigned int size;
  19. char a='6',b,scor[4],hs[4];
  20. int k=2,l,r1,r2,f=0,z=100,first=0,second=1;;
  21. r1=300;
  22. r2=350;
  23.  
  24. ifstream infile("c:\tc\bin\rattle.txt");
  25. infile.getline(hs,4);
  26. infile.close();
  27. hscore = atoi(hs);
  28.  
  29.  
  30.  
  31. /* initialize graphics and local variables */
  32. initgraph(&gdriver, &gmode, "c:\tc\bgi");
  33.  
  34. /* read result of initialization */
  35. errorcode = graphresult();
  36. if (errorcode != grOk) /* an error occurred */
  37. {
  38. printf("Graphics error: %s
  39. ", grapherrormsg(errorcode));
  40. printf("Press any key to halt:");
  41. getch();
  42. exit(1); /* terminate with an error code */
  43. }
  44. maxx = getmaxx();
  45. maxy = getmaxy();
  46. y = 160;
  47. x = 80;
  48. for(int j=0; j<=3; j++)
  49. {
  50. X[i-j]=x-bo*(j+1);
  51. Y[i-j]=y;
  52.  
  53. }
  54.  
  55. //body
  56.  
  57. setfillstyle(1,2);
  58. bar(10,10,20,20);
  59. setcolor(0);
  60. setlinestyle(0,1,3);
  61. line(9,9,21,21);
  62. line(9,21,21,9);
  63. size = imagesize(10,10,20,20);
  64. body = malloc(size);
  65. getimage(10,10,20,20,body);
  66. cleardevice();
  67.  
  68. setcolor(0);
  69. //tail1-right
  70. t[0]=20; t[1]=10;
  71. t[2]=10; t[3]=10;
  72. t[4]=20; t[5]=15;
  73. t[6]=10; t[7]=20;
  74. t[8]=20; t[9]=21;
  75. fillpoly(5, t);
  76. size = imagesize(10,10,20,21);
  77. tail1 = malloc(size);
  78. getimage(10,10,20,21,tail1);
  79. cleardevice();
  80.  
  81. //tail2-left
  82. t[0]=10; t[1]=10;
  83. t[2]=20; t[3]=10;
  84. t[4]=10; t[5]=15;
  85. t[6]=20; t[7]=20;
  86. t[8]=10; t[9]=21;
  87. fillpoly(5, t);
  88. size = imagesize(10,10,20,21);
  89. tail2 = malloc(size);
  90. getimage(10,10,20,21,tail2);
  91. cleardevice();
  92.  
  93. //tail3-up
  94. t[0]=10; t[1]=20;
  95. t[2]=10; t[3]=10;
  96. t[4]=15; t[5]=20;
  97. t[6]=20; t[7]=10;
  98. t[8]=20; t[9]=21;
  99. fillpoly(5, t);
  100. size = imagesize(10,10,20,21);
  101. tail3 = malloc(size);
  102. getimage(10,10,20,21,tail3);
  103. cleardevice();
  104.  
  105. //tail4-down
  106. t[0]=10; t[1]=10;
  107. t[2]=10; t[3]=21;
  108. t[4]=15; t[5]=10;
  109. t[6]=20; t[7]=21;
  110. t[8]=20; t[9]=10;
  111. fillpoly(5, t);
  112. size = imagesize(10,10,20,21);
  113. tail4 = malloc(size);
  114. getimage(10,10,20,21,tail4);
  115. cleardevice();
  116.  
  117. //head1-right
  118. setlinestyle(1,1,1);
  119. setcolor(2);
  120. fillellipse(10,10,10,5);
  121. setcolor(4);
  122. //eyes
  123. fillellipse(15,7,1,1);
  124. fillellipse(15,13,1,1);
  125. size = imagesize(10,5,20,15);
  126. head1 = malloc(size);
  127. getimage(10,5,20,15,head1);
  128.  
  129. //head2-left
  130. //eyes
  131. fillellipse(5,7,1,1);
  132. fillellipse(5,13,1,1);
  133. size = imagesize(0,5,10,15);
  134. head2 = malloc(size);
  135. getimage(0,5,10,15,head2);
  136. cleardevice();
  137.  
  138. //head3-up
  139. //eyes
  140. setcolor(2);
  141. fillellipse(12,12,5,10);
  142. setcolor(4);
  143. fillellipse(9,6,1,1);
  144. fillellipse(15,6,1,1);
  145. size = imagesize(7,2,18,12);
  146. head3 = malloc(size);
  147. getimage(7,2,18,12,head3);
  148.  
  149.  
  150. //head4-down
  151. //eyes
  152. fillellipse(9,17,1,1);
  153. fillellipse(15,17,1,1);
  154. size = imagesize(7,12,18,22);
  155. head4 = malloc(size);
  156. getimage(7,12,18,22,head4);
  157. cleardevice();
  158.  
  159.  
  160. //food
  161. setcolor(15);
  162. setfillstyle(1,15);
  163. fillellipse(10,10,3,5);
  164. size = imagesize(5,3,15,17);
  165. food = malloc(size);
  166. getimage(5,3,15,17,food);
  167. cleardevice();
  168.  
  169. /* repeat until a key is pressed */
  170.  
  171. maxx=getmaxx();
  172. maxy=getmaxy();
  173. setlinestyle(0,1,3);
  174. setcolor(9);
  175. rectangle(0,0,maxx,maxy);
  176. setlinestyle(0,1,2);
  177. line(maxx-150,0,maxx-150,maxy);
  178. setcolor(8);
  179. setfillstyle(1,8);
  180. bar(maxx-147,3,maxx-3,63);
  181. setcolor(12);
  182. int maze=1;
  183. if(maze==1)
  184. {
  185. setfillstyle(1,6);
  186. bar(4,4,maxx-154,14);
  187. bar(4,4,14,maxy-4);
  188. bar(4,maxy-4,maxx-154,maxy-14);
  189. bar(maxx-154-10,4,maxx-154,maxy-4);
  190. }
  191.  
  192.  
  193. settextstyle(0,0,1);
  194. delay(300);
  195. outtextxy(maxx-125,30,"RATTLE SNAKE");
  196. delay(300);
  197. setcolor(11);
  198. setlinestyle(0,1,2);
  199. rectangle(maxx-145,65,maxx-5,205);
  200. outtextxy(maxx-128,70,"INSTRUCTIONS");
  201. delay(200);
  202. setcolor(6);
  203. outtextxy(maxx-140,90,"Commands Key");
  204. delay(300);
  205. setcolor(8);
  206. outtextxy(maxx-140,110," Move Up 8");
  207. delay(50);
  208. outtextxy(maxx-140,130," Move Down 5");
  209. delay(50);
  210. outtextxy(maxx-140,150," Move Right 6");
  211. delay(50);
  212. outtextxy(maxx-140,170," Move Left 4");
  213. delay(50);
  214. outtextxy(maxx-140,190," Exit 0");
  215. delay(200);
  216. setcolor(12);
  217. setlinestyle(0,1,2);
  218. rectangle(maxx-145,210,maxx-5,350);
  219. setcolor(1);
  220. outtextxy(maxx-130,220,"Player's Name");
  221. setcolor(15);
  222. outtextxy(maxx-100,230,"hiral");
  223. setcolor(4);
  224. delay(200);
  225. itoa(score,scor,10);
  226. outtextxy(maxx-140,250,"Score");
  227. outtextxy(maxx-50,250,scor);
  228. delay(200);
  229. setcolor(10);
  230. outtextxy(maxx-140,280,"Level");
  231. delay(200);
  232. setcolor(13);
  233. outtextxy(maxx-140,310,"Maze 1");
  234. delay(200);
  235. setcolor(14);
  236. outtextxy(maxx-140,340,"High Score ");
  237. outtextxy(maxx-50,340,hs);
  238.  
  239. for(j=0; j<=1000; j++)
  240. {
  241. putpixel(random(maxx-150),random(maxy),6);
  242. delay(1);
  243. }
  244.  
  245. while (a!='0')
  246. {
  247.  
  248. // plot new image
  249. if(a=='6')
  250. putimage(x, y, head1, XOR_PUT);
  251. if(a=='4')
  252. putimage(x, y, head2, XOR_PUT);
  253. if(a=='8')
  254. putimage(x, y, head3, XOR_PUT);
  255. if(a=='5')
  256. putimage(x, y, head4, XOR_PUT);
  257.  
  258. for(j=0; j<k; j++)
  259. {
  260. putimage(X[i-j], Y[i-j], body, XOR_PUT);
  261. }
  262.  
  263. if((X[i-j-1]-X[i-j])<0 && (Y[i-j-1]-Y[i-j])==0)
  264. putimage(X[i-j], Y[i-j], tail1, XOR_PUT);
  265.  
  266. if((X[i-j-1]-X[i-j])>0 && (Y[i-j-1]-Y[i-j])==0)
  267. putimage(X[i-j], Y[i-j], tail2, XOR_PUT);
  268.  
  269. if((X[i-j-1]-X[i-j])==0 && (Y[i-j-1]-Y[i-j])<0)
  270. putimage(X[i-j], Y[i-j], tail3, XOR_PUT);
  271.  
  272. if((X[i-j-1]-X[i-j])==0 && (Y[i-j-1]-Y[i-j])>0)
  273. putimage(X[i-j], Y[i-j], tail4, XOR_PUT);
  274.  
  275. delay(speed);
  276.  
  277. if(second==1)
  278. {
  279. setcolor(15);
  280. outtextxy(maxx-140,360,"Press Any Key...");
  281. getch();
  282. setcolor(0);
  283. outtextxy(maxx-140,360,"Press Any Key...");
  284. a='6';
  285.  
  286. second=2;
  287. }
  288.  
  289.  
  290. if(first==1)
  291. {
  292. setcolor(15);
  293. outtextxy(maxx-140,360,"Congratulations ");
  294. sound(100);
  295. delay(300);
  296. nosound();
  297. getch();
  298. setcolor(0);
  299. outtextxy(maxx-140,360,"Congratulations ");
  300. second++;
  301. first=2;
  302.  
  303. }
  304.  
  305. // erase old image
  306. if(a=='6')
  307. putimage(x, y, head1, XOR_PUT);
  308. if(a=='4')
  309. putimage(x, y, head2, XOR_PUT);
  310. if(a=='8')
  311. putimage(x, y, head3, XOR_PUT);
  312. if(a=='5')
  313. putimage(x, y, head4, XOR_PUT);
  314.  
  315.  
  316. for(j=0; j<k; j++)
  317. {
  318. putimage(X[i-j], Y[i-j], body, XOR_PUT);
  319.  
  320. }
  321.  
  322. if((X[i-j-1]-X[i-j])<0 && (Y[i-j-1]-Y[i-j])==0)
  323. putimage(X[i-j], Y[i-j], tail1, XOR_PUT);
  324.  
  325. if((X[i-j-1]-X[i-j])>0 && (Y[i-j-1]-Y[i-j])==0)
  326. putimage(X[i-j], Y[i-j], tail2, XOR_PUT);
  327.  
  328. if((X[i-j-1]-X[i-j])==0 && (Y[i-j-1]-Y[i-j])<0)
  329. putimage(X[i-j], Y[i-j], tail3, XOR_PUT);
  330.  
  331. if((X[i-j-1]-X[i-j])==0 && (Y[i-j-1]-Y[i-j])>0)
  332. putimage(X[i-j], Y[i-j], tail4, XOR_PUT);
  333.  
  334.  
  335. if(f==0)
  336. {
  337. putimage(r1,r2,food,XOR_PUT);
  338. f=1;
  339. }
  340.  
  341. z--;
  342. if((x>=r1 && y>=r2 && x<=r1+10 && y<=r2+10) || (x<=r1 && y<=r2 &&
  343. x>=r1-10 && y>=r2-10) || z==0)
  344. {
  345. if(z!=0)
  346. {
  347. sound(800);
  348. delay(20);
  349. setcolor(0);
  350. score += 10;
  351. outtextxy(maxx-50,250,scor);
  352. itoa(score,scor,10);
  353. if(score>hscore)
  354. {
  355. strcpy(hs,scor);
  356. setfillstyle(1,1);
  357. bar(maxx-140,337,maxx-20,348);
  358. setcolor(14);
  359. outtextxy(maxx-50,340,hs);
  360. outtextxy(maxx-140,340,"High Score ");
  361. if(first==0)
  362. {
  363. first=1;
  364. }
  365. }
  366. setcolor(4);
  367. outtextxy(maxx-50,250,scor);
  368. k++;
  369. nosound();
  370. }
  371. z=100;
  372. putimage(r1,r2,food,XOR_PUT);
  373. repr1:
  374. r1=random(450);
  375. if(r1<50)
  376. goto repr1;
  377. repr2:
  378. r2=random(400);
  379. if(r2<50)
  380. goto repr2;
  381.  
  382. f=0;
  383. }
  384.  
  385.  
  386. i++;
  387. X[i]=x;
  388. Y[i]=y;
  389.  
  390. b=a;
  391. if(kbhit())
  392. {
  393. rep:
  394. a=getche();
  395.  
  396. if((b=='6' && a=='4') || (b=='4' && a=='6'))
  397. a=b;
  398.  
  399. if((b=='8' && a=='5') || (b=='5' && a=='8'))
  400. a=b;
  401.  
  402. if(a!='6' && a!='4' && a!='8' && a!='0' && a!='5')
  403. a=b;
  404. }
  405.  
  406. /* if(x>maxx-170)
  407. x=0;
  408.  
  409.   if(y>maxy)
  410. y=0;*/
  411.  
  412. if(a=='8')
  413. y -= bo;
  414.  
  415. if(a=='5')
  416. y += bo;
  417.  
  418. if(a=='4')
  419. x -= bo;
  420.  
  421. if(a=='6')
  422. x += bo;
  423.  
  424. for(j=i+1; j<i-k; j--)
  425. {
  426. if((Y[i]>=Y[j] && Y[i]<=Y[j]+10) || (Y[i]<=Y[j] && Y[i]>=X[j]-10))
  427. {
  428. if((X[i]>=X[j] && X[i]<=X[j]+10) || (X[i]<=X[j] && X[i]>=X[j]-10))
  429. {
  430. a='0';
  431. }
  432.  
  433. outtextxy(maxx-140,360,"C ");
  434.  
  435.  
  436. }
  437.  
  438. if((X[i]>=X[j] && X[i]<=X[j]+10) || (X[i]<=X[j] && X[i]>=X[j]-10))
  439. {
  440. if((Y[i]>=Y[j] && Y[i]<=Y[j]+10) || (Y[i]<=Y[j] && Y[i]>=X[j]-10))
  441. {
  442. a='0';
  443. }
  444.  
  445. outtextxy(maxx-140,360,"C ");
  446. }
  447.  
  448. }
  449.  
  450. /* for(j=i; j<i-k; j--)
  451.   {
  452. if((y>=Y[j] && y<=Y[j]+10) || (y<=Y[j] && y>=Y[j]-10))
  453. {
  454. if((x>=X[j] && x<=X[j]+10) || (x<=X[j] && x>=X[j]-10))
  455. {
  456. a='5';
  457. }
  458. }
  459.  
  460. if((x>=X[j] && x<=X[j]+10) || (x<=X[j] && x>=X[j]-10))
  461. {
  462. if((y>=Y[j] && y<=Y[j]+10) || (y<=Y[j] && y>=Y[j]-10))
  463. {
  464. a='5';
  465. }
  466. }
  467. if(j==0)
  468. break;
  469.   } */
  470.  
  471.  
  472. /* if(x<=0)
  473. x=maxx-170;
  474.  
  475. if(y<=0)
  476. y=maxy;*/
  477.  
  478. if(maze==1)
  479. {
  480. if(x<=14 || x>=maxx-184 || y<=14 || y>=maxy-14)
  481. {
  482. a='0';
  483. }
  484. }
  485.  
  486. }
  487.  
  488. if(score>hscore)
  489. {
  490. ofstream onfile("c:\tc\bin\rattle.txt");
  491. onfile<<scor;
  492. onfile.close();
  493. }
  494.  
  495. setcolor(15);
  496. outtextxy(maxx-140,390," Gameover");
  497. sound(100);
  498. delay(400);
  499. nosound();
  500. getch();
  501. /* clean up */
  502. free(body);
  503. closegraph();
  504.  
  505. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:22: fatal error: graphics.h: No such file or directory
 #include <graphics.h>
                      ^
compilation terminated.
stdout
Standard output is empty