基于Ncurse图形库的C语言小游戏 。涉及到,C变量,流程控制,函数,指针,结构体等知识内容,动态链表的创建和插入, 以及释放。数组的遍历,
#include <stdlib.h>
#include <curses.h>
#define ROW 20
#define COL 20
#define ROW_Snake 2
#define COL_Snake 2
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
int key;
int dir;
int has_snake_node(int I,int J);
void initfood();
int if_snake_die();
struct Snake
{
int row_s;
int col_s;
struct Snake *next;
};
struct Snake *head = NULL;
struct Snake *tail = NULL;
struct Snake food;
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
void game_map(int row,int col)
{
int i;
int j;
move(0,0);
for(i=0;i<row;i++)
{
printw("--");
}
printw("\n");
for(i=1;i<row;i++)
{
int j = 0;
for(j=0;j<=col;j++)
{
if(j==0)
{
printw("|");
}
else if(j==col)
{
printw("|\n");
}
else if(has_snake_node(i,j))
{
printw("[]");
}
else if(has_food(i,j))
{
printw("##");
}
else
printw(" ");
}
}
for(i=0;i<row;i++)
{
printw("--");
}
printw("\n");
printw("BY LFF,food %d %d",food.row_s,food.col_s);
}
void add_node()
{
struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
new->next = NULL;
switch(dir)
{
case UP:
new->row_s = tail->row_s-1;
new->col_s = tail->col_s;
break;
case DOWN:
new->row_s = tail->row_s+1;
new->col_s = tail->col_s;
break;
case LEFT:
new->row_s = tail->row_s;
new->col_s = tail->col_s-1;
break;
case RIGHT:
new->row_s = tail->row_s;
new->col_s = tail->col_s+1;
break;
}
tail->next = new;
tail = new;
}
void delete_node()
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
void initsnake_body(int row_snake,int col_snake)
{
struct Snake *p;
dir = RIGHT;
while(head!=NULL)
{
p = head;
head = head->next;
free(p);
}
}
initfood();
{
head = (struct Snake *)malloc(sizeof(struct Snake));
head->row_s = row_snake;
head->col_s = col_snake;
head->next = NULL;
tail = head;
add_node();
add_node();
add_node();
}
int has_snake_node(int I,int J)
{
struct Snake *p;
p = head;
while(p != NULL)
{
if(p->row_s == I && p->col_s == J)
{
return 1;
}
p = p->next;
}
return 0;
}
void initfood()
{
int x = rand()%18+1;
int y = rand()%18+1;
food.row_s = x;
food.col_s = y;
}
int has_food(int I,int J)
{
if(food.row_s == I && food.col_s == J)
{
return 1;
}
return 0;
}
void move_snake()
{
add_node();
if(has_food(tail->row_s,tail->col_s))
{
initfood();
}
else
{
delete_node();
}
if(if_snake_die())
{
initsnake_body(ROW_Snake,COL_Snake);
}
}
void refresh_map()
{
while(1)
{
move_snake();
game_map(ROW,COL);
refresh();
usleep(100000);
}
}
void turn(int direction)
{
if(abs(dir) != abs(direction))
{
dir = direction;
}
}
void chang_dir()
{
while(1)
{
key = getch();
switch(key)
{
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int if_snake_die()
{
struct Snake *p;
p = head;
if(tail->row_s == 0 || tail->col_s == 0 || tail->row_s == 20 || tail->col_s == 20)
{
return 1;
}
while(p->next!=NULL)
{
if(p->row_s == tail->row_s && p->col_s == tail->col_s)
{
return 1;
}
p = p->next;
}
return 0;
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
// initscr();
initsnake_body(ROW_Snake,COL_Snake);
game_map(ROW,COL);
pthread_create(&t1,NULL,refresh_map,NULL);
pthread_create(&t2,NULL,chang_dir,NULL);
while(1);
getch();
endwin();
return 0;
}