绘制蛇身的逻辑不难,存储上面使用结构体。
--
|
,分别在头尾处。[]
。#include"curses.h"
struct SnakeNode
{
int row;
int col;
struct SnakeNode* next;
};
struct SnakeNode node1 = {2,2,NULL};
struct SnakeNode node2 = {2,3,NULL};
struct SnakeNode node3 = {2,4,NULL};
void cursesinit()
{
initscr();
keypad(stdscr,1);
}
int hasSnake(int row,int col)
{
struct SnakeNode* p = &node1;
while(p!=NULL)
{
if(row == p->row && col == p->col)
return 1;
p = p->next;
}
return 0;
}
void mapinit()
{
int row;
int col;
for(row = 0;row < 20;row++)
{
// one
if(row == 0 || row == 19)
{
for(col = 0;col < 20;col++)
printw("----");
}
else if(hasSnake(row,col))
{
printw("[]");
}
// two
else
{
printw("|");
for(col = 0;col < 20-2;col++)
{
if(hasSnake(row,col))
{
printw("[]");
printw(" ");
}
else
{
printw(" "); // 4 spaces;
}
}
printw(" |"); // 4 + 3 len;
}
printw("\n");
}
getch();
}
int main()
{
node1.next = &node2;
node2.next = &node3;
cursesinit();
mapinit();
endwin();
return 0;
}