实现功能
- 蛇可以上下左右移动
- 蛇吃到食物,身体会变长
- 蛇撞到墙、咬到自己游戏结束
目的
为了熟悉Linux 环境下代码的开发,熟悉vim 界面编写代码,熟悉一些 vi 常用命令
项目中用到的API介绍
- 头文件 #include <curses.h>
- 编译需要:gcc 文件名 -lcurses
- 初始化ncurse界面 initscr();
- 输出 printw();
- 输入 getch();
- 程序退出 endwin();
- 接收键盘功能键 keypad();
- 改变光标位置 move(0,0);
- 刷新界面 refresh();
- 取绝对值 abs();
- 关闭回显模式,不让它显示乱七八糟的字符 noecho();
- 随机数 rand();
用到的知识点
指针、结构体、链表、curses、线程
代码拆解
main.c
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create(&t1, NULL, refreshJieMian, NULL);
pthread_create(&t2, NULL, changeDir, NULL);
while(1);
getch();
endwin();
return 0;
}
获取键盘输入实现上下左右移动
void *changeDir()
{
int key;
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;
}
}
}
贪吃蛇地图实现, 使用-- | 画出正方形格子方便后续蛇移动
如果用printf打印边界,后续很难实现蛇移动
void gamePic()
{
int hang;
int lie;
move(0,0);
for(hang=0;hang<20;hang++){
if(hang == 0){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
if(hang>=0 || hang<=18){
for(lie=0;lie<=20;lie++){
if(lie == 0 || lie==20){
printw("|");
}else if(hasSnakeNode(hang,lie)){
printw("[]");
}else if(hasFood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
printw("by dzz,key=%d\n",key);
}
}
}
制作蛇身
结构体(行列),链表改变添加长度(2个全局变量各指向蛇的头和尾),在刷新地图的时候把蛇添加进去(根据行和列坐标)
struct Snake
{
int hang;
int lie;
struct Snake *next;
};
struct Snake*head = NULL;
struct Snake*tail = NULL;
int hasSnakeNode(int i, int j)
{
struct Snake *p;
p = head;
while(p != NULL){
if(p->hang == i && p->lie == j){
return 1;
}
p = p->next;
}
return 0;
}
void addNode()
{
struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
new->next = NULL;
switch(dir){
case UP:
new->hang = tail->hang-1;
new->lie = tail->lie;
break;
case DOWN:
new->hang = tail->hang+1;
new->lie = tail->lie;
break;
case RIGHT:
new->hang = tail->hang;
new->lie = tail->lie+1;
break;
case LEFT:
new->hang = tail->hang;
new->lie = tail->lie-1;
break;
}
tail->next = new;
tail = new;
}
void initSnake()
{
struct Snake *p;
dir = RIGHT;
while(head!=NULL){
p = head;
head = head->next;
free(p);
}
initFood();
head = (struct Snake *)malloc(sizeof(struct Snake));
head->hang = 1;
head->lie = 1;
head->next = NULL;
tail = head;
addNode();
addNode();
addNode();
}
实现蛇自行向左移动
思路:链表的头删除一个,尾加一个结点,就实现了移动(尾在移动),同时主函数也要刷新界面
void moveSnake()
{
addNode();
if(hasFood(tail->hang,tail->lie)){
initFood();
}else{
deleNode();
}
if(tail->hang==0 || tail->lie==0 ||tail->hang==20 || tail->lie==20){
initSnake();
}
}
void *refreshJieMian ()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(100000);
}
}
蛇撞墙,重新开始
思路:尾节点和墙坐标重合,就重新初始化蛇,要free释放内存空间
if(tail->hang==0 || tail->lie==0 ||tail->hang==20 || tail->lie==20){
initSnake();
}
struct Snake *p;
while(head!=NULL){
p = head;
head = head->next;
free(p);
}
实现蛇的上下左右移动
思路:按下键盘蛇移动,刷新界面;此时需要用到两个线程来处理。
键盘输入判断后改变dir,进而改变方向(尾部加节点的时候判断往什么地方加)
加节点的时候,根据dir,判断是往上下左右哪个地方加
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
int key;
int dir;
void addNode()
{
struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
new->next = NULL;
switch(dir){
case UP:
new->hang = tail->hang-1;
new->lie = tail->lie;
break;
case DOWN:
new->hang = tail->hang+1;
new->lie = tail->lie;
break;
case RIGHT:
new->hang = tail->hang;
new->lie = tail->lie+1;
break;
case LEFT:
new->hang = tail->hang;
new->lie = tail->lie-1;
break;
}
tail->next = new;
tail = new;
}
void *refreshJieMian ()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(100000);
}
}
void *changeDir()
{
int key;
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;
}
}
}
实现食物的诞生和蛇吃食物
食物,使用一个结构体,行列坐标用rand随机产生
在moveSnake函数中判断是否,蛇的尾部碰到食物,碰到刷新食物,不删除头,就实现了增加蛇的长度。
void initFood()
{
static int x = 4;
static int y = 5;
food.hang = x;
food.lie = y;
x+=2;
y+=2;
}
void hasFood(int i, int j)
{
if(food.hang == i && food.lie == j){
return 1;
}
return 0;
}
void moveSnake()
{
addNode();
if(hasFood(tail->hang,tail->lie)){
initFood();
}else{
deleNode();
}
if(tail->hang==0 || tail->lie==0 ||tail->hang==20 || tail->lie==20){
initSnake();
}
}
完整代码如下
#include<curses.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
{
int hang;
int lie;
struct Snake *next;
};
struct Snake*head = NULL;
struct Snake*tail = NULL;
int key;
int dir;
struct Snake food;
void initFood()
{
static int x = 4;
static int y = 5;
food.hang = x;
food.lie = y;
x+=2;
y+=2;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
int hasSnakeNode(int i, int j)
{
struct Snake *p;
p = head;
while(p != NULL){
if(p->hang == i && p->lie == j){
return 1;
}
p = p->next;
}
return 0;
}
void hasFood(int i, int j)
{
if(food.hang == i && food.lie == j){
return 1;
}
return 0;
}
void gamePic()
{
int hang;
int lie;
move(0,0);
for(hang=0;hang<20;hang++){
if(hang == 0){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
if(hang>=0 || hang<=18){
for(lie=0;lie<=20;lie++){
if(lie == 0 || lie==20){
printw("|");
}else if(hasSnakeNode(hang,lie)){
printw("[]");
}else if(hasFood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
printw("by xutanghao,key=%d\n",key);
}
}
}
void addNode()
{
struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
new->next = NULL;
switch(dir){
case UP:
new->hang = tail->hang-1;
new->lie = tail->lie;
break;
case DOWN:
new->hang = tail->hang+1;
new->lie = tail->lie;
break;
case RIGHT:
new->hang = tail->hang;
new->lie = tail->lie+1;
break;
case LEFT:
new->hang = tail->hang;
new->lie = tail->lie-1;
break;
}
tail->next = new;
tail = new;
}
void initSnake()
{
struct Snake *p;
dir = RIGHT;
while(head!=NULL){
p = head;
head = head->next;
free(p);
}
initFood();
head = (struct Snake *)malloc(sizeof(struct Snake));
head->hang = 1;
head->lie = 1;
head->next = NULL;
tail = head;
addNode();
addNode();
addNode();
}
void deleNode()
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
void moveSnake()
{
addNode();
if(hasFood(tail->hang,tail->lie)){
initFood();
}else{
deleNode();
}
if(tail->hang==0 || tail->lie==0 ||tail->hang==20 || tail->lie==20){
initSnake();
}
}
void *refreshJieMian ()
{
while(1){
moveSnake();
gamePic();
refresh();
usleep(100000);
}
}
void turn(int direction)
{
if(abs(dir) != abs(direction)){
dir = direction;
}
}
void *changeDir()
{
int key;
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 main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gamePic();
pthread_create(&t1, NULL, refreshJieMian, NULL);
pthread_create(&t2, NULL, changeDir, NULL);
while(1);
getch();
endwin();
return 0;
}
运行效果
贪吃蛇项目