贪吃蛇小游戏
实现步骤
1、使用ncurses图形库的基础框架、测试键盘按键响应
2、地图的规划,主要是确定链表的活动范围
3、蛇身的初始化,确定蛇的开始位置
4、蛇身的移动及地图刷新,链表的插入新结点、删除尾结点
5、键盘控制蛇的走向,线程的使用
6、游戏的规则制定,例如撞墙、咬到自己复活、不可以后退等
7、食物的设置
8、计时、得分等
ncurses图形库下载
在linux环境下安装:sudo apt-get install libncurses5-dev
示例代码
#include <curses.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#define DOWN -3
#define UP 3
#define LEFT -2
#define RIGHT 2
#define TIME time(NULL)
struct Snake
{
int hang;
int lie;
struct Snake *next;
};
struct Snake *head = NULL;
struct Snake *tail = NULL;
int direction;
struct Snake food;
int scores = 0;
time_t time1;
void initNcurses()
{
initscr();
keypad(stdscr, 1);
noecho();
}
void create_snake_food()
{
int x;
int y;
x = rand() % 20;
y = rand() % 20;
food.hang = x;
food.lie = y;
}
void add_node()
{
struct Snake *new = malloc(sizeof(struct Snake));
if(new == NULL)