Snake


/* Snake Snake Snake Snake Snake Snake Snake Snake */

#include
#include
#include
#include
#include
#include
#include
typedef struct tikke{
int x,y;
struct tikke *next;
}tikke;

tikke *start,*check;
int tt;

class toop{
public:
int rx,ry,sign;
float punkt;
toop(){
sign=1;
punkt=0;
rx=50;
ry=12;
gotoxy(rx+6,ry+6);
cout<<"*";
}

void rtoop(){
randomize();
rx=rand() % 68;
ry=rand() % 15;
/* check=start;
for(int i=1;i<tt-2;i++){
if((rx+6)==check->x && (ry+6)==check->y)
rtoop();
check=check->next;
} */
gotoxy(rx+6,ry+6);
cout<<"*";
punkt+=10;
sign=-sign;
}
};


class mar:private toop{
int i;
tikke *rec;
public:
mar(){
start=new tikke[1];
start->x=1;
start->y=12;
rec=start;
gotoxy(rec->x,rec->y);
cout<<"";
for(i=2;i<11;i++){
rec->next=new tikke[1];
rec->next->x=i;
rec->next->y=12;
gotoxy(rec->next->x,rec->next->y);
cout<<"";
rec=rec->next;
}
tt=10;

}

void moving(char ch){
if(sign==1){
rec=start;
gotoxy(rec->x,rec->y);
cout<<" ";

for(i=tt-1;i!=0;i--){
rec->x=rec->next->x;
rec->y=rec->next->y;
rec=rec->next;
}
}

if(sign==-1){
rec->next=new tikke[1];
rec->next->x=rec->x;
rec->next->y=rec->y;
sign=1;
tt++;
}

switch (ch){
case 'M':
rec->x++;
break;
case 'K':
rec->x--;
break;
case 'H':
rec->y--;
break;
case 'P':
rec->y++;
break;
default:{
_setcursortype(_NORMALCURSOR);
exit(0);
}
}



checking();
gotoxy(rec->x,rec->y);
cout<<"";
}
void checking(){
check=start;
for(i=1;i<tt-2;i++){
if(check->x==rec->x && check->y==rec->y){
_setcursortype(_NORMALCURSOR);
sound(600);
delay(320);
nosound();
gotoxy(30,1);
cout << "**** Game Over ****
";
cout << punkt;
delay(3000);
exit(1);
}
check=check->next;
}
if(rec->x==75 || rec->y==22 || rec->x==5 || rec->y==3){
_setcursortype(_NORMALCURSOR);
sound(600);
delay(320);
nosound();
gotoxy(30,1);
cout << "**** Game Over ****
";
cout << punkt;
delay(2000);
exit(1);
}
if(rec->x==rx+6 && rec->y==ry+6){
sound(1100);
delay(10);
nosound();
rtoop();
}
}
};
void hashie(){
int i=5,j=3;
for(j=3;j<23;j++){
gotoxy(i,j);
cout << "";
i=75;
gotoxy(i,j);
cout << "";
i=5;
}
j=3;
for(i=6;i<75;i++){
gotoxy(i,j);
cout << "";
j=22;
gotoxy(i,j);
cout << "";
j=3;
}
}

void main(){
clrscr();
fflush(stdin);
hashie();
toop too;
mar badan;
char cht,ch='M';
_setcursortype(_NOCURSOR);

te:

for(;;){
while(!kbhit()){
delay(50);
badan.moving(ch);
}

ch=getch();
ch=getch();
if(ch==cht){
if(cht=='M')
ch='K';
if(cht=='K')
ch='M';
if(cht=='P')
ch='H';
if(cht=='H')
ch='P';
goto te;
}
if(ch=='M')
cht='K';
if(ch=='K')
cht='M';
if(ch=='P')
cht='H';
if(ch=='H')
cht='P';

}

}

