#include <head.h>
#define SQ 8000
unsigned char st=0,ed=9,sl=10,drt=B_RIGHT,over=0,bx,by;
void PutV(unsigned char pos,unsigned char x,unsigned char y)
{
P1=ARAM;
XBYTE[SQ+pos*2]=x;
XBYTE[SQ+pos*2+1]=y;
}
unsigned char GetX(unsigned char pos)
{
P1=ARAM;
return XBYTE[SQ+pos*2];
}
unsigned char GetY(unsigned char pos)
{
P1=ARAM;
return XBYTE[SQ+pos*2+1];
}
unsigned char Rnd(unsigned char max)
{
unsigned char i;
while(1)
{
i=(rand()>>8);
if(i<=max) return i;
}
}
void SnakeInit(void)
{
unsigned char i;
sl=10;st=0;ed=9;drt=B_RIGHT;autokey=1;
for(i=0;i<10;i++) PutV(i,i,0);
while(1)
{
bx=Rnd(31);by=Rnd(15);
if(by!=0) break;
}
Display(sl);
}
unsigned char Pos(unsigned int num)
{
if(ed>st) return st+num;
else return (((unsigned int)st)+num-255);
}
void Draw(void)
{
unsigned char i,x,y;
ClearLCMBuffer();
Rect(0,0,127,63,0,0);
for(i=0;i<sl;i++)
{
x=GetX(Pos(i));
y=GetY(Pos(i));
Rect(x*4,y*4,x*4+3,y*4+3,0,0);
}
Rect(bx*4,by*4,bx*4+3,by*4+3,0,1);
Refresh();
}
void Draw2(unsigned char pos,unsigned char mode)
{
unsigned char x,y;
x=GetX(pos);
y=GetY(pos);
Rect(x*4,y*4,x*4+3,y*4+3,mode,0);
}
void CheckOut(unsigned char x,unsigned char y)
{
unsigned char i;
if((x==0)&&(drt==B_LEFT)) goto OVER;
else if((x==31)&&(drt==B_RIGHT)) goto OVER;
else if((y==0)&&(drt==B_UP)) goto OVER;
else if((y==15)&&(drt==B_DOWN)) goto OVER;
if(drt==B_LEFT) x--;
else if(drt==B_RIGHT) x++;
else if(drt==B_UP) y--;
else if(drt==B_DOWN) y++;
for(i=0;i<sl;i++)
{
if((x==GetX(Pos(i)))&&(y==GetY(Pos(i)))) goto OVER;
}
return;
OVER:
over=1;
Print(3,1,"GAME OVER",0);
Print8(5,5,"Press A-key..",0);
autokey=0;
Refresh();
while(GetKey()!=B_A);
}
void Keys(void)
{
unsigned char k,x,y,i;
k=GetKey();
if(k!=0)
{
if((k==B_A)||(k==B_B))
{
Print(3,1,"P A U S E.",0);
Print8(2,4,"Any direction keys...",0);
Refresh();
autokey=0;
while(1)
{
k=GetKey();
if((k==B_B)||(k==B_A)||(k==0)) ;
else
{
autokey=1;
break;
}
}
Draw();
}
if((k!=B_UP)&&(k!=B_DOWN)&&(k!=B_LEFT)&&(k!=B_RIGHT)) ;
else if((drt==B_UP)&&(k!=B_DOWN)) drt=k;
else if((drt==B_DOWN)&&(k!=B_UP)) drt=k;
else if((drt==B_LEFT)&&(k!=B_RIGHT)) drt=k;
else if((drt==B_RIGHT)&&(k!=B_LEFT)) drt=k;
}
if(drt==B_RIGHT)
{
x=GetX(ed);
y=GetY(ed);
CheckOut(x,y);
Draw2(st,1);
if((x+1==bx)&&(y==by))
{
Rect(bx*4,by*4,bx*4+3,by*4+3,1,1);
while(1)
{
bx=Rnd(31);by=Rnd(15);
if((bx==x+1)&&(by==y)) continue;
i=0;
for(k=0;k<sl;k++)
{
if((bx==GetX(Pos(k)))&&(bx==GetX(Pos(k)))) i=1;
}
if(i==0) break;
}
Rect(bx*4,by*4,bx*4+3,by*4+3,0,1);
Draw2(st,0);
sl++;
Beep2();
Display(sl);
}
else
{
if(st==254) st=0;
else st++;
}
if(ed==254) ed=0;
else ed++;
PutV(ed,x+1,y);
Draw2(ed,0);
}
else if(drt==B_LEFT)
{
x=GetX(ed);
y=GetY(ed);
CheckOut(x,y);
Draw2(st,1);
if((x-1==bx)&&(y==by))
{
Rect(bx*4,by*4,bx*4+3,by*4+3,1,1);
while(1)
{
bx=Rnd(31);by=Rnd(15);
if((bx==x-1)&&(by==y)) continue;
i=0;
for(k=0;k<sl;k++)
{
if((bx==GetX(Pos(k)))&&(bx==GetX(Pos(k)))) i=1;
}
if(i==0) break;
}
Rect(bx*4,by*4,bx*4+3,by*4+3,0,1);
Draw2(st,0);sl++;Beep2();Display(sl);
}
else
{
if(st==254) st=0;
else st++;
}
if(ed==254) ed=0;
else ed++;
PutV(ed,x-1,y);
Draw2(ed,0);
}
else if(drt==B_UP)
{
x=GetX(ed);
y=GetY(ed);
CheckOut(x,y);
Draw2(st,1);
if((x==bx)&&(y-1==by))
{
Rect(bx*4,by*4,bx*4+3,by*4+3,1,1);
while(1)
{
bx=Rnd(31);by=Rnd(15);
if((bx==x)&&(by==y-1)) continue;
i=0;
for(k=0;k<sl;k++)
{
if((bx==GetX(Pos(k)))&&(bx==GetX(Pos(k)))) i=1;
}
if(i==0) break;
}
Rect(bx*4,by*4,bx*4+3,by*4+3,0,1);
Draw2(st,0);sl++;Beep2();Display(sl);
}
else
{
if(st==254) st=0;
else st++;
}
if(ed==254) ed=0;
else ed++;
PutV(ed,x,y-1);
Draw2(ed,0);
}
else if(drt==B_DOWN)
{
x=GetX(ed);
y=GetY(ed);
CheckOut(x,y);
Draw2(st,1);
if((x==bx)&&(y+1==by))
{
Rect(bx*4,by*4,bx*4+3,by*4+3,1,1);
while(1)
{
bx=Rnd(31);by=Rnd(15);
if((bx==x)&&(by==y+1)) continue;
i=0;
for(k=0;k<sl;k++)
{
if((bx==GetX(Pos(k)))&&(bx==GetX(Pos(k)))) i=1;
}
if(i==0) break;
}
Rect(bx*4,by*4,bx*4+3,by*4+3,0,1);
Draw2(st,0);sl++;Beep2();Display(sl);
}
else
{
if(st==254) st=0;
else st++;
}
if(ed==254) ed=0;
else ed++;
PutV(ed,x,y+1);
Draw2(ed,0);
}
if(sl>=250)
{
over=1;
Print(3,1,"WELL DONE!",0);
Print8(1,5,"You've got 240 points !",0);
Print8(5,6,"Press any key..",0);
autokey=0;
Refresh();
while(GetKey()==0);
}
if(over==0)
{
Rect(0,0,127,63,0,0);
Refresh();
}
}
void Game()
{
ClearLCM(0);
SnakeInit();
Draw();
while(GetKey()==0);
while(over==0) Keys();
}
void Return(void)
{
P1=0xaa;
while(1);
}
void main()
{
unsigned char i;
Beep();
Beep();
while(1)
{
over=0;
ClearLCM(0);
Print(0,1,"GLUTTONOUS SNAKE",0);
Print8(8,4,"By R.L.T.",0);
Print8(0,6," Press A-key to start, or",0);
Print8(0,7,"press B-key to exit. ",0);
Refresh();
while(1)
{
i=GetKey();
if(i==B_A) break;
else if(i==B_B) Return();
}
ClearLCM(0);
Game();
}
}