Linux环境下,基于Ncurse图形库贪吃蛇小游戏(下)
Posted 橙子果果
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux环境下,基于Ncurse图形库贪吃蛇小游戏(下)相关的知识,希望对你有一定的参考价值。
Linux环境下,基于Ncurse图形库贪吃蛇小游戏(下)
蛇移动的不合理(用绝对值来解决)
蛇吃食物(链表的添加和食物的随机出来)
蛇自己咬死自己(尾节点与其余节点重合初始化蛇)
最后完全代码:
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake
int hang;
int lie;
struct Snake *next;
;
int key;
int dir;
struct Snake *head = NULL;
struct Snake *tail = NULL;
struct Snake food;
void initFood()
int x = rand()%20;
int y = rand()%20;
food.hang = x;
food.lie = y;
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;
int 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<=19)
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 Chengzi!");
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 LEFT:
new->hang = tail->hang;
new->lie = tail->lie-1;
break;
case RIGHT:
new->hang = tail->hang;
new->lie = tail->lie+1;
break;
tail->next = new;
tail=new;
void initSnake()
struct Snake *p;
dir = RIGHT;
initFood();
while(head!=NULL)
p = head;
head = head->next;
free(p);
head = (struct Snake *)malloc(sizeof(struct Snake));
head->hang = 2;
head->lie = 2;
head->next = NULL;
tail = head;
addNode();
addNode();
void deleNode()
struct Snake *p;
p = head;
head = head->next;
free(p);
int ifSnakeDle()
struct Snake *p;
p = head;
if (tail->hang<0 || tail->hang == 20 || tail->lie == 0 || tail->lie == 20)
return 1;
while(p->next != NULL)
if(p->hang == tail->hang && p->lie == tail->lie)
return 1;
p = p->next;
return 0;
void moveSnake()
addNode();
if(hasFood(tail->hang,tail->lie))
initFood();
else
deleNode();
if (ifSnakeDle())
initSnake();
void* refreshJM()
while(1)
moveSnake();
gamePic();
refresh();
usleep(100000);
void turn(int direction)
if(abs(dir) != abs(direction))
dir = direction;
void* changeDir()
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 th1;
pthread_t th2;
initNcurse();
initSnake();
gamePic();
pthread_create(&th1,NULL,refreshJM,NULL);
pthread_create(&th2,NULL,changeDir,NULL);
while(1);
getch();
endwin();
return 0;
代码运行要加上 -lcurses -lpthread
最后是游戏实现的界面
以上是关于Linux环境下,基于Ncurse图形库贪吃蛇小游戏(下)的主要内容,如果未能解决你的问题,请参考以下文章