am incercat eu mai demult sa fac snake in C, uite ce mi-a iesit:
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
char *a[23];
int s[2][200],head,tail;
int i,j,direction=2,score=0,diff;
void print()
{
for(i=0;i<22;i++)
printf("%s\n",a[i]);
printf("SCORE: %i",score);
}
void init_edges()
{
for(i=0;i<22;i++)
a[i][0]=a[i][78]='*';
for(j=0;j<78;j++)
a[0][j]=a[21][j]='*';
}
void init()
{
for(i=0;i<22;i++){
for(j=0;j<78;j++)
a[i][j]=' ';
a[i][79]='\0';
}
}
int main(){
int ch1, ch2, t, l=10,k=10,ok=1,tl=10,tk=5,fx,fy,fok=1;
for(i=0;i<22;i++)
a[i]=(char *)malloc(79*sizeof(char));
init();
init_edges();
head=0;
for(i=tk+1;i<=k;i++){
a[l][i]='o';
s[0][head]=l;
s[1][head]=i;
head++;
}
tail=0;
srand(time(NULL));
fx = rand()%21;
fy = rand()%77;
ok=1;
while(fx==0||fy==0||a[fx][fy]!=' '){
fx = rand()%21;
fy = rand()%77;
}
a[fx][fy]='@';
printf("Select difficulty:\n1-EASY\n2-MEDIUM\n3-HARD\n4-VERRY HARD\n5-EXTREME\n");
scanf("%i",&diff);
diff=200/(diff*diff);
while(ok){
while (!kbhit()){
Sleep(diff);
system("cls");
print();
a[l][k]='o';
if(direction==1){
l--;
if(l==0)
ok=0;
}
else if(direction==2){
k++;
if(k==78)
ok=0;
}
else if(direction==3){
l++;
if(l==21)
ok=0;
}
else if(direction==4){
k--;
if(k==0)
ok=0;
}
if(a[l][k]=='o')
ok=0;
else if(a[l][k]=='@'){
fx = rand()%21;
fy = rand()%77;
ok=1;
while(fx<2||fy<2||a[fx][fy]!=' '){
fx = rand()%20;
fy = rand()%76;
}
a[fx][fy]='@';
score++;
}
else{
a[s[0][tail]][s[1][tail]]=' ';
tail++;
if(tail==199)
tail=0;
}
a[l][k]='x';
s[0][head]=l;
s[1][head]=k;
head++;
if(head==199)
head=0;
if(ok==0)
break;
}
if(ok){
ch1=getch();
ch2 = 0;
if (ch1 == 0xE0&&ok) {
ch2 = getch();
switch(ch2){
case 72: {if(direction!=3)direction=1;//printf("UP\n");
break;}
case 80: {if(direction!=1)direction=3;//printf("DOWN\n");
break;}
case 75: {if(direction!=2)direction=4;//printf("LEFT\n");
break;}
case 77: {if(direction!=4)direction=2;//printf("RIGHT\n");
break;}
default:break;
}
}
}
}
printf("\nGAME OVER!");
getchar();
getchar();
return 0;
}