5 Contoh Game Sederhana Pada Bahasa C
Halo sobat taugakini balik lagi sama saya eko wardani, kali ini saya akan l sebuah sampel atau contoh sederhana dari sebuah kode pemrograman C
Untuk kalian yang sedang belajar menggunakan bahasa pemrograman c dan sedang bingung bagaimana cara mempraktikannya.
Kalian bisa melihat contoh kode game c atau sebuah kode pemrograman dari bahasa C yang diaplikasikan menjadi sebuah game sederhana.
Nah untuk kode kodenya saya akan memberikan beberapa contohnya
1. Archery Game
Untuk kalian yang suka dengan game memanah kalian bisa mencoba kode berikut ini :
Archery Game di C
ARCHERY GAME
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
union REGS i,o;
int f;
struct arccoordstype arcinfo;
int t,flag=0;
int
gm=0,gd=DETECT,*button,*a,*b,c,bc,j,k,jk,h,x3,y3,alh=0,s,scor=0,sp=0,e,m=2
;
main()
{
//initgraph(&gm,&gd," ");
front();
closegraph();
//delay(10);
//sta();
a:
initgraph(&gd,&gm," ");
flag=0;
setbkcolor(0);
s=-60;e=60;
sound(221.38);
for(f=1;f<=20;f++)
{
setcolor(BLUE);
line(280,f+360,360,f+360);
}
setcolor(RED);
settextstyle(SMALL_FONT,HORIZ_DIR,4);
outtextxy(305,364,"SHOOT");
arrows(0);
setcolor(GREEN+WHITE);
rectangle(5,5,634,474);
rectangle(6,6,633,473);
setcolor(YELLOW);
rectangle(7,7,632,472);
setcolor(GREEN+WHITE);
rectangle(8,8,631,471);
rectangle(9,9,630,470);
rectangle(10,10,629,469);
setcolor(YELLOW);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(530,20,"SCORE");
scor=0;
score(200);
m=6;
equip();
arrow1(0,14);
alh=0;
mousereg();
while(m>1) /* while start */
{
//button=0;
for(j=-70;j<=70;j+=2) /* start for*/
{ c=1;
tar(j,c);
delay(10);
getmouseptr(&button,&a,&b);
if(button==1)
{ x3=a;y3=b;
if((x3>280 && x3<360 )|| (y3>361 && y3<379))
{
bc=1;
}
}
if(bc==1)
{ //sound(277.38);
// delay(10);
nosound();
arrow1(0,0);
setcolor(YELLOW);
line(40+sp+36,240,80+sp+36,240);
line(80+sp+36,240,75+sp+36,235);
line(80+sp+36,240,75+sp+36,245);
line(40+sp+36,241,80+sp+36,241);
line(79+sp+36,240,74+sp+36,236);
line(79+sp+36,241,74+sp+36,245);
line(46+sp+36,240,41+sp+36,236);
line(46+sp+36,241,41+sp+36,245);
line(42+sp+36,240,37+sp+36,236);
line(42+sp+36,241,37+sp+36,245);
delay(8);
setcolor(0);
line(40+sp+36,240,80+sp+36,240);
line(80+sp+36,240,75+sp+36,235);
line(80+sp+36,240,75+sp+36,245);
line(40+sp+36,241,80+sp+36,241);
line(79+sp+36,240,74+sp+36,236);
line(79+sp+36,241,74+sp+36,245);
line(46+sp+36,240,41+sp+36,236);
line(46+sp+36,241,41+sp+36,245);
line(42+sp+36,240,37+sp+36,236);
line(42+sp+36,241,37+sp+36,245);
if((42+sp+36)>=116 && (42+sp+36)<=136)
{
equip();
}
sp=sp+3;
}
if((sp+80+36)>=579)
{ arrow1(0,14);
score(j);
bc=0; sp=0;
alh++;
button=0;
setcolor(YELLOW);
arrows(alh);
m--;
}
// sp=sp+3;
c=0;
tar(j,c);
} /* for end */
}/* while end */
sleep(1.5);
closegraph();
gaover();
setcolor(GREEN);
setcolor(1);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,4);
outtextxy(255,117,"SCORE");
setcolor(14+BLUE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,3);
for(t=0;t<=360;t++)
{ setcolor(random(15));
ellipse(300,200,t,t+2,30,22);
delay(1);
setcolor(random(15));
ellipse(300,200,t,t+2,35,25);
}
setcolor(GREEN);
select(scor,-250,133);
for(t=0;t<=50;t++)
{
setcolor(14);
line(40+t,230,40+t,250);
line(560+t,230,560+t,250);
}
settextstyle(SMALL_FONT,HORIZ_DIR,4);
setcolor(BLUE);
outtextxy(45,235,"Continue");
outtextxy(575,235,"Quit");
restrictmouseptr(0,80,640,440);
flag=0;
while(flag==0)
{
showmouseptr();
getmouseptr(&button,&a,&b);
if(button==1)
{ x3=a;y3=b;
if((x3>41 && x3<79 )&& (y3>231 && y3<249))
{
flag=1;
//break;
}
else if((x3>=561 && x3<=599 )&& (y3>=231 && y3<=249))
{
flag=2;
//break;
}
}
}
if(flag==1)
goto a;
else
{if(flag==2)
{initgraph(&gd,&gm,"");
finalwindow();
closegraph();
goto b;}}
// next();
b: getch();
}//ending of main function
/* sub function target starting*/
arrow1(int sp,int col)
{ setcolor(col); //sound(277.38);
line(40+sp+36,240,80+sp+36,240);
line(80+sp+36,240,75+sp+36,235);
line(80+sp+36,240,75+sp+36,245);
line(40+sp+36,241,80+sp+36,241);
line(79+sp+36,240,74+sp+36,236);
line(79+sp+36,241,74+sp+36,245);
line(46+sp+36,240,41+sp+36,236);
line(46+sp+36,241,41+sp+36,245);
line(42+sp+36,240,37+sp+36,236);
line(42+sp+36,241,37+sp+36,245);
nosound();
}
tar(j,c)
{
setcolor(c*4);
ellipse(579,240+j,0,360,40,90);
setcolor(c*3);
ellipse(579,240+j,0,360,33,75);
setcolor(c*5);
ellipse(579,240+j,0,360,26,60);
setcolor(c*9);
ellipse(579,240+j,0,360,19,45);
setcolor(c*8);
ellipse(579,240+j,0,360,12,30);
setcolor(c*10);
ellipse(579,240+j,0,360,5,15);
}
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
getmouseptr(int *button,int *a,int *b)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*a=o.x.cx;
*b=o.x.dx;
}
mousereg()
{
int x1,x2,y1,y2,ce,k;
initmouse();
showmouseptr();
restrictmouseptr(281,361,359,379);
}
arrows(alh)
{ int z=0,r;
r=5-alh;
setcolor(YELLOW);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(30,20,"ARROWS REMAINING");
for(f=r,z=0;f>0;f--)
{
line(40,60+z*10,80,60+z*10);
line(80,60+z*10,75,55+z*10);
line(80,60+z*10,75,65+z*10);
z=z+1;
}
for(f=alh,z=0;f>0;f--)
{
setcolor(0);
line(40,60+z*10,80,60+z*10);
line(80,60+z*10,75,55+z*10);
line(80,60+z*10,75,65+z*10);
z=z+1;
}
}
score (int j)
{
if( (j>=-90 && j<=-74) || (j>=74 && j<=90))
scor=scor+10;
else if((j>=-72 && j<=-60) || (j>=60 && j<=72))
scor=scor+20;
else if((j>=-58 && j<=-42) || (j>=42 && j<=58))
scor=scor+30;
else if((j>=-40 && j<=-32) || (j>=32 && j<=40))
scor=scor+40;
else if((j>=-30 && j<=-18) || (j>=18 && j<=30))
scor=scor+50;
else if((j>=-16 && j<=0) || (j>=0 && j<=16))
scor=scor+60;
else if (j == 300)
scor=0;
for(jk=50;jk<=120;jk++)
{setcolor(0);
line(530,jk,620,jk);
}
setcolor(YELLOW);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
select(scor,0,0);
}
front1()
{
int gm=0,gd=DETECT,i,j,k,jk,gh;
initgraph(&gd,&gm," ");
setcolor(MAGENTA);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
for(i=12;i<=627;i+=12)
{
setcolor(YELLOW);
line(i,12,i,467);
//clearviewport();
for(j=15;j<=464;j+=5)
ellipse(i+6,j,-180,0,6,4);
}
for(i=3;i<=452;i+=5)
{ for(j=15;j<=627;j+=3)
{setcolor(0);
ellipse(j,467-i,-180,0,3,4);
}delay(40);
setcolor(BLACK);
for(k=1;k<=5;k++)
{
line(12,467-i+k,637,467-i+k); }
//setcolor(25);
setcolor(GREEN+MAGENTA+WHITE+BLUE+YELLOW);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
}
setcolor(2);
rectangle(290,370,350,380);
rectangle(292,372,348,378);
gh=292; //gh<=388;
for(j=1;j<=56;j+=1)
{ /* for(i=24;i<=627;i+=12)
{setcolor(i);
circle(i,23,3);
delay(1);
} */ jk=20;
for(i=24;i<=627;i+=12)
{setcolor(random(15));
circle(i,23,3);
sound(440+jk);
delay(10);
nosound();
setcolor(YELLOW);
rectangle(gh,372,gh,378);
//rectangle(282,362,358,388);}
jk+=40;
if(jk>=880)
{ jk=440;}
}
gh=gh+1;
nosound();
}
sleep(.1);
closegraph();
}
equip()
{
int kl=2;
int midx, midy,i,j,k,y=90,c;
// int stangle = 45, endangle = 270;
c=y/2;
for(c=50;c<=52;c+=1)
{ setcolor(kl);
line(26+c,170,26+c,310);
}
for(c=50;c<=54;c+=1)
{ setcolor(kl);
line(60+c,222,60+c,255);
}
for(c=180;c<=270;c++)
{
// arc(80,92,c,c+1,20);
setcolor(0);
arc(86,166,c,c+1,10);
getarccoords(&arcinfo);
setcolor(kl);
circle(arcinfo.xend, arcinfo.yend,2);
}
for(c=-50;c<=110;c++)
{
// arc(80,92,c,c+1,20);
setcolor(0);
arc(96,201,c,c+1,28);
getarccoords(&arcinfo);
setcolor(kl);
circle(arcinfo.xend, arcinfo.yend,2);
}
for(c=50,i=4;c>=-110;c--)
{
// arc(80,92,c,c+1,20);
setcolor(0);
line(127,170,127,315);
line(126,170,126,315);
arc(96,278,c,c+1,28);
getarccoords(&arcinfo);
setcolor(kl);
i=i+2;
if(i>14)
{i=2;}
circle(arcinfo.xend,arcinfo.yend,2);
}
for(c=90;c<=180;c++)
{
// arc(80,92,c,c+1,20);
setcolor(0);
arc(86,314,c,c+1,10);
getarccoords(&arcinfo);
setcolor(kl);
circle(arcinfo.xend, arcinfo.yend,2);
//setcolor(2);
}
}
sta()
{
int x,y=200,i,st,kl;
//initgraph(&gd,&gm,"");
setcolor(5);
for(st=190,i=2;st>=-20;st-=9,i+=3)
{
setcolor(GREEN);
arc(320,290,st,st+2,190);
getarccoords(&arcinfo);
delay(5);
x=arcinfo.xend;
y=arcinfo.yend;
sound(300-st);
star(x,y);
delay(24);
}
for(st=190,i=2;st>=-17;st-=9,i+=3)
{
setcolor(YELLOW);
arc(320,285,st,st+2,230);
getarccoords(&arcinfo);
delay(5);
x=arcinfo.xend;
y=arcinfo.yend;
sound(320-st);
star(x,y);
delay(24);
}
nosound();
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
for(i=140;i>=20;i-=10)
{
setcolor(8);
delay(13);
ya(i);
delay(13);
setcolor(4);
r(i);
delay(13);
setcolor(2);
e1(i);
delay(13);
h1(i);
delay(13);
c1(i);
delay(13);
r1(i);
delay(13);
a1(i);
delay(13);
/*line(320,240,325,233); // /1
line(325,233,330,240); // 1
line(330,240,337,240); // right hori
line(320,240,313,240); //left hori
line(313,240,318,247); // left horiz to right
line(337,240,332,247); // right horiz to left /
line(332,247,334,253); // left / to right
line(318,247,316,253); // right to left /
line(316,253,325,247);
line(334,253,325,247);*/
// getch();
}
}
ya(int i)
{
if(i==120)
{
delay(1);
setcolor(4);
// delay(800);
outtextxy(300+i,230,"Y");
}setcolor(14);
delay(1);
outtextxy(420,230,"Y"); //440
}
r(int i)
{
if(i==90)
{
setcolor(2);
delay(1);
outtextxy(300+i,230,"R");
}setcolor(14);
delay(1);
outtextxy(390,230,"R"); // 420
}
e1(int i)
{
if(i==60)
{
setcolor(2);
delay(1);
outtextxy(300+i,230,"E");
}setcolor(14);
delay(1);
outtextxy(360,230,"E"); //400
}
h1(int i)
{
if(i==30)
{
setcolor(2);
delay(1);
outtextxy(300+i,230,"H");
}setcolor(14);
delay(1);
outtextxy(330,230,"H"); //380
}
c1(int i)
{
if(i==60)
{
setcolor(2);
delay(1);
outtextxy(300+i-i,230,"C");
}setcolor(14);
delay(1);
outtextxy(300,230,"C"); //360
}
r1(int i)
{
if(i==40)
{
setcolor(2);
delay(1);
outtextxy(300+i-70,230,"R");
}setcolor(14);
delay(1);
outtextxy(270,230,"R"); // 340
}
a1(int i)
{
if(i==20)
{
setcolor(2);
delay(1);
outtextxy(300+i-80,230,"A");
}setcolor(14);
delay(1);
outtextxy(240,230,"A"); //320
}
star(x,y)
{
delay(80);
line(x,y,x+5,y-7); // /1 x=320, y=240
line(x+5,y-7,x+10,y); // 1
line(x+10,y,x+17,y); // right hori
line(x,y,x-7,y); //left hori
line(x-7,y,x-2,y+7); // left horiz to right
line(x+17,y,x+12,y+7); // right horiz to left /
line(x+12,y+7,x+14,y+13); // left / to right
line(x-2,y+7,x-4,y+13); // right to left /
line(x-4,y+13,x+5,y+7);
line(x+14,y+13,x+5,y+7);
}
front()
{
int gm=0,gd=DETECT,i,j,k,jk,gh;
initgraph(&gd,&gm," ");
setcolor(MAGENTA);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
for(i=12;i<=627;i+=12)
{
setcolor(YELLOW);
line(i,12,i,467);
//clearviewport();
for(j=15;j<=464;j+=5)
ellipse(i+6,j,-180,0,6,4);
}
for(i=3;i<=452;i+=5)
{ for(j=15;j<=627;j+=3)
{setcolor(0);
ellipse(j,467-i,-180,0,3,4);
}delay(40);
setcolor(BLACK);
for(k=1;k<=5;k++)
{
line(12,467-i+k,637,467-i+k); }
//setcolor(25);
setcolor(GREEN+MAGENTA+WHITE+BLUE+YELLOW);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
}
setcolor(2);
rectangle(290,370,350,380);
rectangle(292,372,348,378);
gh=292; //gh<=388;
for(j=1;j<=56;j+=1)
{ /* for(i=24;i<=627;i+=12)
{setcolor(i);
circle(i,23,3);
delay(1);
} */ jk=20;
for(i=24;i<=627;i+=12)
{setcolor(random(15));
circle(i,23,3);
sound(200-i);
delay(1);
nosound();
setcolor(YELLOW);
rectangle(gh,372,gh,378);
//rectangle(282,362,358,388);}
jk+=40;
if(jk>=880)
{ jk=440;}
}
gh=gh+1;
nosound();
}
//printf("sdfs");
sta();
sleep(.1);
//closegraph();
}
gaover()
{
int gm=0,gd=DETECT,i,j;
initgraph(&gd,&gm," ");
for(i=1;i<=51;i+=4)
{
setcolor(i);
sound(400-i);
outtextxy(260,340,"GAME OVER");
if(i==49)
{
setcolor(YELLOW);
outtextxy(260,340,"GAME OVER");
}
nosound();
go(0,60,20,80,random(15),random(15));
delay(3);
go(400,460,420,480,random(15),random(15));
}
//getch();
}
go(int sy1,int ey1,int sy2,int ey2,int k,int h)
{
int i,j;
for(j=sy1;j<=ey1;j+=40) //120
{
for(i=0;i<=640;i+=40)
{
setcolor(k);
rectangle(i,1+j,20+i,21+j);
setfillstyle(SOLID_FILL,k);
floodfill(10+i,10+j,k);
setcolor(h);
rectangle(20+i,1+j,40+i,21+j);
setfillstyle(SOLID_FILL,h);
floodfill(25+i,10+j,h);
}
}
for(j=sy2;j<=ey2;j+=40)
{
for(i=0;i<=640;i+=40)
{
setcolor(h);
rectangle(i,1+j,20+i,21+j);
setfillstyle(SOLID_FILL,h);
floodfill(10+i,10+j,h);
setcolor(k);
rectangle(20+i,1+j,40+i,21+j);
setfillstyle(SOLID_FILL,k);
floodfill(25+i,10+j,k);
}
}
//
}
select(int scor,int p,int q)
{
switch(scor)
{ case 0: outtextxy(530+p,50+q,"0");
break;
case 10: outtextxy(530+p,50+q,"10");
break;
case 20: outtextxy(530+p,50+q,"20");
break;
case 30: outtextxy(530+p,50+q,"30");
break;
case 40: outtextxy(530+p,50+q,"40");
break;
case 50: outtextxy(530+p,50+q,"50");
break;
case 60: outtextxy(530+p,50+q,"60");
break;
case 70: outtextxy(530+p,50+q,"70");
break;
case 80: outtextxy(530+p,50+q,"80");
break;
case 90: outtextxy(530+p,50+q,"90");
break;
case 100: outtextxy(530+p,50+q,"100");
break;
case 110: outtextxy(530+p,50+q,"110");
break;
case 120: outtextxy(530+p,50+q,"120");
break;
case 130: outtextxy(530+p,50+q,"130");
break;
case 140: outtextxy(530+p,50+q,"140");
break;
case 150: outtextxy(530+p,50+q,"150");
break;
case 160: outtextxy(530+p,50+q,"160");
break;
case 170: outtextxy(530+p,50+q,"170");
break;
case 180: outtextxy(530+p,50+q,"180");
break;
case 190: outtextxy(530+p,50+q,"190");
break;
case 200: outtextxy(530+p,50+q,"200");
break;
case 210: outtextxy(530+p,50+q,"210");
break;
case 220: outtextxy(530+p,50+q,"220");
break;
case 230: outtextxy(530+p,50+q,"230");
break;
case 240: outtextxy(530+p,50+q,"240");
break;
case 250: outtextxy(530+p,50+q,"250");
break;
case 260: outtextxy(530+p,50+q,"260");
break;
case 270: outtextxy(530+p,50+q,"270");
break;
case 280: outtextxy(530+p,50+q,"280");
break;
case 290: outtextxy(530+p,50+q,"290");
break;
case 300: outtextxy(530+p,50+q,"300");
break;
case 310: outtextxy(530+p,50+q,"310");
break;
case 320: outtextxy(530+p,50+q,"320");
break;
case 330: outtextxy(530+p,50+q,"330");
break;
case 340: outtextxy(530+p,50+q,"340");
break;
case 350: outtextxy(530+p,50+q,"350");
break;
case 360: outtextxy(530+p,50+q,"360");
break;
} }
flower(int i,int j)
{
setcolor(14);
circle(320+i,240+j,10);
setfillstyle(SOLID_FILL,RED);
floodfill(323+i,241+j,14);
//1
setcolor(14);
//floodfill(316,223,13);
ellipse(310+i,220+j,90,-90,6,12);
ellipse(330+i,220+j,-90,90,6,12);
ellipse(320+i,207+j,180,0,8,2);
//2
ellipse(310+i,260+j,90,-90,6,12);
ellipse(330+i,260+j,-90,90,6,12);
ellipse(320+i,273+j,0,180,8,2);
//3
ellipse(298+i,248+j,180,0,12,6);
ellipse(298+i,233+j,0,180,12,6);
ellipse(286+i,240+j,-90,90,2,8);
//4
ellipse(342+i,247+j,180,0,12,6);
ellipse(342+i,233+j,0,180,12,6);
ellipse(354+i,240+j,90,-90,2,8);
}
finalwindow()
{
for(k=-285;k<=320;k+=70)
{ flower(k,-205);
flower(k,+180);
//clearviewport();
delay(100);
}
for(k=-140;k<=180;k+=40)
{ flower(-285,k);
flower(276,k);
//clearviewport();
delay(100);
}
setcolor(2);
sleep(4);
}
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
union REGS i,o;
int f;
struct arccoordstype arcinfo;
int t,flag=0;
int
gm=0,gd=DETECT,*button,*a,*b,c,bc,j,k,jk,h,x3,y3,alh=0,s,scor=0,sp=0,e,m=2
;
main()
{
//initgraph(&gm,&gd," ");
front();
closegraph();
//delay(10);
//sta();
a:
initgraph(&gd,&gm," ");
flag=0;
setbkcolor(0);
s=-60;e=60;
sound(221.38);
for(f=1;f<=20;f++)
{
setcolor(BLUE);
line(280,f+360,360,f+360);
}
setcolor(RED);
settextstyle(SMALL_FONT,HORIZ_DIR,4);
outtextxy(305,364,"SHOOT");
arrows(0);
setcolor(GREEN+WHITE);
rectangle(5,5,634,474);
rectangle(6,6,633,473);
setcolor(YELLOW);
rectangle(7,7,632,472);
setcolor(GREEN+WHITE);
rectangle(8,8,631,471);
rectangle(9,9,630,470);
rectangle(10,10,629,469);
setcolor(YELLOW);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(530,20,"SCORE");
scor=0;
score(200);
m=6;
equip();
arrow1(0,14);
alh=0;
mousereg();
while(m>1) /* while start */
{
//button=0;
for(j=-70;j<=70;j+=2) /* start for*/
{ c=1;
tar(j,c);
delay(10);
getmouseptr(&button,&a,&b);
if(button==1)
{ x3=a;y3=b;
if((x3>280 && x3<360 )|| (y3>361 && y3<379))
{
bc=1;
}
}
if(bc==1)
{ //sound(277.38);
// delay(10);
nosound();
arrow1(0,0);
setcolor(YELLOW);
line(40+sp+36,240,80+sp+36,240);
line(80+sp+36,240,75+sp+36,235);
line(80+sp+36,240,75+sp+36,245);
line(40+sp+36,241,80+sp+36,241);
line(79+sp+36,240,74+sp+36,236);
line(79+sp+36,241,74+sp+36,245);
line(46+sp+36,240,41+sp+36,236);
line(46+sp+36,241,41+sp+36,245);
line(42+sp+36,240,37+sp+36,236);
line(42+sp+36,241,37+sp+36,245);
delay(8);
setcolor(0);
line(40+sp+36,240,80+sp+36,240);
line(80+sp+36,240,75+sp+36,235);
line(80+sp+36,240,75+sp+36,245);
line(40+sp+36,241,80+sp+36,241);
line(79+sp+36,240,74+sp+36,236);
line(79+sp+36,241,74+sp+36,245);
line(46+sp+36,240,41+sp+36,236);
line(46+sp+36,241,41+sp+36,245);
line(42+sp+36,240,37+sp+36,236);
line(42+sp+36,241,37+sp+36,245);
if((42+sp+36)>=116 && (42+sp+36)<=136)
{
equip();
}
sp=sp+3;
}
if((sp+80+36)>=579)
{ arrow1(0,14);
score(j);
bc=0; sp=0;
alh++;
button=0;
setcolor(YELLOW);
arrows(alh);
m--;
}
// sp=sp+3;
c=0;
tar(j,c);
} /* for end */
}/* while end */
sleep(1.5);
closegraph();
gaover();
setcolor(GREEN);
setcolor(1);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,4);
outtextxy(255,117,"SCORE");
setcolor(14+BLUE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,3);
for(t=0;t<=360;t++)
{ setcolor(random(15));
ellipse(300,200,t,t+2,30,22);
delay(1);
setcolor(random(15));
ellipse(300,200,t,t+2,35,25);
}
setcolor(GREEN);
select(scor,-250,133);
for(t=0;t<=50;t++)
{
setcolor(14);
line(40+t,230,40+t,250);
line(560+t,230,560+t,250);
}
settextstyle(SMALL_FONT,HORIZ_DIR,4);
setcolor(BLUE);
outtextxy(45,235,"Continue");
outtextxy(575,235,"Quit");
restrictmouseptr(0,80,640,440);
flag=0;
while(flag==0)
{
showmouseptr();
getmouseptr(&button,&a,&b);
if(button==1)
{ x3=a;y3=b;
if((x3>41 && x3<79 )&& (y3>231 && y3<249))
{
flag=1;
//break;
}
else if((x3>=561 && x3<=599 )&& (y3>=231 && y3<=249))
{
flag=2;
//break;
}
}
}
if(flag==1)
goto a;
else
{if(flag==2)
{initgraph(&gd,&gm,"");
finalwindow();
closegraph();
goto b;}}
// next();
b: getch();
}//ending of main function
/* sub function target starting*/
arrow1(int sp,int col)
{ setcolor(col); //sound(277.38);
line(40+sp+36,240,80+sp+36,240);
line(80+sp+36,240,75+sp+36,235);
line(80+sp+36,240,75+sp+36,245);
line(40+sp+36,241,80+sp+36,241);
line(79+sp+36,240,74+sp+36,236);
line(79+sp+36,241,74+sp+36,245);
line(46+sp+36,240,41+sp+36,236);
line(46+sp+36,241,41+sp+36,245);
line(42+sp+36,240,37+sp+36,236);
line(42+sp+36,241,37+sp+36,245);
nosound();
}
tar(j,c)
{
setcolor(c*4);
ellipse(579,240+j,0,360,40,90);
setcolor(c*3);
ellipse(579,240+j,0,360,33,75);
setcolor(c*5);
ellipse(579,240+j,0,360,26,60);
setcolor(c*9);
ellipse(579,240+j,0,360,19,45);
setcolor(c*8);
ellipse(579,240+j,0,360,12,30);
setcolor(c*10);
ellipse(579,240+j,0,360,5,15);
}
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
getmouseptr(int *button,int *a,int *b)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*a=o.x.cx;
*b=o.x.dx;
}
mousereg()
{
int x1,x2,y1,y2,ce,k;
initmouse();
showmouseptr();
restrictmouseptr(281,361,359,379);
}
arrows(alh)
{ int z=0,r;
r=5-alh;
setcolor(YELLOW);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(30,20,"ARROWS REMAINING");
for(f=r,z=0;f>0;f--)
{
line(40,60+z*10,80,60+z*10);
line(80,60+z*10,75,55+z*10);
line(80,60+z*10,75,65+z*10);
z=z+1;
}
for(f=alh,z=0;f>0;f--)
{
setcolor(0);
line(40,60+z*10,80,60+z*10);
line(80,60+z*10,75,55+z*10);
line(80,60+z*10,75,65+z*10);
z=z+1;
}
}
score (int j)
{
if( (j>=-90 && j<=-74) || (j>=74 && j<=90))
scor=scor+10;
else if((j>=-72 && j<=-60) || (j>=60 && j<=72))
scor=scor+20;
else if((j>=-58 && j<=-42) || (j>=42 && j<=58))
scor=scor+30;
else if((j>=-40 && j<=-32) || (j>=32 && j<=40))
scor=scor+40;
else if((j>=-30 && j<=-18) || (j>=18 && j<=30))
scor=scor+50;
else if((j>=-16 && j<=0) || (j>=0 && j<=16))
scor=scor+60;
else if (j == 300)
scor=0;
for(jk=50;jk<=120;jk++)
{setcolor(0);
line(530,jk,620,jk);
}
setcolor(YELLOW);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
select(scor,0,0);
}
front1()
{
int gm=0,gd=DETECT,i,j,k,jk,gh;
initgraph(&gd,&gm," ");
setcolor(MAGENTA);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
for(i=12;i<=627;i+=12)
{
setcolor(YELLOW);
line(i,12,i,467);
//clearviewport();
for(j=15;j<=464;j+=5)
ellipse(i+6,j,-180,0,6,4);
}
for(i=3;i<=452;i+=5)
{ for(j=15;j<=627;j+=3)
{setcolor(0);
ellipse(j,467-i,-180,0,3,4);
}delay(40);
setcolor(BLACK);
for(k=1;k<=5;k++)
{
line(12,467-i+k,637,467-i+k); }
//setcolor(25);
setcolor(GREEN+MAGENTA+WHITE+BLUE+YELLOW);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
}
setcolor(2);
rectangle(290,370,350,380);
rectangle(292,372,348,378);
gh=292; //gh<=388;
for(j=1;j<=56;j+=1)
{ /* for(i=24;i<=627;i+=12)
{setcolor(i);
circle(i,23,3);
delay(1);
} */ jk=20;
for(i=24;i<=627;i+=12)
{setcolor(random(15));
circle(i,23,3);
sound(440+jk);
delay(10);
nosound();
setcolor(YELLOW);
rectangle(gh,372,gh,378);
//rectangle(282,362,358,388);}
jk+=40;
if(jk>=880)
{ jk=440;}
}
gh=gh+1;
nosound();
}
sleep(.1);
closegraph();
}
equip()
{
int kl=2;
int midx, midy,i,j,k,y=90,c;
// int stangle = 45, endangle = 270;
c=y/2;
for(c=50;c<=52;c+=1)
{ setcolor(kl);
line(26+c,170,26+c,310);
}
for(c=50;c<=54;c+=1)
{ setcolor(kl);
line(60+c,222,60+c,255);
}
for(c=180;c<=270;c++)
{
// arc(80,92,c,c+1,20);
setcolor(0);
arc(86,166,c,c+1,10);
getarccoords(&arcinfo);
setcolor(kl);
circle(arcinfo.xend, arcinfo.yend,2);
}
for(c=-50;c<=110;c++)
{
// arc(80,92,c,c+1,20);
setcolor(0);
arc(96,201,c,c+1,28);
getarccoords(&arcinfo);
setcolor(kl);
circle(arcinfo.xend, arcinfo.yend,2);
}
for(c=50,i=4;c>=-110;c--)
{
// arc(80,92,c,c+1,20);
setcolor(0);
line(127,170,127,315);
line(126,170,126,315);
arc(96,278,c,c+1,28);
getarccoords(&arcinfo);
setcolor(kl);
i=i+2;
if(i>14)
{i=2;}
circle(arcinfo.xend,arcinfo.yend,2);
}
for(c=90;c<=180;c++)
{
// arc(80,92,c,c+1,20);
setcolor(0);
arc(86,314,c,c+1,10);
getarccoords(&arcinfo);
setcolor(kl);
circle(arcinfo.xend, arcinfo.yend,2);
//setcolor(2);
}
}
sta()
{
int x,y=200,i,st,kl;
//initgraph(&gd,&gm,"");
setcolor(5);
for(st=190,i=2;st>=-20;st-=9,i+=3)
{
setcolor(GREEN);
arc(320,290,st,st+2,190);
getarccoords(&arcinfo);
delay(5);
x=arcinfo.xend;
y=arcinfo.yend;
sound(300-st);
star(x,y);
delay(24);
}
for(st=190,i=2;st>=-17;st-=9,i+=3)
{
setcolor(YELLOW);
arc(320,285,st,st+2,230);
getarccoords(&arcinfo);
delay(5);
x=arcinfo.xend;
y=arcinfo.yend;
sound(320-st);
star(x,y);
delay(24);
}
nosound();
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
for(i=140;i>=20;i-=10)
{
setcolor(8);
delay(13);
ya(i);
delay(13);
setcolor(4);
r(i);
delay(13);
setcolor(2);
e1(i);
delay(13);
h1(i);
delay(13);
c1(i);
delay(13);
r1(i);
delay(13);
a1(i);
delay(13);
/*line(320,240,325,233); // /1
line(325,233,330,240); // 1
line(330,240,337,240); // right hori
line(320,240,313,240); //left hori
line(313,240,318,247); // left horiz to right
line(337,240,332,247); // right horiz to left /
line(332,247,334,253); // left / to right
line(318,247,316,253); // right to left /
line(316,253,325,247);
line(334,253,325,247);*/
// getch();
}
}
ya(int i)
{
if(i==120)
{
delay(1);
setcolor(4);
// delay(800);
outtextxy(300+i,230,"Y");
}setcolor(14);
delay(1);
outtextxy(420,230,"Y"); //440
}
r(int i)
{
if(i==90)
{
setcolor(2);
delay(1);
outtextxy(300+i,230,"R");
}setcolor(14);
delay(1);
outtextxy(390,230,"R"); // 420
}
e1(int i)
{
if(i==60)
{
setcolor(2);
delay(1);
outtextxy(300+i,230,"E");
}setcolor(14);
delay(1);
outtextxy(360,230,"E"); //400
}
h1(int i)
{
if(i==30)
{
setcolor(2);
delay(1);
outtextxy(300+i,230,"H");
}setcolor(14);
delay(1);
outtextxy(330,230,"H"); //380
}
c1(int i)
{
if(i==60)
{
setcolor(2);
delay(1);
outtextxy(300+i-i,230,"C");
}setcolor(14);
delay(1);
outtextxy(300,230,"C"); //360
}
r1(int i)
{
if(i==40)
{
setcolor(2);
delay(1);
outtextxy(300+i-70,230,"R");
}setcolor(14);
delay(1);
outtextxy(270,230,"R"); // 340
}
a1(int i)
{
if(i==20)
{
setcolor(2);
delay(1);
outtextxy(300+i-80,230,"A");
}setcolor(14);
delay(1);
outtextxy(240,230,"A"); //320
}
star(x,y)
{
delay(80);
line(x,y,x+5,y-7); // /1 x=320, y=240
line(x+5,y-7,x+10,y); // 1
line(x+10,y,x+17,y); // right hori
line(x,y,x-7,y); //left hori
line(x-7,y,x-2,y+7); // left horiz to right
line(x+17,y,x+12,y+7); // right horiz to left /
line(x+12,y+7,x+14,y+13); // left / to right
line(x-2,y+7,x-4,y+13); // right to left /
line(x-4,y+13,x+5,y+7);
line(x+14,y+13,x+5,y+7);
}
front()
{
int gm=0,gd=DETECT,i,j,k,jk,gh;
initgraph(&gd,&gm," ");
setcolor(MAGENTA);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
for(i=12;i<=627;i+=12)
{
setcolor(YELLOW);
line(i,12,i,467);
//clearviewport();
for(j=15;j<=464;j+=5)
ellipse(i+6,j,-180,0,6,4);
}
for(i=3;i<=452;i+=5)
{ for(j=15;j<=627;j+=3)
{setcolor(0);
ellipse(j,467-i,-180,0,3,4);
}delay(40);
setcolor(BLACK);
for(k=1;k<=5;k++)
{
line(12,467-i+k,637,467-i+k); }
//setcolor(25);
setcolor(GREEN+MAGENTA+WHITE+BLUE+YELLOW);
rectangle(10,10,639,469);
rectangle(12,12,637,467);
}
setcolor(2);
rectangle(290,370,350,380);
rectangle(292,372,348,378);
gh=292; //gh<=388;
for(j=1;j<=56;j+=1)
{ /* for(i=24;i<=627;i+=12)
{setcolor(i);
circle(i,23,3);
delay(1);
} */ jk=20;
for(i=24;i<=627;i+=12)
{setcolor(random(15));
circle(i,23,3);
sound(200-i);
delay(1);
nosound();
setcolor(YELLOW);
rectangle(gh,372,gh,378);
//rectangle(282,362,358,388);}
jk+=40;
if(jk>=880)
{ jk=440;}
}
gh=gh+1;
nosound();
}
//printf("sdfs");
sta();
sleep(.1);
//closegraph();
}
gaover()
{
int gm=0,gd=DETECT,i,j;
initgraph(&gd,&gm," ");
for(i=1;i<=51;i+=4)
{
setcolor(i);
sound(400-i);
outtextxy(260,340,"GAME OVER");
if(i==49)
{
setcolor(YELLOW);
outtextxy(260,340,"GAME OVER");
}
nosound();
go(0,60,20,80,random(15),random(15));
delay(3);
go(400,460,420,480,random(15),random(15));
}
//getch();
}
go(int sy1,int ey1,int sy2,int ey2,int k,int h)
{
int i,j;
for(j=sy1;j<=ey1;j+=40) //120
{
for(i=0;i<=640;i+=40)
{
setcolor(k);
rectangle(i,1+j,20+i,21+j);
setfillstyle(SOLID_FILL,k);
floodfill(10+i,10+j,k);
setcolor(h);
rectangle(20+i,1+j,40+i,21+j);
setfillstyle(SOLID_FILL,h);
floodfill(25+i,10+j,h);
}
}
for(j=sy2;j<=ey2;j+=40)
{
for(i=0;i<=640;i+=40)
{
setcolor(h);
rectangle(i,1+j,20+i,21+j);
setfillstyle(SOLID_FILL,h);
floodfill(10+i,10+j,h);
setcolor(k);
rectangle(20+i,1+j,40+i,21+j);
setfillstyle(SOLID_FILL,k);
floodfill(25+i,10+j,k);
}
}
//
}
select(int scor,int p,int q)
{
switch(scor)
{ case 0: outtextxy(530+p,50+q,"0");
break;
case 10: outtextxy(530+p,50+q,"10");
break;
case 20: outtextxy(530+p,50+q,"20");
break;
case 30: outtextxy(530+p,50+q,"30");
break;
case 40: outtextxy(530+p,50+q,"40");
break;
case 50: outtextxy(530+p,50+q,"50");
break;
case 60: outtextxy(530+p,50+q,"60");
break;
case 70: outtextxy(530+p,50+q,"70");
break;
case 80: outtextxy(530+p,50+q,"80");
break;
case 90: outtextxy(530+p,50+q,"90");
break;
case 100: outtextxy(530+p,50+q,"100");
break;
case 110: outtextxy(530+p,50+q,"110");
break;
case 120: outtextxy(530+p,50+q,"120");
break;
case 130: outtextxy(530+p,50+q,"130");
break;
case 140: outtextxy(530+p,50+q,"140");
break;
case 150: outtextxy(530+p,50+q,"150");
break;
case 160: outtextxy(530+p,50+q,"160");
break;
case 170: outtextxy(530+p,50+q,"170");
break;
case 180: outtextxy(530+p,50+q,"180");
break;
case 190: outtextxy(530+p,50+q,"190");
break;
case 200: outtextxy(530+p,50+q,"200");
break;
case 210: outtextxy(530+p,50+q,"210");
break;
case 220: outtextxy(530+p,50+q,"220");
break;
case 230: outtextxy(530+p,50+q,"230");
break;
case 240: outtextxy(530+p,50+q,"240");
break;
case 250: outtextxy(530+p,50+q,"250");
break;
case 260: outtextxy(530+p,50+q,"260");
break;
case 270: outtextxy(530+p,50+q,"270");
break;
case 280: outtextxy(530+p,50+q,"280");
break;
case 290: outtextxy(530+p,50+q,"290");
break;
case 300: outtextxy(530+p,50+q,"300");
break;
case 310: outtextxy(530+p,50+q,"310");
break;
case 320: outtextxy(530+p,50+q,"320");
break;
case 330: outtextxy(530+p,50+q,"330");
break;
case 340: outtextxy(530+p,50+q,"340");
break;
case 350: outtextxy(530+p,50+q,"350");
break;
case 360: outtextxy(530+p,50+q,"360");
break;
} }
flower(int i,int j)
{
setcolor(14);
circle(320+i,240+j,10);
setfillstyle(SOLID_FILL,RED);
floodfill(323+i,241+j,14);
//1
setcolor(14);
//floodfill(316,223,13);
ellipse(310+i,220+j,90,-90,6,12);
ellipse(330+i,220+j,-90,90,6,12);
ellipse(320+i,207+j,180,0,8,2);
//2
ellipse(310+i,260+j,90,-90,6,12);
ellipse(330+i,260+j,-90,90,6,12);
ellipse(320+i,273+j,0,180,8,2);
//3
ellipse(298+i,248+j,180,0,12,6);
ellipse(298+i,233+j,0,180,12,6);
ellipse(286+i,240+j,-90,90,2,8);
//4
ellipse(342+i,247+j,180,0,12,6);
ellipse(342+i,233+j,0,180,12,6);
ellipse(354+i,240+j,90,-90,2,8);
}
finalwindow()
{
for(k=-285;k<=320;k+=70)
{ flower(k,-205);
flower(k,+180);
//clearviewport();
delay(100);
}
for(k=-140;k<=180;k+=40)
{ flower(-285,k);
flower(276,k);
//clearviewport();
delay(100);
}
setcolor(2);
sleep(4);
}
Kalian pasti sudah tahu dengan game ini, walaupun game ini sangat simpel tapi pada zamannya game ini sangat booming dan trending. Tertarik untuk mencoba membuatnya?? Kalian bisa mengikuti atau mencontoh kodenya seperti yang ada di bawah sini :
Snake Game di C
Snake Game in C
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
check();
end();
win();
int m[500],n[500],con=20;
clock_t start,stop;
void main(void)
{
int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100;
initgraph(&gd,&gm,"..\bgi");
setcolor(WHITE);
settextstyle(3,0,6);
outtextxy(200,2," SNAKE 2 ");
settextstyle(6,0,2);
outtextxy(20,80," Use Arrow Keys To Direct The Snake ");
outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake
");
outtextxy(20,160," Pick The Beats Untill You Win The Game ");
outtextxy(20,200," Press 'Esc' Anytime To Exit ");
outtextxy(20,220," Press Any Key To Continue ");
ch=getch();
if(ch==27) exit(0);
cleardevice();
maxx=getmaxx();
maxy=getmaxy();
randomize();
p=random(maxx);
int temp=p%13;
p=p-temp;
q=random(maxy);
temp=q%14;
q=q-temp;
start=clock();
int a=0,i=0,j,t;
while(1)
{
setcolor(WHITE);
setfillstyle(SOLID_FILL,con+5);
circle(p,q,5);
floodfill(p,q,WHITE);
if( kbhit() )
{
ch=getch(); if(ch==0) ch=getch();
if(ch==72&& a!=2) a=1;
if(ch==80&& a!=1) a=2;
if(ch==75&& a!=4) a=3;
if(ch==77&& a!=3) a=4;
}
else
{
if(ch==27
) break;
}
if(i<20){
m[i]=x;
n[i]=y;
i++;
}
if(i>=20)
{
for(j=con;j>=0;j--){
m[1+j]=m[j];
n[1+j]=n[j];
}
m[0]=x;
n[0]=y;
setcolor(WHITE);
setfillstyle(SOLID_FILL,con);
circle(m[0],n[0],8);
floodfill(m[0],n[0],WHITE);
setcolor(WHITE);
for(j=1;j<con;j++){
setfillstyle(SOLID_FILL,con+j%3);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(spd);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[0],n[0],8);
floodfill(m[0],n[0],BLACK);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[j],n[j],5);
floodfill(m[j],n[j],BLACK);
}
stop=clock();
t=(stop-start)/CLK_TCK;
printf(" TIME %d sec ",t);
printf("SCORE %d
",con-5);
check();
if(x==p&&y==q) { con=con+5; if(spd>=5) spd=spd-5; else spd=5;
if(con>490) win();
p=random(maxx); temp=p%13; p=p-temp;
q=random(maxy); temp=q%14; q=q-temp;
}
if(a==1) y = y-14; if(y<0) { temp=maxy%14;y=maxy-temp;}
if(a==2) y = y+14; if(y>maxy) y=0;
if(a==3) x = x-13; if(x<0) { temp=maxx%13;x=maxx-temp;}
if(a==4) x = x+13; if(x>maxx) x=0;
if(a==0){ y = y+14 ; x=x+13; }
}
}
check(){
int a;
for(a=1;a<con;a++)
if(m[0]==m[a] && n[0]==n[a]) end();
else continue;
}
end()
{
int j,i;
setcolor(WHITE);
for(i=0;i<5;i++){
delay(500);
cleardevice();
delay(500);
for(j=0;j<=con;j++){
setfillstyle(SOLID_FILL,RED);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
}
settextstyle(3,0,4);
outtextxy(150,150," GAME OVER ");
getch();
getch();
exit(0);
}
win()
{
int j,i;
setcolor(WHITE);
for(i=0;i<5;i++){
for(j=0;j<=con;j++){
setfillstyle(SOLID_FILL,con);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(500);
cleardevice();
delay(500);
}
settextstyle(3,0,4);
outtextxy(210,320," YOU WIN ");
getch();
exit(0);
}
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
check();
end();
win();
int m[500],n[500],con=20;
clock_t start,stop;
void main(void)
{
int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100;
initgraph(&gd,&gm,"..\bgi");
setcolor(WHITE);
settextstyle(3,0,6);
outtextxy(200,2," SNAKE 2 ");
settextstyle(6,0,2);
outtextxy(20,80," Use Arrow Keys To Direct The Snake ");
outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake
");
outtextxy(20,160," Pick The Beats Untill You Win The Game ");
outtextxy(20,200," Press 'Esc' Anytime To Exit ");
outtextxy(20,220," Press Any Key To Continue ");
ch=getch();
if(ch==27) exit(0);
cleardevice();
maxx=getmaxx();
maxy=getmaxy();
randomize();
p=random(maxx);
int temp=p%13;
p=p-temp;
q=random(maxy);
temp=q%14;
q=q-temp;
start=clock();
int a=0,i=0,j,t;
while(1)
{
setcolor(WHITE);
setfillstyle(SOLID_FILL,con+5);
circle(p,q,5);
floodfill(p,q,WHITE);
if( kbhit() )
{
ch=getch(); if(ch==0) ch=getch();
if(ch==72&& a!=2) a=1;
if(ch==80&& a!=1) a=2;
if(ch==75&& a!=4) a=3;
if(ch==77&& a!=3) a=4;
}
else
{
if(ch==27
) break;
}
if(i<20){
m[i]=x;
n[i]=y;
i++;
}
if(i>=20)
{
for(j=con;j>=0;j--){
m[1+j]=m[j];
n[1+j]=n[j];
}
m[0]=x;
n[0]=y;
setcolor(WHITE);
setfillstyle(SOLID_FILL,con);
circle(m[0],n[0],8);
floodfill(m[0],n[0],WHITE);
setcolor(WHITE);
for(j=1;j<con;j++){
setfillstyle(SOLID_FILL,con+j%3);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(spd);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[0],n[0],8);
floodfill(m[0],n[0],BLACK);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(m[j],n[j],5);
floodfill(m[j],n[j],BLACK);
}
stop=clock();
t=(stop-start)/CLK_TCK;
printf(" TIME %d sec ",t);
printf("SCORE %d
",con-5);
check();
if(x==p&&y==q) { con=con+5; if(spd>=5) spd=spd-5; else spd=5;
if(con>490) win();
p=random(maxx); temp=p%13; p=p-temp;
q=random(maxy); temp=q%14; q=q-temp;
}
if(a==1) y = y-14; if(y<0) { temp=maxy%14;y=maxy-temp;}
if(a==2) y = y+14; if(y>maxy) y=0;
if(a==3) x = x-13; if(x<0) { temp=maxx%13;x=maxx-temp;}
if(a==4) x = x+13; if(x>maxx) x=0;
if(a==0){ y = y+14 ; x=x+13; }
}
}
check(){
int a;
for(a=1;a<con;a++)
if(m[0]==m[a] && n[0]==n[a]) end();
else continue;
}
end()
{
int j,i;
setcolor(WHITE);
for(i=0;i<5;i++){
delay(500);
cleardevice();
delay(500);
for(j=0;j<=con;j++){
setfillstyle(SOLID_FILL,RED);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
}
settextstyle(3,0,4);
outtextxy(150,150," GAME OVER ");
getch();
getch();
exit(0);
}
win()
{
int j,i;
setcolor(WHITE);
for(i=0;i<5;i++){
for(j=0;j<=con;j++){
setfillstyle(SOLID_FILL,con);
circle(m[j],n[j],5);
floodfill(m[j],n[j],WHITE);
}
delay(500);
cleardevice();
delay(500);
}
settextstyle(3,0,4);
outtextxy(210,320," YOU WIN ");
getch();
exit(0);
}
Tetris game adalah game jadul yang tujuannya menyusun balok balok yang memiliki bentuk berbeda seperti bentuk L, T , I dan bentuk lainnya.
Kalian bisa mencoba membuat game menyusun ini dengan melihat contoh berikut ini.
Tetris game di C
Tetris Game in C
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
void swap(int a,int b);void bar1();void bar2();
void bar3(); void bar4(); check0(); check1();
check2(); void del(); void rod1(); void rod2();
void box1();void box2();void tee1();voidtee2();
void tee3();void tee4();check3();void insert();
void again();void print();void remove();void frame();
int score=0,spd,q,t,a=0,i,j,l[6]={300,90,315,105,301,91},p,turn,m,n;
char play='y';
static int arr[26][13];
void main(void)
{
char ch,y;
int gd= DETECT,gm,area,maxx,maxy,move,lmove,rmove;
void *buff;
initgraph(&gd,&gm,"d:\tc\bgi");
do{
turn=1;
score=0;
do{
clrscr();
cleardevice();
printf("
Enter Your Choice
");
printf(" Basic Level.......... 1
");
printf(" Middle Level......... 2
");
printf(" Advanced Level....... 3
");
printf(" Experts Level........ 4
");
printf(" Professional Level... 5
");
y=getch();
clrscr();
cleardevice();
if(y=='1') {spd=200; settextstyle(2,0,6);outtextxy(2,150," Basic
Level ");break;}
if(y=='2') {spd=150; settextstyle(2,0,6);outtextxy(2,150," Middle
Level ");break;}
if(y=='3') {spd=100; settextstyle(2,0,6);outtextxy(2,150," Advanced
Level ");break;}
if(y=='4') {spd=75; settextstyle(2,0,6);outtextxy(2,150," Experts
Level ");break;}
if(y=='5') {spd=50; settextstyle(2,0,6);outtextxy(2,150,"Professional
Level ");break;}
}while(1);
frame();
getch();
int k,ar[2],pr[2],t[6];
randomize();
ar[0]=random(2);
pr[0]=random(6);
ar[1]=random(2);
pr[1]=random(6);
while(1)
{
printf(" score= %d
",score);
a=ar[0];
p=pr[0];
l[0]=450;l[1]=250;l[2]=465;l[3]=265;l[4]=451;l[5]=251;
if(y=='1') spd=100;
if(y=='2') spd=75;
if(y=='3') spd=50;
if(y=='4') spd=30;
if(y=='5') spd=20;
print();
delay(500);
a=ar[1];
p=pr[1];
t[0]=l[0]; t[1]=l[1]; t[2]=l[2];
t[3]=l[3]; t[4]=l[4]; t[5]=l[5];
a=ar[1];
p=pr[1];
l[0]=270;l[1]=90;l[2]=285;l[3]=105;l[4]=271;l[5]=91;
do{
k=0;
if(kbhit()){ ch=getch();
if(ch==0) ch=getch();
if(ch==77) {move=check1(); if(move==0) {l[0]+=15;l[2]+=15;l[4]+=15;
k=1;}}
if(ch==75) {move=check2(); if(move==0) {l[0]-=15;l[2]-=15;l[4]-=15;
k=1;}}
if(ch==80) spd=10;
if(ch==27) exit(0);
if(ch==32){
k=1;
if(p==4||p==5) switch(p)
{
case 4: if(a==0) a++; else p++; break;
case 5: if(a==1) a--; else p--;
}
if(p==1||p==2) switch(p)
{ case 1: if(a==0) a++; else p++;
l[0]=270;l[2]=285;l[4]=271;break;
case 2: if(a==1) a--; else p--; l[0]=270;l[2]=285;l[4]=271;
}
if(p==3) { if(a==0) a++; else a=0;
l[0]=270;l[2]=285;l[4]=271; }
}
if(a==1) {swap(l[0],l[1]);swap(l[2],l[3]);swap(l[4],l[5]);}
}
move=check0();
if(move==1) break;
print();
if(k==1) delay(0); else delay(spd);
remove();
if(k==0) {l[1]+=15; l[3]+=15; l[5]+=15;}
}while(1);
print();
fflush(stdin);
insert();
check3();
a=ar[0];
p=pr[0];
l[0]=t[0];l[1]=t[1];l[2]=t[2];l[3]=t[3];
l[4]=t[4];l[5]=t[5];
remove();
ar[1]=ar[0];
pr[1]=pr[0];
ar[0]=random(2);
pr[0]=random(6);
if(pr[0]==2) pr[0]=3;
if(turn==0) break;
}
}while(play=='y');
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
}
void bar1()
{ if(a==0&&i<1) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==0&&i>=1){l[1]+=15;l[3]+=15;l[5]+=15; }
// if(a==1&&i==1)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==1&&i==0) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==1&&i>0) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==2&&i>=2) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==2&&i<2) {l[1]+=15;l[3]+=15;l[5]+=15; }
}
void bar2()
{ if(a==0&&i>=3){l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i<3) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==1&&i<3) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==1&&i==3){l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==2&&i<2) {l[0]+=15;l[2]+=15;l[4]+=15;}
if(a==2&&i>=2){l[1]-=15;l[3]-=15;l[5]-=15; }
}
void bar3()
{if(a==0&&i>=3){l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==0&&i<3){l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==2&&i<2) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==2&&i>=2) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==1&&i>=1) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==1&&i<1) {l[1]+=15;l[3]+=15;l[5]+=15; }
}
void bar4()
{if(a==0&&i<1){l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i>=1) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==2&&i>=2) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==2&&i<2){l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==1&&i<3) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==1&&i==3) {l[1]-=15;l[3]-=15;l[5]-=15; }
}
void rod1()
{
if(a==0) {l[0]+=15;l[2]+=15;l[4]+=15; }
else {l[1]+=15;l[3]+=15;l[5]+=15; }
}
void rod2()
{
if(a==0) {l[0]-=15;l[2]-=15;l[4]-=15; }
else {l[1]-=15;l[3]-=15;l[5]-=15; }
}
void box1()
{if(i<1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(i==1) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(i==2) {l[0]+=15;l[2]+=15;l[4]+=15; }
}
void box2()
{if(i<1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(i==1) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(i==2) {l[0]+=15;l[2]+=15;l[4]+=15; }
}
void tee1()
{
if(a==0&&i==1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i<1) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==0&&(i==2||i==3)) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a>0&&i==2)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&i==0) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&(i==1)) {l[0]+=15;l[2]+=15;l[4]+=15; }
}
void tee2()
{
if(a==0&&i==0) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i==1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i==2)
{l[0]+=15;l[2]+=15;l[4]+=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==2)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==1) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a>0&&i==0) {l[1]-=15;l[3]-=15;l[5]-=15; }
}
void tee3()
{ if(a==0&&i==0) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i==2)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==0&&i==1) {l[1]+=15;l[3]+=15;l[5]+=15;}
if(a>0&&i==2)
{l[0]+=15;l[2]+=15;l[4]+=15;l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&i==0) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&(i==1)) {l[0]-=15;l[2]-=15;l[4]-=15; }
}
void tee4()
{ if(a==0&&i==1) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==0&&i==0) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==0&&i==2)
{l[1]-=15;l[3]-=15;l[5]-=15;l[0]+=15;l[2]+=15;l[4]+=15; }
if(a>0&&i==2)
{l[0]+=15;l[2]+=15;l[4]+=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==0) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==1) {l[0]-=15;l[2]-=15;l[4]-=15; }
}
check0()
{ int x,y,s,d=5;
x=(l[0]-180)/15;
y=(l[1]-90)/15;
if(a==0){
switch (p)
{ case 0: y+=2;d=2; if(arr[y][x-1]==1) return(1); else break;
case 1: y+=4;x++;d=1;if(arr[y-3][x-1]==1) return(1); else break;
case 2: y=y+4;d=2;x+=1;break;
case 3: y=y+1;d=5;x+=4;break;
case 4: y+=2;d=3;x++;break;
case 5: y+=1;d=3;if(arr[y+1][x-1]==1) return(1); else break;
}
for(s=1;s<d;s++,x--)
if(arr[y][x]==1) return(1);
if((arr[y][x])==0) return(0); else return(1);
}
if(a==1){
switch (p)
{ case 0: y+=2;d=2; if(arr[y][x-1]==1) return(1); else break;
case 1: y=y+2;d=4;if(arr[y][x]==1) return(1); else break;
case 2: y=y+2;d=4;x+=3;if(arr[y][x-3]==1) return(1); else break;
case 3: y=y+5;d=0;break;
case 4: y+=3;d=0;if(arr[y-1][x+1]==1) return(1); else break;
case 5: y+=3;d=0;if(arr[y-1][x-1]==1) return(1); else break;
}
for(s=1;s<d;s++,x--)
if(arr[y][x]==1) break;
if((arr[y][x])==0) return(0); else return(1);
}
return(0);
}
check1()
{ int x,y,s,d=5;
x=(l[0]-150)/15;
y=(l[1]-90)/15;
if(a==0){
switch (p)
{ case 0: x=x-1;y++;d=2;break;
case 1: x=x;d=3; y=y+3; break;
case 2: x=x;d=4;y+=3; break;
case 3: x=x+3;d=0;break;
case 4: x=x;break;
case 5: x=x-1;d=0;
}
for(s=0;s<d;s++,y--)
if(arr[y][x]==1) break;
if((arr[y][x])==0) return(0); else return(2);
}
if(a==1)
{ switch(p)
{ case 0: x=x; break;
case 1: x=x;d=1;break;
case 2: x=x+3; break;
case 3: x=x;y+=5;d=4;break;
case 4: x=x+1;break;
case 5: x=x;y+=2;d=2;
}
for(s=0;s<d;s++,y--)
if(arr[y][x-1]==1) break;
if((arr[y][x-1])==0) return(0); else return(2);
}
return(0);
}
check2()
{
int x,y,s,d=5;
x=(l[0]-150)/15;
y=(l[1]-90)/15;
if(a==0){
switch (p)
{ case 0: x=x-1;d=1;break;
case 1: x=x;d=3;break;
case 2: x=x;d=3;break;
case 3: x=x;d=0;break;
case 4: x=x-1;d=1;break;
case 5: x=x-2;d=0;
}
for(s=0;s<d;s++,y++)
if(arr[y][x-3]==1) break;
if((arr[y][x-3])==0) return(0); else return(2);
}
if(a==1){
switch (p)
{ case 0: x=x-4;d=2;break;
case 1: x=x-6;d=0;break;
case 2: x=x-3;d=2;y-=1;break;
case 3: x=x-3;d=5;break;
case 4: x-=3; d=3;break;
case 5: x-=4; d=4;
}
for(s=1;s<d;s++,y++)
if(arr[y][x]==1) break;
if((arr[y][x])==0) return(0); else return(2);
}
return(0);
}
check3()
{
for(t=1;t<25;t++)
for(q=1;q<12;)
if(arr[t][q]==1) { q++; if(q==12) {score+=100;del();}} else break;
return(0);
}
void insert()
{ int x,y;
for(i=0;i<5;i++){
x=(l[0]-180)/15;
y=(l[1]-90)/15;
arr[y][x]=1;
switch(p)
{
case 0: box2();break;
case 1: bar2();break;
case 2: bar4();break;
case 3: rod2();break;
case 4: tee2();break;
case 5: tee4();
}
}
if(arr[0][5]==1||arr[0][6]==1)
{
settextstyle(3,0,4);
outtextxy(180,100," GAME OVER ");
fflush(stdin);
getch();
turn=0;
again();
}
}
void print()
{
for(i=0;i<5;i++){
setcolor(WHITE);
setfillstyle(SOLID_FILL,6);
rectangle(l[0]+1,l[1]+1,l[2]-1,l[3]-1);
floodfill(l[4]+1,l[5]+1,WHITE);
if(i<4) switch (p)
{ case 0 : box1();break;
case 1 : bar1();break;
case 2 : bar3();break;
case 3 : rod1();break;
case 4 : tee1();break;
case 5 : tee3();
}
}
}
void remove()
{
for(i=0;i<5;i++){
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
rectangle(l[0]+1,l[1]+1,l[2]-1,l[3]-1);
floodfill(l[4]+1,l[5]+1,BLACK);
if(i<4) switch (p)
{ case 0 : box2();break;
case 1 : bar2();break;
case 2 : bar4();break;
case 3 : rod2();break;
case 4 : tee2();break;
case 5 : tee4();
}
}
}
void del()
{ int m,n;
for(;t>0;t--)
for(q=1;q<12;q++)
arr[t][q]=arr[t-1][q];
for(n=0;n<25;n++)
//delay(50);
for(m=1;m<12;m++){
if(arr[n][m]==1) { setcolor(WHITE);
setfillstyle(SOLID_FILL,6);
rectangle((m*15)+181,(n*15)+91,(m*15)+194,(n*15)+104);
floodfill((m*15)+182,(n*15)+92,WHITE);
}
else { setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
rectangle((m*15)+181,(n*15)+91,(m*15)+194,(n*15)+104);
floodfill((m*15)+182,(n*15)+92,BLACK);
}
}
}
void frame()
{ setcolor(WHITE);
setfillstyle(SOLID_FILL,8);
settextstyle(7,0,6);
outtextxy(140,3," TETTRIS ");
setcolor(WHITE);
settextstyle(2,0,7);
outtextxy(400,440,"Press 'Esc' To Exit");
settextstyle(2,0,5);
outtextxy(5,285," PRESS ");
outtextxy(5,300,"Space To Change Shape");
outtextxy(5,315,"Use Arrow Keys To Move");
rectangle(180,465,375,480);
floodfill(181,466,WHITE);
rectangle(180,90,194,480);
floodfill(181,95,WHITE);
rectangle(361,90,375,480);
floodfill(362,95,WHITE);
setcolor(WHITE);
settextstyle(7,0,1);
outtextxy(400,200," Next To Come ");
setfillstyle(SOLID_FILL,0);
rectangle(400,230,530,335);
floodfill(401,231,WHITE);
for(m=0;m<26;m++)
for(n=0;n<13;n++)
arr[m][n]=0;
for(m=0,n=0;n<26;n++)
arr[n][m]=1;
for(n=12,m=0;m<26;m++)
arr[m][n]=1;
for(m=25,n=0;n<13;n++)
arr[m][n]=1;
}
void again()
{
cleardevice();
setcolor(WHITE);
setfillstyle(SOLID_FILL,BLACK);
rectangle(150,175,530,335);
floodfill(159,179,BLACK);
setcolor(WHITE);
settextstyle(7,0,1);
outtextxy(175,200," Enter Your Choice ");
outtextxy(175,220," Press 'Y' To Play Again ");
outtextxy(175,240," Press Any Key To Exit ");
play=getch();
if(play=='y') cleardevice();
else exit(0);
}
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
void swap(int a,int b);void bar1();void bar2();
void bar3(); void bar4(); check0(); check1();
check2(); void del(); void rod1(); void rod2();
void box1();void box2();void tee1();voidtee2();
void tee3();void tee4();check3();void insert();
void again();void print();void remove();void frame();
int score=0,spd,q,t,a=0,i,j,l[6]={300,90,315,105,301,91},p,turn,m,n;
char play='y';
static int arr[26][13];
void main(void)
{
char ch,y;
int gd= DETECT,gm,area,maxx,maxy,move,lmove,rmove;
void *buff;
initgraph(&gd,&gm,"d:\tc\bgi");
do{
turn=1;
score=0;
do{
clrscr();
cleardevice();
printf("
Enter Your Choice
");
printf(" Basic Level.......... 1
");
printf(" Middle Level......... 2
");
printf(" Advanced Level....... 3
");
printf(" Experts Level........ 4
");
printf(" Professional Level... 5
");
y=getch();
clrscr();
cleardevice();
if(y=='1') {spd=200; settextstyle(2,0,6);outtextxy(2,150," Basic
Level ");break;}
if(y=='2') {spd=150; settextstyle(2,0,6);outtextxy(2,150," Middle
Level ");break;}
if(y=='3') {spd=100; settextstyle(2,0,6);outtextxy(2,150," Advanced
Level ");break;}
if(y=='4') {spd=75; settextstyle(2,0,6);outtextxy(2,150," Experts
Level ");break;}
if(y=='5') {spd=50; settextstyle(2,0,6);outtextxy(2,150,"Professional
Level ");break;}
}while(1);
frame();
getch();
int k,ar[2],pr[2],t[6];
randomize();
ar[0]=random(2);
pr[0]=random(6);
ar[1]=random(2);
pr[1]=random(6);
while(1)
{
printf(" score= %d
",score);
a=ar[0];
p=pr[0];
l[0]=450;l[1]=250;l[2]=465;l[3]=265;l[4]=451;l[5]=251;
if(y=='1') spd=100;
if(y=='2') spd=75;
if(y=='3') spd=50;
if(y=='4') spd=30;
if(y=='5') spd=20;
print();
delay(500);
a=ar[1];
p=pr[1];
t[0]=l[0]; t[1]=l[1]; t[2]=l[2];
t[3]=l[3]; t[4]=l[4]; t[5]=l[5];
a=ar[1];
p=pr[1];
l[0]=270;l[1]=90;l[2]=285;l[3]=105;l[4]=271;l[5]=91;
do{
k=0;
if(kbhit()){ ch=getch();
if(ch==0) ch=getch();
if(ch==77) {move=check1(); if(move==0) {l[0]+=15;l[2]+=15;l[4]+=15;
k=1;}}
if(ch==75) {move=check2(); if(move==0) {l[0]-=15;l[2]-=15;l[4]-=15;
k=1;}}
if(ch==80) spd=10;
if(ch==27) exit(0);
if(ch==32){
k=1;
if(p==4||p==5) switch(p)
{
case 4: if(a==0) a++; else p++; break;
case 5: if(a==1) a--; else p--;
}
if(p==1||p==2) switch(p)
{ case 1: if(a==0) a++; else p++;
l[0]=270;l[2]=285;l[4]=271;break;
case 2: if(a==1) a--; else p--; l[0]=270;l[2]=285;l[4]=271;
}
if(p==3) { if(a==0) a++; else a=0;
l[0]=270;l[2]=285;l[4]=271; }
}
if(a==1) {swap(l[0],l[1]);swap(l[2],l[3]);swap(l[4],l[5]);}
}
move=check0();
if(move==1) break;
print();
if(k==1) delay(0); else delay(spd);
remove();
if(k==0) {l[1]+=15; l[3]+=15; l[5]+=15;}
}while(1);
print();
fflush(stdin);
insert();
check3();
a=ar[0];
p=pr[0];
l[0]=t[0];l[1]=t[1];l[2]=t[2];l[3]=t[3];
l[4]=t[4];l[5]=t[5];
remove();
ar[1]=ar[0];
pr[1]=pr[0];
ar[0]=random(2);
pr[0]=random(6);
if(pr[0]==2) pr[0]=3;
if(turn==0) break;
}
}while(play=='y');
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
}
void bar1()
{ if(a==0&&i<1) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==0&&i>=1){l[1]+=15;l[3]+=15;l[5]+=15; }
// if(a==1&&i==1)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==1&&i==0) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==1&&i>0) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==2&&i>=2) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==2&&i<2) {l[1]+=15;l[3]+=15;l[5]+=15; }
}
void bar2()
{ if(a==0&&i>=3){l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i<3) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==1&&i<3) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==1&&i==3){l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==2&&i<2) {l[0]+=15;l[2]+=15;l[4]+=15;}
if(a==2&&i>=2){l[1]-=15;l[3]-=15;l[5]-=15; }
}
void bar3()
{if(a==0&&i>=3){l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==0&&i<3){l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==2&&i<2) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==2&&i>=2) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==1&&i>=1) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==1&&i<1) {l[1]+=15;l[3]+=15;l[5]+=15; }
}
void bar4()
{if(a==0&&i<1){l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i>=1) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==2&&i>=2) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==2&&i<2){l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==1&&i<3) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==1&&i==3) {l[1]-=15;l[3]-=15;l[5]-=15; }
}
void rod1()
{
if(a==0) {l[0]+=15;l[2]+=15;l[4]+=15; }
else {l[1]+=15;l[3]+=15;l[5]+=15; }
}
void rod2()
{
if(a==0) {l[0]-=15;l[2]-=15;l[4]-=15; }
else {l[1]-=15;l[3]-=15;l[5]-=15; }
}
void box1()
{if(i<1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(i==1) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(i==2) {l[0]+=15;l[2]+=15;l[4]+=15; }
}
void box2()
{if(i<1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(i==1) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(i==2) {l[0]+=15;l[2]+=15;l[4]+=15; }
}
void tee1()
{
if(a==0&&i==1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i<1) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==0&&(i==2||i==3)) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a>0&&i==2)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&i==0) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&(i==1)) {l[0]+=15;l[2]+=15;l[4]+=15; }
}
void tee2()
{
if(a==0&&i==0) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i==1) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i==2)
{l[0]+=15;l[2]+=15;l[4]+=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==2)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==1) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a>0&&i==0) {l[1]-=15;l[3]-=15;l[5]-=15; }
}
void tee3()
{ if(a==0&&i==0) {l[0]-=15;l[2]-=15;l[4]-=15; }
if(a==0&&i==2)
{l[0]-=15;l[2]-=15;l[4]-=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a==0&&i==1) {l[1]+=15;l[3]+=15;l[5]+=15;}
if(a>0&&i==2)
{l[0]+=15;l[2]+=15;l[4]+=15;l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&i==0) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a>0&&(i==1)) {l[0]-=15;l[2]-=15;l[4]-=15; }
}
void tee4()
{ if(a==0&&i==1) {l[1]+=15;l[3]+=15;l[5]+=15; }
if(a==0&&i==0) {l[0]+=15;l[2]+=15;l[4]+=15; }
if(a==0&&i==2)
{l[1]-=15;l[3]-=15;l[5]-=15;l[0]+=15;l[2]+=15;l[4]+=15; }
if(a>0&&i==2)
{l[0]+=15;l[2]+=15;l[4]+=15;l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==0) {l[1]-=15;l[3]-=15;l[5]-=15; }
if(a>0&&i==1) {l[0]-=15;l[2]-=15;l[4]-=15; }
}
check0()
{ int x,y,s,d=5;
x=(l[0]-180)/15;
y=(l[1]-90)/15;
if(a==0){
switch (p)
{ case 0: y+=2;d=2; if(arr[y][x-1]==1) return(1); else break;
case 1: y+=4;x++;d=1;if(arr[y-3][x-1]==1) return(1); else break;
case 2: y=y+4;d=2;x+=1;break;
case 3: y=y+1;d=5;x+=4;break;
case 4: y+=2;d=3;x++;break;
case 5: y+=1;d=3;if(arr[y+1][x-1]==1) return(1); else break;
}
for(s=1;s<d;s++,x--)
if(arr[y][x]==1) return(1);
if((arr[y][x])==0) return(0); else return(1);
}
if(a==1){
switch (p)
{ case 0: y+=2;d=2; if(arr[y][x-1]==1) return(1); else break;
case 1: y=y+2;d=4;if(arr[y][x]==1) return(1); else break;
case 2: y=y+2;d=4;x+=3;if(arr[y][x-3]==1) return(1); else break;
case 3: y=y+5;d=0;break;
case 4: y+=3;d=0;if(arr[y-1][x+1]==1) return(1); else break;
case 5: y+=3;d=0;if(arr[y-1][x-1]==1) return(1); else break;
}
for(s=1;s<d;s++,x--)
if(arr[y][x]==1) break;
if((arr[y][x])==0) return(0); else return(1);
}
return(0);
}
check1()
{ int x,y,s,d=5;
x=(l[0]-150)/15;
y=(l[1]-90)/15;
if(a==0){
switch (p)
{ case 0: x=x-1;y++;d=2;break;
case 1: x=x;d=3; y=y+3; break;
case 2: x=x;d=4;y+=3; break;
case 3: x=x+3;d=0;break;
case 4: x=x;break;
case 5: x=x-1;d=0;
}
for(s=0;s<d;s++,y--)
if(arr[y][x]==1) break;
if((arr[y][x])==0) return(0); else return(2);
}
if(a==1)
{ switch(p)
{ case 0: x=x; break;
case 1: x=x;d=1;break;
case 2: x=x+3; break;
case 3: x=x;y+=5;d=4;break;
case 4: x=x+1;break;
case 5: x=x;y+=2;d=2;
}
for(s=0;s<d;s++,y--)
if(arr[y][x-1]==1) break;
if((arr[y][x-1])==0) return(0); else return(2);
}
return(0);
}
check2()
{
int x,y,s,d=5;
x=(l[0]-150)/15;
y=(l[1]-90)/15;
if(a==0){
switch (p)
{ case 0: x=x-1;d=1;break;
case 1: x=x;d=3;break;
case 2: x=x;d=3;break;
case 3: x=x;d=0;break;
case 4: x=x-1;d=1;break;
case 5: x=x-2;d=0;
}
for(s=0;s<d;s++,y++)
if(arr[y][x-3]==1) break;
if((arr[y][x-3])==0) return(0); else return(2);
}
if(a==1){
switch (p)
{ case 0: x=x-4;d=2;break;
case 1: x=x-6;d=0;break;
case 2: x=x-3;d=2;y-=1;break;
case 3: x=x-3;d=5;break;
case 4: x-=3; d=3;break;
case 5: x-=4; d=4;
}
for(s=1;s<d;s++,y++)
if(arr[y][x]==1) break;
if((arr[y][x])==0) return(0); else return(2);
}
return(0);
}
check3()
{
for(t=1;t<25;t++)
for(q=1;q<12;)
if(arr[t][q]==1) { q++; if(q==12) {score+=100;del();}} else break;
return(0);
}
void insert()
{ int x,y;
for(i=0;i<5;i++){
x=(l[0]-180)/15;
y=(l[1]-90)/15;
arr[y][x]=1;
switch(p)
{
case 0: box2();break;
case 1: bar2();break;
case 2: bar4();break;
case 3: rod2();break;
case 4: tee2();break;
case 5: tee4();
}
}
if(arr[0][5]==1||arr[0][6]==1)
{
settextstyle(3,0,4);
outtextxy(180,100," GAME OVER ");
fflush(stdin);
getch();
turn=0;
again();
}
}
void print()
{
for(i=0;i<5;i++){
setcolor(WHITE);
setfillstyle(SOLID_FILL,6);
rectangle(l[0]+1,l[1]+1,l[2]-1,l[3]-1);
floodfill(l[4]+1,l[5]+1,WHITE);
if(i<4) switch (p)
{ case 0 : box1();break;
case 1 : bar1();break;
case 2 : bar3();break;
case 3 : rod1();break;
case 4 : tee1();break;
case 5 : tee3();
}
}
}
void remove()
{
for(i=0;i<5;i++){
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
rectangle(l[0]+1,l[1]+1,l[2]-1,l[3]-1);
floodfill(l[4]+1,l[5]+1,BLACK);
if(i<4) switch (p)
{ case 0 : box2();break;
case 1 : bar2();break;
case 2 : bar4();break;
case 3 : rod2();break;
case 4 : tee2();break;
case 5 : tee4();
}
}
}
void del()
{ int m,n;
for(;t>0;t--)
for(q=1;q<12;q++)
arr[t][q]=arr[t-1][q];
for(n=0;n<25;n++)
//delay(50);
for(m=1;m<12;m++){
if(arr[n][m]==1) { setcolor(WHITE);
setfillstyle(SOLID_FILL,6);
rectangle((m*15)+181,(n*15)+91,(m*15)+194,(n*15)+104);
floodfill((m*15)+182,(n*15)+92,WHITE);
}
else { setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
rectangle((m*15)+181,(n*15)+91,(m*15)+194,(n*15)+104);
floodfill((m*15)+182,(n*15)+92,BLACK);
}
}
}
void frame()
{ setcolor(WHITE);
setfillstyle(SOLID_FILL,8);
settextstyle(7,0,6);
outtextxy(140,3," TETTRIS ");
setcolor(WHITE);
settextstyle(2,0,7);
outtextxy(400,440,"Press 'Esc' To Exit");
settextstyle(2,0,5);
outtextxy(5,285," PRESS ");
outtextxy(5,300,"Space To Change Shape");
outtextxy(5,315,"Use Arrow Keys To Move");
rectangle(180,465,375,480);
floodfill(181,466,WHITE);
rectangle(180,90,194,480);
floodfill(181,95,WHITE);
rectangle(361,90,375,480);
floodfill(362,95,WHITE);
setcolor(WHITE);
settextstyle(7,0,1);
outtextxy(400,200," Next To Come ");
setfillstyle(SOLID_FILL,0);
rectangle(400,230,530,335);
floodfill(401,231,WHITE);
for(m=0;m<26;m++)
for(n=0;n<13;n++)
arr[m][n]=0;
for(m=0,n=0;n<26;n++)
arr[n][m]=1;
for(n=12,m=0;m<26;m++)
arr[m][n]=1;
for(m=25,n=0;n<13;n++)
arr[m][n]=1;
}
void again()
{
cleardevice();
setcolor(WHITE);
setfillstyle(SOLID_FILL,BLACK);
rectangle(150,175,530,335);
floodfill(159,179,BLACK);
setcolor(WHITE);
settextstyle(7,0,1);
outtextxy(175,200," Enter Your Choice ");
outtextxy(175,220," Press 'Y' To Play Again ");
outtextxy(175,240," Press Any Key To Exit ");
play=getch();
if(play=='y') cleardevice();
else exit(0);
}
Berikut ini adalah kode C untuk membuat
Sebuah game bouncing ball, mirip miriplah dengan bouncing ball yang ada di hp nokia jadul dulu
Bouncing Game di C
Bouncing Ball
#include<graphics.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
#include<stdlib.h>
void *ball;
void image()
{
//ball
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(10,10,10,10);
ball=malloc(imagesize(0,0,20,20));
getimage(0,0,20,20,ball);
cleardevice();
}
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int l=getmaxx()/2,r=0,t=0,b=0;
int x=1,y=1;
int s=0,key=0;
int xstep=1,ystep=1;
image();
setbkcolor(GREEN);
while(key!=27)
{
while(!kbhit()){
putimage(l,t,ball,XOR_PUT);
delay(5);
putimage(l,t,ball,XOR_PUT);
if(l>=getmaxx()||l<=0){x*=-1;sound(1000);s=0;xstep=x*(random(4)+1);ystep=y
*(random(3)+1);if(l<=0)l=0;else l=getmaxx();}
if(t>=getmaxy()||t<=0){y*=-1;sound(1000);s=0;ystep=y*(random(4)+1);xstep=x
*(random(3)+1);if(t<=0)t=0;else t=getmaxy();}
l+=x+xstep;
t+=y+ystep;
s++;
if(s==5)
{nosound();}
}
key=getch();
}
closegraph();
}
#include<graphics.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
#include<stdlib.h>
void *ball;
void image()
{
//ball
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(10,10,10,10);
ball=malloc(imagesize(0,0,20,20));
getimage(0,0,20,20,ball);
cleardevice();
}
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int l=getmaxx()/2,r=0,t=0,b=0;
int x=1,y=1;
int s=0,key=0;
int xstep=1,ystep=1;
image();
setbkcolor(GREEN);
while(key!=27)
{
while(!kbhit()){
putimage(l,t,ball,XOR_PUT);
delay(5);
putimage(l,t,ball,XOR_PUT);
if(l>=getmaxx()||l<=0){x*=-1;sound(1000);s=0;xstep=x*(random(4)+1);ystep=y
*(random(3)+1);if(l<=0)l=0;else l=getmaxx();}
if(t>=getmaxy()||t<=0){y*=-1;sound(1000);s=0;ystep=y*(random(4)+1);xstep=x
*(random(3)+1);if(t<=0)t=0;else t=getmaxy();}
l+=x+xstep;
t+=y+ystep;
s++;
if(s==5)
{nosound();}
}
key=getch();
}
closegraph();
}
Game terakhir adalah game catur, kalian bisa mencoba membuat game catur yang bisa dimainkan oleh dua orang, nah untuk susunan kodenya, kalian bisa melihat contohnya disini :
Chess Game di C
Chess for two humans
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void blk(int,int);
void display();
//enum bool{TRUE,FALSE};
/*void main() //MAIN FUNCTION
{
display();
getch();
} */
void display() //DISPLAY TO SHOW THE BOARD
{
int gd=DETECT,gm,i,j,l,m,b;
char pattern[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
int arrodd[32][2]={ {0,0},{100,0},{200,0},{300,0},{50,50},{150,50}
,{250,50},{350,50},{0,100},{100,100},{200,100},{300,100}
,{50,150},{150,150},{250,150},{350,150},{0,200},{100,200}
,{200,200},{300,200},{50,250},{150,250},{250,250},{350,250},
{0,300},{100,300},{200,300},{300,300},{50,350},{150,350},
{250,350},{350,350} };
int
arrevn[33][2]={{50,0},{150,0},{250,0},{350,0},{0,50},{100,50},{200,50},
{300,50},{50,100},{150,100},{250,100},{350,100},{0,150},
{100,150},{200,150},{300,150},{50,200},{150,200},{250,200},
{350,200},{0,250},{100,250},{200,250},{300,250},{50,300},
{150,300},{250,300},{350,300},{0,350},{100,350},{200,350},
{300,350}};
clrscr();
initgraph(&gd,&gm,"");
for(i=0;i<=400;i=i+50) //to draw vertical lines of the chess board.
{
line(i,0,i,400);
}
for(j=0;j<=400;j=j+50) //to draw the horizontal lines of the chess
board.
{
line(0,j,400,j);
}
blk(420,0);
blk(420,250);
line(0,430,0,460); //to draw the display at the bottom
line(0,430,400,430);
line(0,460,400,460);
line(400,430,400,460);
settextstyle(7,HORIZ_DIR,3);
setfillpattern(&pattern,GREEN);
for(b=0;b<=31;b++)
{
bar(arrodd[b][0],arrodd[b][1],arrodd[b][0]+50,arrodd[b][1]+50);
rectangle(arrodd[b][0],arrodd[b][1],arrodd[b][0]+50,arrodd[b][1]+50);
}
setfillpattern(&pattern,DARKGRAY);
for(m=0;m<=31;m++)
{
bar(arrevn[m][0],arrevn[m][1],arrevn[m][0]+50,arrevn[m][1]+50);
rectangle(arrevn[m][0],arrevn[m][1],arrevn[m][0]+50,arrevn[m][1]+50);
}
}
void blk(int q,int r)
{
int y,z;
for(y=q;y<=(q+200);y=y+50)
{ line(y,r,y,r+200);}
for(z=r;z<=(r+200);z=z+50)
{ line(q,z,q+200,z);}
}
//Source file//
#include<akhchs.c> //we used akhchs.c as header file
#include<stdio.h>
#include<conio.h>
#include<dos.h>
char pos[8][8],color[8][8];
void assign(char*,char*);
struct block{ //structure for every block
int curx,cury;
char clr,blkcolor;
};
union REGS i,o; //for hardware interaction
char getblkcolor(int,int,int,int,char); //functions prototype
char chkpawnmov(struct block* selected,int x3,int y3);
char chkrookmov(struct block* selected,int x3,int y3);
char chkknightmov(struct block* selected,int x3,int y3);
char chkbishopmov(struct block* selected,int x3,int y3);
char chkqueenmov(struct block* selected,int x3,int y3);
char chkkingmov(struct block* selected,int x3,int y3);
void checkpwn(int,int);
void main()
{
int i1,j1,button,x,y,x1,y1,x2,y2,x3,y3,ch,i=0,j=0,z=1,turn=0;
struct block
*selected,pw[8],pb[8],rookw[2],rookb[2],bishopw[2],bishopb[2];
struct block kingw,kingb,queenw,queenb,knightw[2],knightb[2];
char id,nwblkclr,move='i';
assign(pos,color);
clrscr();
display(); //for showing chess board
design();
setspare(); //for drawing rook etc.
setposw(pw); //sets position of black and
setposb(pb); //white pawns with color
setposrw(rookw,bishopw,knightw,&kingw,&queenw);//sets position of
whites
setposrb(rookb,bishopb,knightb,&kingb,&queenb);//sete position of
blacks
//restrictmouseptr(0,0,400,400); //restricts pointer of mouse in
the
area specified there.
showmouseptr();
while(z)
{
i=i+1;
setcolor(WHITE);
if(turn%2==0) //means white's turn
outtextxy(100,430,"white's turn");
else if(turn%2!=0) //means black's turn
outtextxy(100,430,"black's turn");
while(i)
{
if(kbhit())
{
ch=getch();
if(ch=='
') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x,&y); //gets click position of mouse
if((button== 001))
{
sound(1000);
i=i-1;
delay(50);
nosound();
delay(500);
}
}
x1=relposx(x)/50;
y1=relposy(y)/50;
gotoxy(6,28);
if(x1==pw[0].curx && y1==pw[0].cury )
{ selected=&pw[0]; id='p'; }
else if(x1==pw[1].curx && y1==pw[1].cury )
{ selected=&pw[1]; id='p'; }
else if(x1==pw[2].curx && y1==pw[2].cury )
{ selected=&pw[2]; id='p'; }
else if(x1==pw[3].curx && y1==pw[3].cury )
{ selected=&pw[3]; id='p'; }
else if(x1==pw[4].curx && y1==pw[4].cury )
{ selected=&pw[4]; id='p'; }
else if(x1==pw[5].curx && y1==pw[5].cury )
{ selected=&pw[5]; id='p'; }
else if(x1==pw[6].curx && y1==pw[6].cury )
{ selected=&pw[6]; id='p'; }
else if(x1==pw[7].curx && y1==pw[7].cury )
{ selected=&pw[7]; id='p'; }
else if(x1==pb[0].curx && y1==pb[0].cury )
{ selected=&pb[0]; id='p'; }
else if(x1==pb[1].curx && y1==pb[1].cury )
{ selected=&pb[1]; id='p'; }
else if(x1==pb[2].curx && y1==pb[2].cury )
{ selected=&pb[2]; id='p'; }
else if(x1==pb[3].curx && y1==pb[3].cury )
{ selected=&pb[3]; id='p'; }
else if(x1==pb[4].curx && y1==pb[4].cury )
{ selected=&pb[4]; id='p'; }
else if(x1==pb[5].curx && y1==pb[5].cury )
{ selected=&pb[5]; id='p'; }
else if(x1==pb[6].curx && y1==pb[6].cury )
{ selected=&pb[6]; id='p'; }
else if(x1==pb[7].curx && y1==pb[7].cury )
{ selected=&pb[7]; id='p'; }
else if(x1==rookw[0].curx && y1==rookw[0].cury)
{ selected=&rookw[0]; id='r'; }
else if(x1==rookw[1].curx && y1==rookw[1].cury)
{ selected=&rookw[1]; id='r'; }
else if(x1==rookb[0].curx && y1==rookb[0].cury)
{ selected=&rookb[0]; id='r'; }
else if(x1==rookb[1].curx && y1==rookb[1].cury)
{ selected=&rookb[1]; id='r'; }
else if(x1==bishopw[0].curx && y1==bishopw[0].cury)
{ selected=&bishopw[0];id='b'; }
else if(x1==bishopw[1].curx && y1==bishopw[1].cury)
{ selected=&bishopw[1];id='b'; }
else if(x1==bishopb[0].curx && y1==bishopb[0].cury)
{ selected=&bishopb[0];id='b'; }
else if(x1==bishopb[1].curx && y1==bishopb[1].cury)
{ selected=&bishopb[1];id='b'; }
else if(x1==kingw.curx && y1==kingw.cury)
{ selected=&kingw; id='g'; }
else if(x1==queenw.curx && y1==queenw.cury)
{ selected=&queenw; id='q'; }
else if(x1==kingb.curx && y1==kingb.cury)
{ selected=&kingb; id='g'; }
else if(x1==queenb.curx && y1==queenb.cury)
{ selected=&queenb; id='q'; }
else if(x1==knightw[0].curx && y1==knightw[0].cury)
{ selected=&knightw[0];id='k'; }
else if(x1==knightw[1].curx && y1==knightw[1].cury)
{ selected=&knightw[1];id='k'; }
else if(x1==knightb[0].curx && y1==knightb[0].cury)
{ selected=&knightb[0];id='k'; }
else if(x1==knightb[1].curx && y1==knightb[1].cury)
{ selected=&knightb[1];id='k'; }
if((turn%2==0 && selected->clr=='b')||(turn%2!=0 &&
selected->clr=='w'))
exit();
turn=turn+1;
j=j+1;
while(j)
{
if(kbhit())
{
ch=getch();
if(ch=='
') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x2,&y2); //gets click position of mouse
if((button== 001))
{
sound(500);
j=j-1;
delay(100);
nosound();
delay(500);
}
}
x3=relposx(x2)/50; //gives standard x coordinate
y3=relposy(y2)/50; //gives standard y coordinate
nwblkclr=getblkcolor(x1,y1,x3,y3,selected->blkcolor);//get
destination
block's color;
gotoxy(6,28);
bottom(); //clears bottom display
if(id=='p') //wheteher pawn?
{
move= chkpawnmov(selected,x3,y3);
if(move=='v')
{
draw(x3,y3,selected,nwblkclr,'p');
checkpwn(x3,y3);
}
}
else if(id=='r') //whether rook?
{
move=chkrookmov(selected,x3,y3);
if(move=='v' )
{
draw(x3,y3,selected,nwblkclr,'r');
}
}
else if(id=='k') //whether knight?
{
move= chkknightmov(selected,x3,y3);
if(move=='v' )
{
draw(x3,y3,selected,nwblkclr,'k');
}
}
else if(id=='b') //whether bishop?
{
move=chkbishopmov(selected,x3,y3);
if(move=='v')
{
draw(x3,y3,selected,nwblkclr,'b');
}
}
else if(id=='q') //whether queen?
{
move=chkqueenmov(selected,x3,y3);
if(move=='v')
draw(x3,y3,selected,nwblkclr,'q');
}
else if(id=='g') //whether king?
{
move=chkkingmov(selected,x3,y3);
if(move=='v')
draw(x3,y3,selected,nwblkclr,'g');
}
if(move=='i') turn=turn-1;
}
getch();
}
getmousepos(int *button,int* x,int* y) //gets mouse position.
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
return;
}
showmouseptr() //shows pointer in graphics mode.
{
i.x.ax=1;
int86(0x33,&i,&o);
return;
}
void hidemouseptr() //hides pointer
{
i.x.ax=0;
int86(0x33,&i,&o);
return;
}
restrictmouseptr(int x1,int y1,int x2,int y2) //restrict pointer
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
return;
}
setposw(struct block* pawnw) //sete each white pawn.
{ int count,togl;
for(count=0,togl=0;count<=7;count++,togl++)
{
pawnw->curx=count;
pawnw->cury=6;
pawnw->clr='b';
if(togl%2==0)
pawnw->blkcolor='g';
else if(togl%2==1)
pawnw->blkcolor='m';
pawnw++;
}
return;
}
setposb(struct block* pawnb) //sets each black pawn.
{
int count,togl;
for(count=0,togl=0;count<=7;count++,togl++)
{
pawnb->curx=count;
pawnb->cury=1;
pawnb->clr='w';
if(togl%2==0)
pawnb->blkcolor='m';
else if(togl%2==1)
pawnb->blkcolor='g';
pawnb++;
}
return;
}
char chkpawnmov(struct block* selected,int x3,int y3)
{ char mov,id,c; //checks each pawn (black and white) move.
id=pos[x3][y3]; //anybody?
c=color[x3][y3]; //color?
if(selected->clr=='w') //white pawn?
{
if(x3==selected->curx && y3==selected->cury+1 ) //move 1
{ if(id=='n') //id is none when no body is there
mov='v';
}
else if( (x3==selected->curx+1 || x3==selected->curx-1) &&
y3==selected->cury+1)
{
if(id!='n' && c=='b') //black present?
{
mov='v';
send(id,c);
}
}
}
else if(selected->clr=='b')
{
if(x3==selected->curx && y3==selected->cury-1) //black and white
both
not there?
{ if(id=='n')
mov='v';
}
else if((x3==selected->curx-1 || x3==selected->curx+1) &&
y3==selected->cury-1)
{ if(id!='n' && c=='w' ) //white there
{
mov='v'; //valid move
send(id,c);
}
}
}
else
{ //no action at invalid move.
mov='i';
}
return(mov);
}
setposrw(struct block* rookw,struct block* bishopw,struct block*
knightw,struct block* kingw,struct block* queenw)
{ //it sets the white rook,knight,bishop etc.
int i;
rookw->curx=rookw->cury=7;
rookw->clr='w';
rookw->blkcolor='m';
rookw++;
rookw->curx=7;rookw->cury=7;
rookw->clr='w';
rookw->blkcolor='g';
bishopw->curx=2;
bishopw->cury=7;
bishopw->clr='w';
bishopw->blkcolor='m';
bishopw++;
bishopw->curx=5;
bishopw->cury=7;
bishopw->clr='w';
bishopw->blkcolor='g';
knightw->curx=1;
knightw->cury=7;
knightw->clr='w';
knightw->blkcolor='g';
knightw++;
knightw->curx=6;
knightw->cury=7;
knightw->clr='w';
knightw->blkcolor='m';
kingw->curx=3;kingw->cury=7;
kingw->clr='w';kingw->blkcolor='m';
queenw->curx=4;queenw->cury=7;
queenw->clr='w';queenw->blkcolor='g';
return;
}
setposrb(struct block* rookb,struct block* bishopb,struct block*
knightb,struct block* kingb,struct block* queenb)
{ //it sets the current coords of black rook,bishop,knight etc.
int i;
rookb->curx=0;rookb->cury=0;
rookb->clr='b';
rookb->blkcolor='g';
rookb++;
rookb->curx=7;rookb->cury=0;
rookb->clr='b';
rookb->blkcolor='m';
bishopb->curx=2;
bishopb->cury=0;
bishopb->clr='b';
bishopb->blkcolor='g';
bishopb++;
bishopb->curx=5;
bishopb->cury=0;
bishopb->clr='b';
bishopb->blkcolor='m';
knightb->curx=1;
knightb->cury=0;
knightb->clr='b';
knightb->blkcolor='m';
knightb++;
knightb->curx=6;
knightb->cury=0;
knightb->clr='b';
knightb->blkcolor='g';
kingb->curx=3;kingb->cury=0;
kingb->clr='b';kingb->blkcolor='m';
queenb->curx=4;queenb->cury=0;
queenb->clr='b';queenb->blkcolor='g';
return;
}
draw(int x3,int y3,struct block* selected,char nwblkclr,char id1)
{
int nx3,ny3,ncx,ncy;
nx3=x3*50; ny3=y3*50; ncx=(selected->curx)*50;
ncy=(selected->cury)*50;
if(nwblkclr=='g') //CLEAR NEW POSITION
setfillpattern(pattern,GREEN);
else if(nwblkclr=='m')
setfillpattern(pattern,MAGENTA);
hidemouseptr(); //necessary to avoid the effect of mouse pointer.
bar(nx3,ny3,nx3+50,ny3+50);
setcolor(WHITE);
rectangle(nx3,ny3,nx3+50,ny3+50);
if(selected->blkcolor=='g') //CLEAR PREVIOUS POSITION
setfillpattern(pattern,GREEN);
else if(selected->blkcolor=='m')
setfillpattern(pattern,MAGENTA); //clear the previous pos.
bar(ncx,ncy,ncx+50,ncy+50);
setcolor(WHITE);
rectangle(ncx,ncy,ncx+50,ncy+50);
if(id1=='p') //if pawn was selected then draw the pawn at new
position.
{ if(selected->clr=='w')
pawndraw(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
pawndraw(nx3,ny3,1);
}
else if(id1=='r') //if rook was selected
{ if(selected->clr=='w')
rookdraw(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
rookdraw(nx3,ny3,1);
}
else if(id1=='k') //if knight was selected
{ if(selected->clr=='w')
drawknite(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawknite(nx3,ny3,1);
}
else if(id1=='b') //if bishop was selected
{ if(selected->clr=='w')
drawbishop(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawbishop(nx3,ny3,1);
}
else if(id1=='g')
{ if(selected->clr=='w')
drawking(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawking(nx3,ny3,1);
}
else if(id1=='q')
{ if(selected->clr=='w')
drawqueen(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawqueen(nx3,ny3,1);
}
showmouseptr();
pos[selected->curx][selected->cury]='n';
color[selected->curx][selected->cury]='n'; //welcome back mr.
pointer.
selected->curx=x3;
selected->cury=y3;
selected->blkcolor=nwblkclr;
pos[x3][y3]=id1;
color[x3][y3]=selected->clr;
return;
}
char chkrookmov(struct block* selected,int x3,int y3)
{ //checks rook move for black and white as both have
same
moves.
int i;
char id,c,mov='v';
if( (x3==selected->curx) || (y3==selected->cury) )
{
if(x3>selected->curx && y3==selected->cury)
{
for(i=selected->curx+1;i<x3;i++)
{
id=pos[i][y3];
// c=color[i][y3];
if(id!='n') //if black or white present move is invalid
{
mov='i';
}
}
}
else if(x3<selected->curx && y3==selected->cury)
{
for(i=selected->curx-1;i>x3;i--)
{
id=pos[i][y3];
// c=color[i][y3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==selected->curx && y3>selected->cury)
{
for(i=selected->cury+1;i<y3;i++)
{
id=pos[x3][i];
// c=color[x3][i];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==selected->curx && y3<selected->cury)
{
for(i=selected->cury-1;i>y3;i--)
{
id=pos[x3][i];
// c=color[x3][i];
if(id!='n') //if black or white
{
mov='i';
}
}
}
if(mov=='v')
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n' && c=='b' && selected->clr=='w') //means black
at
x3,y3
{
mov='v';
send(id,c);
}
else if(id!='n' && c=='w' && selected->clr=='b') //means white
there
{
mov='v';
send(id,c);
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
else
mov='i';
}
}
else mov='i';
return(mov);
}
char chkknightmov(struct block* selected,int x3,int y3)
{
//knight's moves are not affected if some entity lies in the way.
//it just jumps over them.Atmost only eight moves are possible.
int i,posk[8][2];
char id,c,mov='i';
posk[0][0]=selected->curx+1;
posk[0][1]=selected->cury-2;
posk[1][0]=selected->curx-1;
posk[1][1]=selected->cury-2;
posk[2][0]=selected->curx+1;
posk[2][1]=selected->cury+2;
posk[3][0]=selected->curx-1;
posk[3][1]=selected->cury+2;
posk[4][0]=selected->curx+2;
posk[4][1]=selected->cury-1;
posk[5][0]=selected->curx+2;
posk[5][1]=selected->cury+1;
posk[6][0]=selected->curx-2;
posk[6][1]=selected->cury+1;
posk[7][0]=selected->curx-2;
posk[7][1]=selected->cury-1;
for(i=0;i<=7;i++)
{
if(x3==posk[i][0] && y3==posk[i][1])
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b' && selected->clr=='w')
{
mov='v'; //means black is there.
send(id,c);
}
else if(id!='n'&&c=='w' && selected->clr=='b')
{
mov='v'; //means white is there.
send(id,c);
}
else if(id=='n')
mov='v'; //means it is blank.
}
}
return(mov);
}
char chkbishopmov(struct block* selected,int x3,int y3)
{
int m,n,m1,n1,i,j;
char mov,id,c;
m=x3-selected->curx;
n=y3-selected->cury;
if(abs(m)==abs(n))
{ if(m>0 && n<0)//move 1
{ for(i=selected->curx+1,j=selected->cury-1;(i<x3 && j>y3);i=i++,j--)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(m>0 && n>0)//move 2
{ for(i=selected->curx+1,j=selected->cury+1;(i<x3 && j<y3);i++,j++)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(m<0 && n<0)//move 3
{ for(i=selected->curx-1,j=selected->cury-1;(i!=x3 && j!=y3);i--,j--)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(m<0 && n>0)//move 4
{ for(i=selected->curx-1,j=selected->cury+1;(i>x3 && j<y3);i--,j++)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(mov!='i') //..that no body is in the way,now at x3 y3?
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b') //means black at x3,y3
{
if(selected->clr=='w')
{
mov='v';
send(id,c);
}
else
mov='i';
}
else if(id!='n'&& c=='w') //means white there
{
if(selected->clr=='b')
{
mov='v';
send(id,c);
}
else
{ mov='i';}
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
}
} //means not at the x cross.
else
{
mov='i';
}
return(mov);
}
char chkqueenmov(struct block* selected,int x3,int y3)
{
char mov1,mov2,mov3='i',id,c;
mov1=chkrookmov(selected,x3,y3);
mov2=chkbishopmov(selected,x3,y3);
if(mov1=='v' || mov2=='v') //move is valid and color is
checked already.
{
mov3='v';
}
return(mov3);
}
char chkkingmov(struct block* selected,int x3,int y3)
{
char id,c,mov='i';
id=pos[x3][y3];
c=pos[x3][y3];
if(x3==selected->curx+1 && y3==selected->cury-1) //move 1
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx-1 && y3==selected->cury-1) //move 2
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx-1 && y3==selected->cury+1) //move 3
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx+1 && y3==selected->cury+1) //move 4
{
if(id=='n' )
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx+1 && y3==selected->cury) //move 5
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx && y3==selected->cury-1) //move 6
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx-1 && y3==selected->cury) //move 7
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx && y3==selected->cury+1) //move 8
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else //means move is not valid
{ mov='i';}
return(mov);
}
void assign(char* post,char* col)
{ int i;
char
val1[64]={"rpnnnnprkpnnnnpkbpnnnnpbgpnnnnpqqpnnnnpgbpnnnnpbkpnnnnpkrpnnnnpr"};
char
val2[64]={"wwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbb"};
for(i=0;i<=63;i++,post++,col++)
{ *post=val1[i]; *col=val2[i]; }
}
void checkpwn(int x3,int y3)
{
char a;
a=color[x3][y3];//whether pawn is white or black?
bottom();
setcolor(WHITE);
if(a=='w' && (x3>=0 && x3<=7) && y3==7)
{
outtextxy(100,430,"well done white..press w");
getch(); bottom();
}
else if(a=='b' && (x3>=0 && x3<=7) && y3==0)
{
outtextxy(100,430,"well done black..press b");
getch(); bottom();
}
return;
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void blk(int,int);
void display();
//enum bool{TRUE,FALSE};
/*void main() //MAIN FUNCTION
{
display();
getch();
} */
void display() //DISPLAY TO SHOW THE BOARD
{
int gd=DETECT,gm,i,j,l,m,b;
char pattern[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
int arrodd[32][2]={ {0,0},{100,0},{200,0},{300,0},{50,50},{150,50}
,{250,50},{350,50},{0,100},{100,100},{200,100},{300,100}
,{50,150},{150,150},{250,150},{350,150},{0,200},{100,200}
,{200,200},{300,200},{50,250},{150,250},{250,250},{350,250},
{0,300},{100,300},{200,300},{300,300},{50,350},{150,350},
{250,350},{350,350} };
int
arrevn[33][2]={{50,0},{150,0},{250,0},{350,0},{0,50},{100,50},{200,50},
{300,50},{50,100},{150,100},{250,100},{350,100},{0,150},
{100,150},{200,150},{300,150},{50,200},{150,200},{250,200},
{350,200},{0,250},{100,250},{200,250},{300,250},{50,300},
{150,300},{250,300},{350,300},{0,350},{100,350},{200,350},
{300,350}};
clrscr();
initgraph(&gd,&gm,"");
for(i=0;i<=400;i=i+50) //to draw vertical lines of the chess board.
{
line(i,0,i,400);
}
for(j=0;j<=400;j=j+50) //to draw the horizontal lines of the chess
board.
{
line(0,j,400,j);
}
blk(420,0);
blk(420,250);
line(0,430,0,460); //to draw the display at the bottom
line(0,430,400,430);
line(0,460,400,460);
line(400,430,400,460);
settextstyle(7,HORIZ_DIR,3);
setfillpattern(&pattern,GREEN);
for(b=0;b<=31;b++)
{
bar(arrodd[b][0],arrodd[b][1],arrodd[b][0]+50,arrodd[b][1]+50);
rectangle(arrodd[b][0],arrodd[b][1],arrodd[b][0]+50,arrodd[b][1]+50);
}
setfillpattern(&pattern,DARKGRAY);
for(m=0;m<=31;m++)
{
bar(arrevn[m][0],arrevn[m][1],arrevn[m][0]+50,arrevn[m][1]+50);
rectangle(arrevn[m][0],arrevn[m][1],arrevn[m][0]+50,arrevn[m][1]+50);
}
}
void blk(int q,int r)
{
int y,z;
for(y=q;y<=(q+200);y=y+50)
{ line(y,r,y,r+200);}
for(z=r;z<=(r+200);z=z+50)
{ line(q,z,q+200,z);}
}
//Source file//
#include<akhchs.c> //we used akhchs.c as header file
#include<stdio.h>
#include<conio.h>
#include<dos.h>
char pos[8][8],color[8][8];
void assign(char*,char*);
struct block{ //structure for every block
int curx,cury;
char clr,blkcolor;
};
union REGS i,o; //for hardware interaction
char getblkcolor(int,int,int,int,char); //functions prototype
char chkpawnmov(struct block* selected,int x3,int y3);
char chkrookmov(struct block* selected,int x3,int y3);
char chkknightmov(struct block* selected,int x3,int y3);
char chkbishopmov(struct block* selected,int x3,int y3);
char chkqueenmov(struct block* selected,int x3,int y3);
char chkkingmov(struct block* selected,int x3,int y3);
void checkpwn(int,int);
void main()
{
int i1,j1,button,x,y,x1,y1,x2,y2,x3,y3,ch,i=0,j=0,z=1,turn=0;
struct block
*selected,pw[8],pb[8],rookw[2],rookb[2],bishopw[2],bishopb[2];
struct block kingw,kingb,queenw,queenb,knightw[2],knightb[2];
char id,nwblkclr,move='i';
assign(pos,color);
clrscr();
display(); //for showing chess board
design();
setspare(); //for drawing rook etc.
setposw(pw); //sets position of black and
setposb(pb); //white pawns with color
setposrw(rookw,bishopw,knightw,&kingw,&queenw);//sets position of
whites
setposrb(rookb,bishopb,knightb,&kingb,&queenb);//sete position of
blacks
//restrictmouseptr(0,0,400,400); //restricts pointer of mouse in
the
area specified there.
showmouseptr();
while(z)
{
i=i+1;
setcolor(WHITE);
if(turn%2==0) //means white's turn
outtextxy(100,430,"white's turn");
else if(turn%2!=0) //means black's turn
outtextxy(100,430,"black's turn");
while(i)
{
if(kbhit())
{
ch=getch();
if(ch=='
') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x,&y); //gets click position of mouse
if((button== 001))
{
sound(1000);
i=i-1;
delay(50);
nosound();
delay(500);
}
}
x1=relposx(x)/50;
y1=relposy(y)/50;
gotoxy(6,28);
if(x1==pw[0].curx && y1==pw[0].cury )
{ selected=&pw[0]; id='p'; }
else if(x1==pw[1].curx && y1==pw[1].cury )
{ selected=&pw[1]; id='p'; }
else if(x1==pw[2].curx && y1==pw[2].cury )
{ selected=&pw[2]; id='p'; }
else if(x1==pw[3].curx && y1==pw[3].cury )
{ selected=&pw[3]; id='p'; }
else if(x1==pw[4].curx && y1==pw[4].cury )
{ selected=&pw[4]; id='p'; }
else if(x1==pw[5].curx && y1==pw[5].cury )
{ selected=&pw[5]; id='p'; }
else if(x1==pw[6].curx && y1==pw[6].cury )
{ selected=&pw[6]; id='p'; }
else if(x1==pw[7].curx && y1==pw[7].cury )
{ selected=&pw[7]; id='p'; }
else if(x1==pb[0].curx && y1==pb[0].cury )
{ selected=&pb[0]; id='p'; }
else if(x1==pb[1].curx && y1==pb[1].cury )
{ selected=&pb[1]; id='p'; }
else if(x1==pb[2].curx && y1==pb[2].cury )
{ selected=&pb[2]; id='p'; }
else if(x1==pb[3].curx && y1==pb[3].cury )
{ selected=&pb[3]; id='p'; }
else if(x1==pb[4].curx && y1==pb[4].cury )
{ selected=&pb[4]; id='p'; }
else if(x1==pb[5].curx && y1==pb[5].cury )
{ selected=&pb[5]; id='p'; }
else if(x1==pb[6].curx && y1==pb[6].cury )
{ selected=&pb[6]; id='p'; }
else if(x1==pb[7].curx && y1==pb[7].cury )
{ selected=&pb[7]; id='p'; }
else if(x1==rookw[0].curx && y1==rookw[0].cury)
{ selected=&rookw[0]; id='r'; }
else if(x1==rookw[1].curx && y1==rookw[1].cury)
{ selected=&rookw[1]; id='r'; }
else if(x1==rookb[0].curx && y1==rookb[0].cury)
{ selected=&rookb[0]; id='r'; }
else if(x1==rookb[1].curx && y1==rookb[1].cury)
{ selected=&rookb[1]; id='r'; }
else if(x1==bishopw[0].curx && y1==bishopw[0].cury)
{ selected=&bishopw[0];id='b'; }
else if(x1==bishopw[1].curx && y1==bishopw[1].cury)
{ selected=&bishopw[1];id='b'; }
else if(x1==bishopb[0].curx && y1==bishopb[0].cury)
{ selected=&bishopb[0];id='b'; }
else if(x1==bishopb[1].curx && y1==bishopb[1].cury)
{ selected=&bishopb[1];id='b'; }
else if(x1==kingw.curx && y1==kingw.cury)
{ selected=&kingw; id='g'; }
else if(x1==queenw.curx && y1==queenw.cury)
{ selected=&queenw; id='q'; }
else if(x1==kingb.curx && y1==kingb.cury)
{ selected=&kingb; id='g'; }
else if(x1==queenb.curx && y1==queenb.cury)
{ selected=&queenb; id='q'; }
else if(x1==knightw[0].curx && y1==knightw[0].cury)
{ selected=&knightw[0];id='k'; }
else if(x1==knightw[1].curx && y1==knightw[1].cury)
{ selected=&knightw[1];id='k'; }
else if(x1==knightb[0].curx && y1==knightb[0].cury)
{ selected=&knightb[0];id='k'; }
else if(x1==knightb[1].curx && y1==knightb[1].cury)
{ selected=&knightb[1];id='k'; }
if((turn%2==0 && selected->clr=='b')||(turn%2!=0 &&
selected->clr=='w'))
exit();
turn=turn+1;
j=j+1;
while(j)
{
if(kbhit())
{
ch=getch();
if(ch=='
') //escape at pressing enter.
exit(0);
}
getmousepos(&button,&x2,&y2); //gets click position of mouse
if((button== 001))
{
sound(500);
j=j-1;
delay(100);
nosound();
delay(500);
}
}
x3=relposx(x2)/50; //gives standard x coordinate
y3=relposy(y2)/50; //gives standard y coordinate
nwblkclr=getblkcolor(x1,y1,x3,y3,selected->blkcolor);//get
destination
block's color;
gotoxy(6,28);
bottom(); //clears bottom display
if(id=='p') //wheteher pawn?
{
move= chkpawnmov(selected,x3,y3);
if(move=='v')
{
draw(x3,y3,selected,nwblkclr,'p');
checkpwn(x3,y3);
}
}
else if(id=='r') //whether rook?
{
move=chkrookmov(selected,x3,y3);
if(move=='v' )
{
draw(x3,y3,selected,nwblkclr,'r');
}
}
else if(id=='k') //whether knight?
{
move= chkknightmov(selected,x3,y3);
if(move=='v' )
{
draw(x3,y3,selected,nwblkclr,'k');
}
}
else if(id=='b') //whether bishop?
{
move=chkbishopmov(selected,x3,y3);
if(move=='v')
{
draw(x3,y3,selected,nwblkclr,'b');
}
}
else if(id=='q') //whether queen?
{
move=chkqueenmov(selected,x3,y3);
if(move=='v')
draw(x3,y3,selected,nwblkclr,'q');
}
else if(id=='g') //whether king?
{
move=chkkingmov(selected,x3,y3);
if(move=='v')
draw(x3,y3,selected,nwblkclr,'g');
}
if(move=='i') turn=turn-1;
}
getch();
}
getmousepos(int *button,int* x,int* y) //gets mouse position.
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
return;
}
showmouseptr() //shows pointer in graphics mode.
{
i.x.ax=1;
int86(0x33,&i,&o);
return;
}
void hidemouseptr() //hides pointer
{
i.x.ax=0;
int86(0x33,&i,&o);
return;
}
restrictmouseptr(int x1,int y1,int x2,int y2) //restrict pointer
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
return;
}
setposw(struct block* pawnw) //sete each white pawn.
{ int count,togl;
for(count=0,togl=0;count<=7;count++,togl++)
{
pawnw->curx=count;
pawnw->cury=6;
pawnw->clr='b';
if(togl%2==0)
pawnw->blkcolor='g';
else if(togl%2==1)
pawnw->blkcolor='m';
pawnw++;
}
return;
}
setposb(struct block* pawnb) //sets each black pawn.
{
int count,togl;
for(count=0,togl=0;count<=7;count++,togl++)
{
pawnb->curx=count;
pawnb->cury=1;
pawnb->clr='w';
if(togl%2==0)
pawnb->blkcolor='m';
else if(togl%2==1)
pawnb->blkcolor='g';
pawnb++;
}
return;
}
char chkpawnmov(struct block* selected,int x3,int y3)
{ char mov,id,c; //checks each pawn (black and white) move.
id=pos[x3][y3]; //anybody?
c=color[x3][y3]; //color?
if(selected->clr=='w') //white pawn?
{
if(x3==selected->curx && y3==selected->cury+1 ) //move 1
{ if(id=='n') //id is none when no body is there
mov='v';
}
else if( (x3==selected->curx+1 || x3==selected->curx-1) &&
y3==selected->cury+1)
{
if(id!='n' && c=='b') //black present?
{
mov='v';
send(id,c);
}
}
}
else if(selected->clr=='b')
{
if(x3==selected->curx && y3==selected->cury-1) //black and white
both
not there?
{ if(id=='n')
mov='v';
}
else if((x3==selected->curx-1 || x3==selected->curx+1) &&
y3==selected->cury-1)
{ if(id!='n' && c=='w' ) //white there
{
mov='v'; //valid move
send(id,c);
}
}
}
else
{ //no action at invalid move.
mov='i';
}
return(mov);
}
setposrw(struct block* rookw,struct block* bishopw,struct block*
knightw,struct block* kingw,struct block* queenw)
{ //it sets the white rook,knight,bishop etc.
int i;
rookw->curx=rookw->cury=7;
rookw->clr='w';
rookw->blkcolor='m';
rookw++;
rookw->curx=7;rookw->cury=7;
rookw->clr='w';
rookw->blkcolor='g';
bishopw->curx=2;
bishopw->cury=7;
bishopw->clr='w';
bishopw->blkcolor='m';
bishopw++;
bishopw->curx=5;
bishopw->cury=7;
bishopw->clr='w';
bishopw->blkcolor='g';
knightw->curx=1;
knightw->cury=7;
knightw->clr='w';
knightw->blkcolor='g';
knightw++;
knightw->curx=6;
knightw->cury=7;
knightw->clr='w';
knightw->blkcolor='m';
kingw->curx=3;kingw->cury=7;
kingw->clr='w';kingw->blkcolor='m';
queenw->curx=4;queenw->cury=7;
queenw->clr='w';queenw->blkcolor='g';
return;
}
setposrb(struct block* rookb,struct block* bishopb,struct block*
knightb,struct block* kingb,struct block* queenb)
{ //it sets the current coords of black rook,bishop,knight etc.
int i;
rookb->curx=0;rookb->cury=0;
rookb->clr='b';
rookb->blkcolor='g';
rookb++;
rookb->curx=7;rookb->cury=0;
rookb->clr='b';
rookb->blkcolor='m';
bishopb->curx=2;
bishopb->cury=0;
bishopb->clr='b';
bishopb->blkcolor='g';
bishopb++;
bishopb->curx=5;
bishopb->cury=0;
bishopb->clr='b';
bishopb->blkcolor='m';
knightb->curx=1;
knightb->cury=0;
knightb->clr='b';
knightb->blkcolor='m';
knightb++;
knightb->curx=6;
knightb->cury=0;
knightb->clr='b';
knightb->blkcolor='g';
kingb->curx=3;kingb->cury=0;
kingb->clr='b';kingb->blkcolor='m';
queenb->curx=4;queenb->cury=0;
queenb->clr='b';queenb->blkcolor='g';
return;
}
draw(int x3,int y3,struct block* selected,char nwblkclr,char id1)
{
int nx3,ny3,ncx,ncy;
nx3=x3*50; ny3=y3*50; ncx=(selected->curx)*50;
ncy=(selected->cury)*50;
if(nwblkclr=='g') //CLEAR NEW POSITION
setfillpattern(pattern,GREEN);
else if(nwblkclr=='m')
setfillpattern(pattern,MAGENTA);
hidemouseptr(); //necessary to avoid the effect of mouse pointer.
bar(nx3,ny3,nx3+50,ny3+50);
setcolor(WHITE);
rectangle(nx3,ny3,nx3+50,ny3+50);
if(selected->blkcolor=='g') //CLEAR PREVIOUS POSITION
setfillpattern(pattern,GREEN);
else if(selected->blkcolor=='m')
setfillpattern(pattern,MAGENTA); //clear the previous pos.
bar(ncx,ncy,ncx+50,ncy+50);
setcolor(WHITE);
rectangle(ncx,ncy,ncx+50,ncy+50);
if(id1=='p') //if pawn was selected then draw the pawn at new
position.
{ if(selected->clr=='w')
pawndraw(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
pawndraw(nx3,ny3,1);
}
else if(id1=='r') //if rook was selected
{ if(selected->clr=='w')
rookdraw(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
rookdraw(nx3,ny3,1);
}
else if(id1=='k') //if knight was selected
{ if(selected->clr=='w')
drawknite(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawknite(nx3,ny3,1);
}
else if(id1=='b') //if bishop was selected
{ if(selected->clr=='w')
drawbishop(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawbishop(nx3,ny3,1);
}
else if(id1=='g')
{ if(selected->clr=='w')
drawking(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawking(nx3,ny3,1);
}
else if(id1=='q')
{ if(selected->clr=='w')
drawqueen(nx3,ny3,0); // 0 for white.
else if(selected->clr=='b')
drawqueen(nx3,ny3,1);
}
showmouseptr();
pos[selected->curx][selected->cury]='n';
color[selected->curx][selected->cury]='n'; //welcome back mr.
pointer.
selected->curx=x3;
selected->cury=y3;
selected->blkcolor=nwblkclr;
pos[x3][y3]=id1;
color[x3][y3]=selected->clr;
return;
}
char chkrookmov(struct block* selected,int x3,int y3)
{ //checks rook move for black and white as both have
same
moves.
int i;
char id,c,mov='v';
if( (x3==selected->curx) || (y3==selected->cury) )
{
if(x3>selected->curx && y3==selected->cury)
{
for(i=selected->curx+1;i<x3;i++)
{
id=pos[i][y3];
// c=color[i][y3];
if(id!='n') //if black or white present move is invalid
{
mov='i';
}
}
}
else if(x3<selected->curx && y3==selected->cury)
{
for(i=selected->curx-1;i>x3;i--)
{
id=pos[i][y3];
// c=color[i][y3];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==selected->curx && y3>selected->cury)
{
for(i=selected->cury+1;i<y3;i++)
{
id=pos[x3][i];
// c=color[x3][i];
if(id!='n') //if black or white
{
mov='i';
}
}
}
else if(x3==selected->curx && y3<selected->cury)
{
for(i=selected->cury-1;i>y3;i--)
{
id=pos[x3][i];
// c=color[x3][i];
if(id!='n') //if black or white
{
mov='i';
}
}
}
if(mov=='v')
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n' && c=='b' && selected->clr=='w') //means black
at
x3,y3
{
mov='v';
send(id,c);
}
else if(id!='n' && c=='w' && selected->clr=='b') //means white
there
{
mov='v';
send(id,c);
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
else
mov='i';
}
}
else mov='i';
return(mov);
}
char chkknightmov(struct block* selected,int x3,int y3)
{
//knight's moves are not affected if some entity lies in the way.
//it just jumps over them.Atmost only eight moves are possible.
int i,posk[8][2];
char id,c,mov='i';
posk[0][0]=selected->curx+1;
posk[0][1]=selected->cury-2;
posk[1][0]=selected->curx-1;
posk[1][1]=selected->cury-2;
posk[2][0]=selected->curx+1;
posk[2][1]=selected->cury+2;
posk[3][0]=selected->curx-1;
posk[3][1]=selected->cury+2;
posk[4][0]=selected->curx+2;
posk[4][1]=selected->cury-1;
posk[5][0]=selected->curx+2;
posk[5][1]=selected->cury+1;
posk[6][0]=selected->curx-2;
posk[6][1]=selected->cury+1;
posk[7][0]=selected->curx-2;
posk[7][1]=selected->cury-1;
for(i=0;i<=7;i++)
{
if(x3==posk[i][0] && y3==posk[i][1])
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b' && selected->clr=='w')
{
mov='v'; //means black is there.
send(id,c);
}
else if(id!='n'&&c=='w' && selected->clr=='b')
{
mov='v'; //means white is there.
send(id,c);
}
else if(id=='n')
mov='v'; //means it is blank.
}
}
return(mov);
}
char chkbishopmov(struct block* selected,int x3,int y3)
{
int m,n,m1,n1,i,j;
char mov,id,c;
m=x3-selected->curx;
n=y3-selected->cury;
if(abs(m)==abs(n))
{ if(m>0 && n<0)//move 1
{ for(i=selected->curx+1,j=selected->cury-1;(i<x3 && j>y3);i=i++,j--)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(m>0 && n>0)//move 2
{ for(i=selected->curx+1,j=selected->cury+1;(i<x3 && j<y3);i++,j++)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(m<0 && n<0)//move 3
{ for(i=selected->curx-1,j=selected->cury-1;(i!=x3 && j!=y3);i--,j--)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(m<0 && n>0)//move 4
{ for(i=selected->curx-1,j=selected->cury+1;(i>x3 && j<y3);i--,j++)
{
id=pos[i][j];
// c=color[i][j];
if(id!='n')
mov='i';
}
}
if(mov!='i') //..that no body is in the way,now at x3 y3?
{
id=pos[x3][y3];
c=color[x3][y3];
if(id!='n'&& c=='b') //means black at x3,y3
{
if(selected->clr=='w')
{
mov='v';
send(id,c);
}
else
mov='i';
}
else if(id!='n'&& c=='w') //means white there
{
if(selected->clr=='b')
{
mov='v';
send(id,c);
}
else
{ mov='i';}
}
else if(id=='n')//none of the white or black even at x3 & y3?
mov='v';
}
} //means not at the x cross.
else
{
mov='i';
}
return(mov);
}
char chkqueenmov(struct block* selected,int x3,int y3)
{
char mov1,mov2,mov3='i',id,c;
mov1=chkrookmov(selected,x3,y3);
mov2=chkbishopmov(selected,x3,y3);
if(mov1=='v' || mov2=='v') //move is valid and color is
checked already.
{
mov3='v';
}
return(mov3);
}
char chkkingmov(struct block* selected,int x3,int y3)
{
char id,c,mov='i';
id=pos[x3][y3];
c=pos[x3][y3];
if(x3==selected->curx+1 && y3==selected->cury-1) //move 1
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx-1 && y3==selected->cury-1) //move 2
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx-1 && y3==selected->cury+1) //move 3
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx+1 && y3==selected->cury+1) //move 4
{
if(id=='n' )
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx+1 && y3==selected->cury) //move 5
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w') //means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx && y3==selected->cury-1) //move 6
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx-1 && y3==selected->cury) //move 7
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else if(x3==selected->curx && y3==selected->cury+1) //move 8
{
if(id=='n')
mov='v';
else if(selected->clr=='w' && id!='n' && c=='b')//means black there.
{
mov='v';
send(id,c);
}
else if(selected->clr=='b' && id!='n' && c=='w')//means white there.
{
mov='v';
send(id,c);
}
}
else //means move is not valid
{ mov='i';}
return(mov);
}
void assign(char* post,char* col)
{ int i;
char
val1[64]={"rpnnnnprkpnnnnpkbpnnnnpbgpnnnnpqqpnnnnpgbpnnnnpbkpnnnnpkrpnnnnpr"};
char
val2[64]={"wwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbbwwnnnnbb"};
for(i=0;i<=63;i++,post++,col++)
{ *post=val1[i]; *col=val2[i]; }
}
void checkpwn(int x3,int y3)
{
char a;
a=color[x3][y3];//whether pawn is white or black?
bottom();
setcolor(WHITE);
if(a=='w' && (x3>=0 && x3<=7) && y3==7)
{
outtextxy(100,430,"well done white..press w");
getch(); bottom();
}
else if(a=='b' && (x3>=0 && x3<=7) && y3==0)
{
outtextxy(100,430,"well done black..press b");
getch(); bottom();
}
return;
}
Nah bagaimana?? Tadi itu adalah 5 bahasa pemrograman C yang diaplikasikan kedalam sebuah game sederhana. Jika kalian berminat untuk membanguns sebuah game kalian harus mempelajari dasar - dasarnya dulu guys. Oke jika ada pertanyaan atau saran kalian bisa menulisnya di kolom komentar.
Sekian terimakasih semoga artikel ini dapat membantu :D
Tidak ada komentar:
Posting Komentar
Komentarlah dengan kata kata yang baik dan bijaksana