Add to Favorites    Make Home Page 14365 Online  
 Language Categories  
 Our Services  

Home » C Home » Games and Graphics Home » Snake game in graphics (Mini Project)

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Snake game in graphics (Mini Project)
Author Rajkumar
Author Email rajeni711 [at] yahoo.co.in
Description This is a snake program not yet fully completed
but we can play using arrow keys in the numberpad only(number lock should be unlock.It is a turbo c program.


Category C » Games and Graphics
Hits 384403
Code Select and Copy the Code
Code : #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <ctype.h> #include <dos.h> #include <time.h> void main() { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, ""); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s ", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } textscreen(); } /*IMPORTANT SECTION*/ FILE *fy,*fx; int i5,j5,i0,j0,x0[100],y0[100]; int f2,q0,q1,yu,q2,q3,q4,s1=0,a1,z,h=0,c1=1,c2=0,z1,t1,t2; char *msg[]={"","NEW ","LOAD ","SAVE","SPEED","LEVEL"}; char *msg1[]={"","(N)","(L)","(S)","(D)","(E)"}; char *msg2[]={"","START","STOP","EXIT","GAME SCORE"}; char *msg3[]={"","(T)","(P)","(X)"}; int r1[10],r2[10],r3[10],r4[10]; int speed,level; /////////////////////////////////////////////////////////// textscreen() { rect5(3,1,1,635,470); black(); initial(); rect5(0,430,100,620,450); rect5(0,10,100,80,442); rect6(); menu(0); return(0); } initial() { for(f2=1;f2<=5;f2++) { rect5(9,5+(126*(f2-1)),5,126*f2,70); citi(); } return(0); } //////////////////////////////////////////////// rect5( int q10, int q11, int q12, int q13, int q14) { setcolor(q10); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,q10); bar3d(q11,q12,q13,q14,1,1); q1=q11; q2=q12; q3=q13; q4=q14; return(0); } //////////////////////////////////////////////////// text1(a1) { setcolor(a1); settextstyle(0, 0, 2); outtextxy((f2*25)+((f2-1)*100)+f2*3,25,msg[f2]); setcolor(a1+3); settextstyle(0, 0, 1); outtextxy((f2*25)+((f2-1)*100)+f2*8,50,msg1[f2]); return(0); } citi () { if(s1==0) { r1[f2]=q1; r2[f2]=q2; r3[f2]=q3; r4[f2]=q4; rect5(1,r1[f2]+10,r2[f2]+10,r3[f2]-10,r4[f2]-10); a1=2; } else { a1=6; rect5(4,r1[f2],r2[f2],r3[f2],r4[f2]); rect5(15,r1[f2]+10,r2[f2]+10,r3[f2]-10,r4[f2]-10); } text1(a1); s1=0; return(0); } rect6() { for(f2=1;f2<=4;f2++) { rect5(6,12,102+((f2-1)*85),78,100+((f2)*85)); rect5(0,22,102+((f2-1)*85)+10,68,90+((f2)*85)); if(f2<=3) text2(4); } return(0); } rect7() { rect5(5,12,102+((f2-1)*85),78,100+((f2)*85)); rect5(2,22,102+((f2-1)*85)+10,68,90+((f2)*85)); text2(3); return(0); } text2(a1) { setcolor(a1); settextstyle(0, 0, 0); outtextxy(28,140+(f2-1)*80,msg2[f2]); setcolor(a1+1); settextstyle(0, 0, 0); outtextxy(34,150+(f2-1)*80,msg3[f2]); return(0); } menu(int y3) { L1:if(y3==0) z=getch(); else if(y3==1) z=83; else if(y3==2) z=80; if(c1==1) if((z==110)||(z==78)||(z==76)||(z==108)||(z==88)||(z==120)) goto L2; else goto L1; if((t1==78)||(t2==110)) if((z==80)||(z==112))goto L1; if((t1==76)||(t2==108)) if((z==68)||(z==100)||(z==69)||(z==101)||(z==80)||(z==112))goto L1; if((t1==83)||(t2==115)) if((z==68)||(z==100)||(z==69)||(z==101))goto L1; if((t1==68)||(t2==100)) if((z==80)||(z==112))goto L1; if((t1==69)||(t2==101)) if((z==80)||(z==112))goto L1; if((t1==84)||(t2==116)) if((z==68)||(z==100)||(z==69)||(z==101))goto L1; if((t1==80)||(t2==112)) if((z==68)||(z==100)||(z==69)||(z==101))goto L1; L2:rect6();initial(); switch(z) { case 110:case 78:s1=1;f2=1;citi();new1();break; case 76:case 108:s1=1;f2=2;citi();load1();break; case 83:case 115:s1=1;f2=3;citi(); if(y3==1) z=13; else save1(); y3=0; break; case 68:case 100:s1=1;f2=4;rect5(0,430,100,620,450);citi();speed1();break; case 69:case 101:s1=1;f2=5;rect5(0,430,100,620,450);citi();level1();break; case 84:case 116:f2=1;rect7();start1();break; case 80:case 112:f2=2;rect7();stop1();y3=0;break; case 88:case 120:f2=3;z=13;rect7();exit1();break; default:goto L1; } if(c2==1) { z=z1;c2=0; goto L2; } if(z!=13)goto L1; return(0); } new1() { L3:z1=getch(); switch(z1) { case 13:c2=0;c1=0;t1=78;t2=110;initial(); rect5(0,430,100,620,450); black(); setcolor(12);settextstyle(0,0,2); outtextxy(120,250,"Press T To Start"); outtextxy(450,120,"New Game"); break; case 76:case 108:case 88:case 120:c2=1;break; default:goto L3; } return(0); } load1() { L3:z1=getch(); switch(z1) { case 13:c2=0;c1=0;t1=76;t2=108;initial(); rect5(0,430,100,620,450); black(); setcolor(12);settextstyle(0,0,2); outtextxy(120,250,"Select the Number"); outtextxy(450,120,"Load Game"); load_file(); break; case 78:case 110:case 88:case 120:c2=1;break; default:goto L3; } return(0); } exit1() { L3:z1=getch(); switch(z1) { case 13:c2=0;c1=0;rect6(); initial();z=13;break; case 76:case 108:case 78:case 110:c2=1;break; default:goto L3; } return(0); } save1() { L3:z1=getch(); if((t1==78)||(t2==110)) { switch(z1) { case 13:c2=0;c1=0;initial();break; case 76:case 108:case 68:case 100:case 69:case 101:case 84:case 116: case 88:case 120:c2=1;break; default:goto L3; } } else { switch(z1) { case 13:c2=0;c1=0;t1=83;t2=115;initial();break; case 78:case 110:case 84:case 116:case 88:case 120:c2=1;break; default:goto L3; } } return(0); } speed1() { L3:z1=getch(); if((t1==78)||(t2==110)) { switch(z1) { case 13:c2=0;c1=0;t1=68;t2=100;black();setcolor(10);settextstyle(0,0,2); outtextxy(120,250,"Press T To Start"); outtextxy(120,230,"Select the Speed");speed_level();initial();break; case 76:case 108:case 83:case 115:case 69:case 101:case 84:case 116: case 88:case 120:c2=1;break; default:goto L3; } } else { switch(z1) { case 13:c2=0;c1=0;t1=68;t2=100;black();setcolor(10);settextstyle(0,0,2); outtextxy(120,230,"Press T To Start"); outtextxy(120,280,"Select the Speed");speed_level();initial();break; case 78:case 110:case 84:case 116:case 88:case 120:c2=1;break; default:goto L3; } } return(0); } level1() { L3:z1=getch(); if((t1==78)||(t2==110)) { switch(z1) { case 13:c2=0;c1=0;t1=69;t2=101;black();setcolor(14);settextstyle(0,0,2); outtextxy(120,230,"Press T To Start"); outtextxy(120,280,"Select the Level");speed_level();initial();break; case 76:case 108:case 83:case 115:case 68:case 100:case 84:case 116: case 88:case 120:c2=1;break; default:goto L3; } } else { switch(z1) { case 13:c2=0;c1=0;t1=69;t2=101;black();setcolor(14);settextstyle(0,0,2); outtextxy(120,230,"Press T To Start"); outtextxy(120,280,"Select the Level");speed_level();initial();break; case 78:case 110:case 84:case 116:case 88:case 120:c2=1;break; default:goto L3; } } return(0); } start1() { L3:z1=getch(); if((t1==78)||(t2==110)) { switch(z1) { case 13:c2=0;c1=0;t1=84;t2=116;rect6();initial();black();start2();z=84;break; case 76:case 108:case 68:case 100:case 69:case 101:case 83:case 115: case 88:case 120:c2=1;break; default:goto L3; } } else { switch(z1) { case 13:c2=0;c1=0;t1=84;t2=116;rect6();initial();start2();break; case 78:case 110:case 83:case 115:case 88:case 120:c2=1;break; default:goto L3; } } return(0); } stop1() { L3:z1=getch(); switch(z1) { case 13:c2=0;c1=0;t1=80;t2=112;rect6();black();initial();break; case 76:case 108:case 68:case 100:case 69:case 101:case 83:case 115: case 88:case 120:c2=1;break; default:goto L3; } return(0); } speed_level() { le_se(); choose_le_se(); return(0); } le_se() { int i1; for(i1=0;i1<9;i1++) { le_se_box(i1,6,8); le_se_write(i1); } return(0); } le_se_box(int i1,int c1,int c2) { rect5(c1,430,110+i1*38,620,138+i1*38); rect5(c2,435,115+i1*38,615,133+i1*38); settextstyle(8,0,2); return(0); } le_se_write(int i1) { char *m; if(f2==5) { setcolor(14); outtextxy(460,108+i1*38,"LEVEL "); } else { setcolor(10); outtextxy(460,108+i1*38,"SPEED "); } itoa(i1+1,m,10); outtextxy(570,108+i1*38,m); return(0); } choose_le_se() { int w; O1:w=getch(); w-=48; le_se(); if((w>=1)&&(w<=9)) { le_se_box(w-1,7,9); le_se_write(w-1); if(f2==4) speed=w; else level=w; goto O1; } else { w+=48; if(w==13) { le_se(); return(0); } else goto O1; } } ////////////////////////////////// int e1,xa,i1=1,h1,j1=0,na,cn[50],rn,a1,m1,w1=0; char *xn[]={"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P", "Q","R","S","T","U","V","W","X","Y","Z"}; char *mn[]={"0","1","2","3","4","5","6","7","8","9"}; char *sa; struct file { int count; char name[50]; }sn[100],sm; cur() { rect5(0,430,100,620,450); L8:i1=1;*sa=NULL; setcolor(RED); settextstyle(0, 0, 0); outtextxy(450,340,"Enter your name"); setcolor(10); L6:if(i1==7)goto L7; na=getch(); if((na<=122)&&(na>=97)) { cn[i1]=na-96; outtextxy(450+i1*8,350, xn[na-96]); strcat(sa,xn[na-96]); i1++; goto L6; } else if((na!=13)||(i1<=3))goto L6; L7:rect5(0,430,100,620,450); rn=file_new(); if(rn==0)goto L8; setcolor(12); settextstyle(1,0,2); outtextxy(455,120,"Game Name"); line(455,145,574,145); line(455,148,574,148); outtextxy(458,160,sa); setcolor(9); if(a1>=10) { w1=a1%10; outtextxy(495,280,mn[w1]); a1/=10; } outtextxy(455,240,"Your No"); line(455,270,550,270); line(455,273,550,273); outtextxy(480,280,mn[a1]); return(0); } load_file() { if((fx=fopen("game.value","r"))==NULL) { fclose(fx); rect5(0,430,100,620,450); black(); setcolor(12);settextstyle(0,0,2); outtextxy(120,250,"Press N & Enter"); } else { j1=0; fx=fopen("game.value","r"); while(!feof(fx)) { j1++; fscanf(fx,"%s ",sm.name); strcpy(sn[j1].name,sm.name); } fclose(fx); for(i1=1;i1<=j1;i1++) { setcolor(3); settextstyle(0,0,0); if(i1<10) outtextxy(462,130+i1*12,mn[i1]); else { w1=i1/10; outtextxy(452,130+i1*12,mn[w1]); w1=i1%10; outtextxy(462,130+i1*12,mn[(i1%10)]); } outtextxy(480,130+i1*12,sn[i1].name); } } return(0); } file_new() { if((fx=fopen("game.value","r"))==NULL) { strcpy(sm.name,sa); strcat(sm.name,".snake"); fclose(fy); fx=fopen("game.value","w"); fprintf(fx,"%s ",sm.name); strcpy(sn[1].name,sm.name); a1=1; fclose(fx); } else { j1=0; fx=fopen("game.value","r"); while(!feof(fx)) { j1++; fscanf(fx,"%s ",sm.name); strcpy(sn[j1].name,sm.name); } a1=j1+1; strcpy(sn[a1].name,sa); strcat(sn[a1].name,".snake"); fclose(fx); for(j1=1;j1<a1;j1++) { xa=strcmp(sn[a1].name,sn[j1].name); if(xa==0) { setcolor(9); outtextxy(452,120,"Name Already Exist"); return(0); } } } strcpy(sm.name,sn[a1].name); fx=fopen("game.value","a"); fprintf(fx,"%s ",sm.name); fclose(fx); strcpy(sa,sn[a1].name); return(1); } ////////////////////////////////////////////////////////// // SECTION - II int i=0,j=0,d,e=0,k,a,ll=3,b,p=0,t=0,g=0; int x[100],y[100],m=0,r,x1[100],y1[100]; clock_t start,end; char *bn; float thf; int thin,thi[100],nm,sc; start2() { i=0;j=0;e=0;p=0;t=0,g=0,sc=0; m=0; rect5(0,430,100,620,450); outtextxy(450,120,"New Game"); randomize(); black(); func1(); continue1(); level_selection(); for(m=1;m<=50;m++) { start=clock(); func1(); a=i;b=j; move(); if(t==1) { m=50; t=0; } end=clock(); rect5(0,430,300,620,450); thf= (end-start) / CLK_TCK; thin=(int)thf; thi[m]=20-thin; setcolor(10); settextstyle(6,0,2); sc+=thi[m]*10; itoa(sc,bn,10); outtextxy(450,350,"GAME SCORE"); setcolor(13); outtextxy(500,400,bn); nm=m; } return(0); } /*Function 1* to create, draw & assign the Bar postion values*/ func1() { e++; create(); rect1(i,j); x[e]=i; y[e]=j; return(0); } /*Function 2 * To fix the initial & Destination*/ create() { int f; l1:if(m!=0) { i=rand1(29)+1; j=rand1(29)+1; for(i5=1;i5<=(level-1)*10;i5++) if((x0[i5]==i)&&(y0[i5]==j))goto l1; } else { i=rand1(25)+3; j=rand1(25)+3; } if(e==(ll+1+m)) { for(f=1;f<=ll+m;f++) { if((i==x[f])||(j==y[f])) goto l1; } } return(0); } /*Function 3 * to draw the bar3d in Blinking color*/ rect1(int i1,int j1) { setcolor(2+128); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,2+128); bar3d(100+(j1*10),100+(i1*10),110+(j1*10),110+(i1*10),0,0); setfillstyle(SOLID_FILL,12+128); bar3d(100+(j1*10)+2,100+(i1*10)+2,110+(j1*10)-2,110+(i1*10)-2,1,1); return(0); } /*Function 4 * to draw the bar3d in green color*/ rect2(int i1,int j1) { setcolor(2); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,2); bar3d(100+(j1*10),100+(i1*10),110+(j1*10),110+(i1*10),0,0); setfillstyle(SOLID_FILL,10); bar3d(100+(j1*10)+2,100+(i1*10)+2,110+(j1*10)-2,110+(i1*10)-2,1,1); return(0); } /*Function 4a*/ rect4(int i1,int j1) { setcolor(0); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,0); bar(100+(j1*10),100+(i1*10),110+(j1*10),110+(i1*10)); setfillstyle(SOLID_FILL,0); bar(100+(j1*10)+2,100+(i1*10)+2,110+(j1*10)-2,110+(i1*10)-2); return(0); } /*Function 5 * to draw the bar3d in Black color*/ rect3(int i1,int j1) { setcolor(0); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,0); bar3d(100+(j1*10),100+(i1*10),110+(j1*10),110+(i1*10),0,0); setfillstyle(SOLID_FILL,0); bar3d(100+(j1*10)+2,100+(i1*10)+2,110+(j1*10)-2,110+(i1*10)-2,1,1); return(0); } /* Function 6 * to select a random number*/ rand1(int f) { return( rand () % f); } /* Function 7 * to draw the next to the initial */ continue1() { r=rand1(3)+1; l3:direction();e++; x[e]=i;y[e]=j;rect2(i,j); if(e<ll)goto l3; return(0); } /*Function 8 * to set the direction */ direction() { switch(r) { case 1: j+=1;break; case 2: i+=1;break; case 3: j-=1;break; case 4: i-=1;break; } return(0); } /*function 9 * to move bar array */ move() { int s=0; l5:k=getch(); if((g==1)&&((k==83)||(k==115)))goto l5; delay(10); switch(k) { case 56:r=4;s=loop();break; case 54:r=1;s=loop();break; case 52:r=3;s=loop();break; case 50:r=2;s=loop();break; case 80:case 112:s=0;t=1;menu(2);break; case 83:case 115:s=1;g=1;menu(1);cur();initial();break; default:s=1; } if(s==1)goto l5; return(0); } /* Function 10 * Do array loop to move */ loop() { int s; for(d=1;d<=e;d++) { s=move1(d); if (s==0) if(p!=2) d=e; } return(s); } /*Function 11 * to move according to direction */ move1(int f) { int s, f1; x1[f]=x[f];y1[f]=y[f]; if(f==1) { i=x[f];j=y[f];direction(); s=check1(); if (s==0) { for(f1=1;f1<=e;f1++) rect_red(x[f1],y[f1]); black1(); return(s); } else { x[f]=i;y[f]=j; if(i==0 || j==0 || i==31 || j==31) { for(f1=2;f1<e;f1++) rect_red(x[f1],y[f1]); black1(); t=1; return(0); } rect1(i,j); s=check2(); if (s==0) return(s); } } else { i=x1[f-1];j=y1[f-1]; if(i==0 || j==0 || i==31 || j==31) { for(f1=2;f1<e;f1++) rect_red(x[f1],y[f1]); black1(); t=1; return(0); } rect2(i,j); x[f]=i;y[f]=j; if((p==2)&&(f==e)) { rect2(x[f],y[f]); p=1; return(0); } else { if(f==e) rect3(i,j); } } return(1); } /* Function 11 * to check the changed value */ check1() { int f1; for(f1=2;f1<(e-1);f1++) { if((i==x[f1])&&(j==y[f1])) { t=1; return(0); } else { for(i5=1;i5<=(level-1)*10;i5++) if((x0[i5]==x[1])&&(y0[i5]==y[1])) { t=1; return(0); } } } return(1); } /*Function 12 * to check the final value */ check2() { if((i==a)&&(j==b)) { p=2; return(0); } else return(1); } black() { setcolor(9); setlinestyle(SOLID_LINE,1,3); rectangle(107,107,413,413); for(i=1;i<=30;i++) for(j=1;j<=30;j++) rect4(i,j); return(0); } black1() { setcolor(9); setlinestyle(SOLID_LINE,1,3); rectangle(107,107,413,413); for(i=1;i<=30;i++) { for(j=1;j<=30;j++) rect_white(i,j); delay(10); } for(i=1;i<=30;i++) { for(j=1;j<=30;j++) rect4(i,j); delay(1); setcolor(5); settextstyle(4,0,6); outtextxy(135,175,"G A M E"); outtextxy(135,250,"O V E R"); } return(0); } rect_red(int i1,int j1) { setcolor(5); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,5); bar3d(100+(j1*10),100+(i1*10),110+(j1*10),110+(i1*10),0,0); setfillstyle(SOLID_FILL,12+128); bar3d(100+(j1*10)+2,100+(i1*10)+2,110+(j1*10)-2,110+(i1*10)-2,1,1); return(0); } rect_white(int i1,int j1) { setcolor(5); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,5); bar(100+(j1*10),100+(i1*10),110+(j1*10),110+(i1*10)); setfillstyle(SOLID_FILL,5); bar(100+(j1*10)+2,100+(i1*10)+2,110+(j1*10)-2,110+(i1*10)-2); return(0); } level_selection() { /*switch (level) { case 1:loop_draw();break; case 2: } */ loop_draw(); return(0); } loop_draw() { for(i5=1;i5<=(level-1)*10;i5++) { k1:i0=rand1(29)+1; j0=rand1(29)+1; for(j5=1;j5<=4;j5++) if((x[j5]==i0)&&(y[j5]==j0))goto k1; rect_blue(i0,j0); x0[i5]=i0;y0[i5]=j0; } return(0); } rect_blue(int i1,int j1) { setcolor(9); setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,9); bar(100+(j1*10),100+(i1*10),110+(j1*10),110+(i1*10)); setfillstyle(SOLID_FILL,8); bar(100+(j1*10)+2,100+(i1*10)+2,110+(j1*10)-2,110+(i1*10)-2); return(0); }

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi �դ ?�� ��??���
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=909


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review