Comments

  • Good program man. you should try it in graphics mode.
    Here is the one I have written. But it is in c and not in c++


    #include
    #include
    #include

    int arr_s[20][15],x,y,d,n,one_s[300][2],rx,ry;
    /////////////////////////////////////////////
    void initialise_s()
    {
    int i,j;
    for (i=0;i<20;i++)
    for (j=0;j<15;j++)
    arr_s[i][j]=0;
    for (i=0;i<300;i++)
    {
    one_s[i][1]=-1;
    one_s[i][0]=-1;
    }
    for (i=0;i<5;i++)
    {
    arr_s[i][0]=1;
    one_s[i][0]=4-i;
    one_s[i][1]=0;
    }
    rx=5;ry=5;
    arr_s[rx][ry]=2;
    x=4;y=0;d='r';n=5;
    }
    /////////////////////////////////////////////
    void square_s(int i,int j,int k)
    {
    setcolor(k);
    for (k=0;k<16;k++)
    rectangle ( 22+i*30+k , 17+j*30+k , 48+i*30-k , 43+j*30-k );
    }
    /////////////////////////////////////////////
    void display_s ()
    {
    int i,j,k;
    setcolor(3);
    for (j=0;j<20;j++)
    for (k=0;k<15;k++)
    {
    if (arr_s[j][k]==1)
    square_s(j,k,3);
    else
    if (arr_s[j][k]==2)
    square_s(j,k,4);
    else
    if (arr_s[j][k]==0)
    square_s(j,k,0);
    }
    }
    /////////////////////////////////////////////
    int check_s()
    {
    int i,j,flag;
    if ((x==rx)&&(y==ry))
    {
    for (i=n;i>0;i--)
    {
    one_s[i][0]=one_s[i-1][0];
    one_s[i][1]=one_s[i-1][1];
    }
    one_s[0][0]=rx;
    one_s[0][1]=ry;
    n++;
    do
    {
    rx=rand()%20;
    ry=rand()%15;
    if (rx<0) rx*=-1;
    if (ry<0) ry*=-1;
    flag=1;
    for (i=0;i<n;i++)
    if ((one_s[i][0]==rx)&&(one_s[i][1]==ry))
    {
    flag=0;
    break;
    }
    } while(flag==0);
    arr_s[rx][ry]=2;
    return(1);
    }
    if ((y<0)||(x<0)||(y>15)||(x>20))
    return(2);
    for (i=0;i=0;i--)
    {
    one_s[i][0]=one_s[i-1][0];
    one_s[i][1]=one_s[i-1][1];
    }
    one_s[0][0]=x;
    one_s[0][1]=y;
    }
    if (i==2) end_s();
    arr_s[x][y]=1;
    }
    /////////////////////////////////////////////
    void main()
    {
    int i;
    char c;
    int gd=0,gm;
    initgraph(&gd,&gm,"d:\programs\tcc\program");
    setcolor(15);
    rectangle(0,0,639,479);
    rectangle(18,13,622,467);
    setcolor(1);
    line (1,12,638,12);
    line (17,12,17,467);
    for (i=1;i<12;i++)
    {
    line (1,i,638,i);
    line (1,479-i,638,479-i);
    }
    for (i=1;i<17;i++)
    {
    line (i,12,i,467);
    line (639-i,12,639-i,467);
    }
    initialise_s();
    do
    {
    if (kbhit())
    {
    c=getch();
    if (c==0)
    {
    c=getch();
    if (c==75) // Left.
    if ((d!='l')&&(d!='r')) d='l';
    if (c==77) // Right
    if ((d!='r')&&(d!='l')) d='r';
    if (c==72) // Up.
    if ((d!='u')&&(d!='d')) d='u';
    if (c==80) // Down.
    if ((d!='u')&&(d!='d')) d='d';
    }
    }
    change_s();
    display_s();
    delay(500);
    }while (c!=27);
    getch();
    }
  • Did you compile Snake in Borland or MSVC++6.0?

    .:.:.:.:.:.:.:.

    [NS]Ein[CoFounder]

    Co-Founder of Nuclear Storm

    Go to http://nuclearstorm.cjb.net and visit us!

    .:.:.:.:.:.:.:.

  • : Did you compile Snake in Borland or MSVC++6.0?

    I think he did it in Borland; the graphics library he uses doesn't come with and doesn't work with MSVC++6.0 (at least not the version I have).
    :
    : .:.:.:.:.:.:.:.
    :
    : [NS]Ein[CoFounder]
    :
    : Co-Founder of Nuclear Storm
    :
    : Go to http://nuclearstorm.cjb.net and visit us!
    :
    : .:.:.:.:.:.:.:.
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories