?? snake.cpp
字號(hào):
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"conio.h"
#include"dos.h"
#include"bios.h"
#include"time.h"
#define WHITE 7
#define BLUE 1
#define BLACK 0
int toward=1;
int finish=1;
int back[2];
struct node
{
int mm[2];
node *pnext;
node *ppre;
};
node *head;
node *end;
void insert(int ne[2])
{
node *p=head;
node *pnew=new node;
pnew->mm[0]=ne[0];
pnew->mm[1]=ne[1];
node *puf;
pnew->ppre=0;
gotoxy(ne[0],ne[1]);
cprintf("%c",1);
if(p==0)
{
pnew->pnext=0;
end=pnew;
head=pnew;
return;
}
p->ppre=pnew;
pnew->pnext=p;
head=pnew;
}
void del()
{
node *p=end;
end=p->ppre;
gotoxy(p->mm[0],p->mm[1]);
cputs(" ");
delete p;
}
void go()
{
int m[2];
switch(toward)
{
case 1:
m[0]=head->mm[0]+1;
m[1]=head->mm[1];
break;
case 2:
m[0]=head->mm[0];
m[1]=head->mm[1]+1;
break;
case 3:
m[0]=head->mm[0]-1;
m[1]=head->mm[1];
break;
case 4:
m[0]=head->mm[0];
m[1]=head->mm[1]-1;
break;
}
insert(m);
if(back[0]==head->mm[0]&&head->mm[1]==back[1])
{
finish=1;
return;
}
del();
}
void gamg()
{
int x,y;
node *p;
while(1)
{
x=rand()%14+1;
y=rand()%14+1;
for(p=head;p!=0;p=p->pnext)
{
if(x==p->mm[0]&&y==p->mm[1])
break;
if(p->pnext==0)
{
back[0]=x;
back[1]=y;
gotoxy(x,y);
cprintf("%c",1);
return;
}
}
}
}
void main()
{
clock_t time2;
clock_t time1=0;
char key;
window(20,5,34,19);
_setcursortype(_NOCURSOR);
textcolor(BLACK);
textbackground(WHITE);
clrscr();
int a1[2]={3,3};
int a2[2]={4,3};
int a3[2]={5,3};
insert(a1);
insert(a2);
insert(a3);
while(1)
{
if(finish==1)
{
gamg();
finish=0;
}
time2=clock();
if(time2-time1>10)
{
time1=time2;
go();
}
if(bioskey(1)!=0)
{
char key1=getch();
switch(key1)
{
case 'a':
toward=3;
break;
case 'd':
toward=1;
break;
case 's':
toward=2;
break;
case '1':
return;
case 'w':
toward=4;
break;
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -