#
include
<stdio.h>
#
include
<conio.h>
#
include
<windows.h>
#define BEG_X 2
#define BEG_Y 1
#define WID 20
#define HEI 20
HANDLE hout;
typedef enum {UP, DOWN, LEFT, RIGHT} DIR;
typedef struct Snake_body
{
COORD pos;
struct Snake_body *next;
struct Snake_body *prev;
}SNAKE, *PSNAKE;
PSNAKE head = NULL;
PSNAKE tail = NULL;
void DrawBorder()
{
int i, j;
COORD pos = {BEG_X, BEG_Y};
for
(i = 0; i < HEI; ++i)
{
SetConsoleCursorPosition(hout, pos);
for
(j = 0; j < WID; ++j)
{
if
(i == 0)
{
if
(j == 0)
printf(
"┏"
);
else
if
(j == WID - 1)
printf(
"┓"
);
else
printf(
"━"
);
}
else
if
(i == HEI - 1)
{
if
(j == 0)
printf(
"┗"
);
else
if
(j == WID - 1)
printf(
"┛"
);
else
printf(
"━"
);
}
else
if
(j == 0 || j == WID - 1)
printf(
"┃"
);
else
printf(
" "
);
}
++pos.Y;
}
}
void AddBody(COORD pos)
{
PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));
pnew->pos = pos;
if
(!head)
{
head = tail = pnew;
}
else
{
pnew->next = head;
head->prev = pnew;
head = pnew;
}
SetConsoleCursorPosition(hout, head->pos);
printf(
"◎"
);
}
void MoveBody(DIR dir)
{
PSNAKE ptmp;
COORD pos = head->pos;
switch
(dir)
{
case
UP:
if
(head->pos.Y > BEG_Y + 1)
--pos.Y;
else
return
;
break
;
case
DOWN:
if
(head->pos.Y < BEG_Y + HEI - 2)
++pos.Y;
else
return
;
break
;
case
LEFT:
if
(head->pos.X > BEG_X + 2)
pos.X -= 2;
else
return
;
break
;
case
RIGHT:
if
(head->pos.X < BEG_X + (WID - 2) * 2)
pos.X += 2;
else
return
;
break
;
}
AddBody(pos);
ptmp = tail;
tail = tail->prev;
if
(tail)
tail->next = NULL;
SetConsoleCursorPosition(hout, ptmp->pos);
printf(
" "
);
free(ptmp);
}
int main()
{
int ctrl;
DIR dir = RIGHT;
COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};
system(
"color 0E"
);
system(
"mode con cols=90 lines=30"
);
hout = GetStdHandle(STD_OUTPUT_HANDLE);
printf(
" ------------贪吃蛇的移动------------"
);
DrawBorder();
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
pos.X += 2;
AddBody(pos);
while
(ctrl = getch())
{
switch
(ctrl)
{
case
'w'
:
if
(dir == DOWN)
continue
;
dir = UP;
break
;
case
's'
:
if
(dir == UP)
continue
;
dir = DOWN;
break
;
case
'a'
:
if
(dir == RIGHT)
continue
;
dir = LEFT;
break
;
case
'd'
:
if
(dir == LEFT)
continue
;
dir = RIGHT;
break
;
case
'q'
:
return
0;
}
MoveBody(dir);
}
return
0;
}