Donatur utama

Kamis, 23 Oktober 2008

Kode Untuk Program Ular Sederhana

# include
# include
# include

struct segment
{
int x;
int y;
}tembok[240],makanan;


main()
{
char tekan,arah='d',counter='a';
struct segment snake[500];
int i,j,k,kena=0,benar=0,panjang=10,posisi=1,score=0,sp=5;

randomize();

for(i=0;i<10;i++)
{
tembok[i].y=1;
tembok[i].x=1+i;
}

for (i=0;i{
snake[i].x=40+i;
snake[i].y=12;
}

clrscr();

do
{

for(i=0;i<204;i++)
{
if((i%20==0 && i!=0) || kena==1)
{
if(kena!=1)
{gotoxy(makanan.x,makanan.y);
printf(" ");
}

do
{
makanan.x=random(77)+2;
makanan.y=random(21)+2;
for (j=0;jif (snake[j].x==makanan.x && snake[j].y==makanan.y) benar=1;

}
while(benar==1);
benar=0;
gotoxy(makanan.x,makanan.y);
printf("@");
kena=0;
}


for(j=0;j{
gotoxy (snake[j].x,snake[j].y); printf("*");
}

for(j=0;j{
gotoxy(tembok[j].x,tembok[j].y);
printf("O");
}

for(j=0;j<3;j++)
delay(30000);

if (snake[sp-1].x==makanan.x && snake[sp-1].y==makanan.y) {score+=10;sp++;}

for(j=0;jfor (k=0;k{
if(tembok[j].x==snake[k].x && tembok[j].y==snake[k].y)
{
gotoxy(30,12);
printf("Game Over (score : %d)",score);
tekan=27;
getch();
}
}

gotoxy(tembok[0].x,tembok[0].y); printf(" ");
gotoxy(snake[0].x,snake[0].y); printf(" ");


if (kbhit()) tekan=getch();

if ((tekan =='w' || tekan=='a' || tekan=='s' || tekan=='d') && tekan!=counter) arah=tekan;
{switch (arah)
{
case 'w' :
{snake[sp].x=snake[sp-1].x;
snake[sp].y=snake[sp-1].y-1;
counter='s';
break;}
case 's' :
{snake[sp].x=snake[sp-1].x;
snake[sp].y=snake[sp-1].y+1;
counter='w';
break;}
case 'a' :
{snake[sp].x=snake[sp-1].x-1;
snake[sp].y=snake[sp-1].y;
counter='d';
break;}
case'd':
{snake[sp].x=snake[sp-1].x+1;
snake[sp].y=snake[sp-1].y;
counter='a';
break;}
}
}
if(tekan==27) break;

if (snake[sp].x==81) snake[sp].x=1;
if (snake[sp].x==0) snake[sp].x=80;
if (snake[sp].y==25) snake[sp].y=1;
if (snake[sp].y==0) snake[sp].y=24;


if (posisi==1)
{ tembok[panjang].x=tembok[panjang-1].x+1;
tembok[panjang].y=tembok[panjang-1].y;
if (tembok[panjang].x==80) posisi=2;
}
else if (posisi==2)
{
tembok[panjang].x=tembok[panjang-1].x;
tembok[panjang].y=tembok[panjang-1].y+1;
if (tembok[panjang].y==24) posisi=3;
}
else if (posisi==3)
{
tembok[panjang].x=tembok[panjang-1].x-1;
tembok[panjang].y=tembok[panjang-1].y;
if (tembok[panjang].x==1) posisi=4;
}
else
{
tembok[panjang].x=tembok[panjang-1].x;
tembok[panjang].y=tembok[panjang-1].y-1;
if (tembok[panjang].y==1) {posisi=1;}
}

for (j=0;jtembok[j]=tembok[j+1];

for (j=0;jsnake[j]=snake[j+1];

delay(200);

}
panjang++;
}
while(tekan!=27);
}

0 komentar:

 

Berlangganan

Enter your email address:

Delivered by FeedBurner

Belajar Tik


Pengikut

Komentar Terbaru

Belajar Tik
Tempatnya Belajar Teknologi Informasi dan Komunikasi

Administrator By : 3eta