一、编程思路
大概思路:
1、打印出界面(40*40)
initscr();//ncurses界面的初始化函数
GraphInterface();//打印界面
getch();//当用户按下某个字符时,函数自动读取,无需按回车
endwin();//恢复终端机原来的状态
2、初始化蛇身
2.1 确定蛇身的初始位置(一个节点)
2.2 添加蛇神的节点(总共3-4个)
3、识别键盘,开始对进行蛇进行移动
3.1移动蛇身
3.1.1 如向右移动,向右添加一个节点,左边删除一个节点
3.1.2 如向左移动,向左添加一个节点,右边删除一个节点
3.1.3 如向上移动,向上添加一个节点,下边删除一个节点
3.1.4 如向下移动,向下添加一个节点,上边删除一个节点
3.2 移动完蛇身,要进行界面的刷新
(1)再打印一次页面
(2)move(0,0);加载到打印函数里面
4、创建线程th1,让蛇身初始移动方向向右(每半秒刷一次)
5、创建线程th2,不断检测按键来改变蛇的移动方向
6、主函数要在程序结束之前有个while(1);防止程序退出;
7、初始化食物位置,random()随机
8、界面初始化中,找到食物的位置,打印食物
9、判断食物是否被蛇碰到,碰到的话添加一个节点
10、判断食物死亡的情况,两种:边界和碰到自己蛇身
二、代码
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>
#include <pthread.h>
#define UP 1
#define DOWN -1
#define LIGHT 2
#define RIGHT -2
int graphLength = 0;
int graphWide = 0;
int keyValue;
int dir = 0;
struct Snake
{
int hang;
int lie;
struct Snake *next;
};
struct Snake food;
struct Snake *head = NULL;
struct Snake *tail = NULL;
void initFood()
{
int x=rand()%20;
int y=rand()%20;
food.hang = x;
food.lie = y;
}
int judgeFoodPosition(int hang,int lie)
{
if(food.hang == hang && food.lie == lie){
return 1;
}
return 0;
}
int judgeSnakePositon(int j,int i)
{
struct Snake *p;
p = head;
while(p != NULL){
if(p->hang == j && p->lie == i){
return 1;
}
p = p->next;
}
return 0;
}
void printInterPointrt(int hang)
{
for (graphWide = 0; graphWide < 40;graphWide++){
if (graphWide == 0 || graphWide == 39){
printw("|");
}
else if(judgeSnakePositon(hang,graphWide)){
printw("O");
}
else if( judgeFoodPosition(hang,graphWide)){
printw("*");
}
else
printw(" ");
}
printw("\n");
}
void GraphInterface()
{
move(0,0);
for (graphLength = 0; graphLength < 20; graphLength++){
if (graphLength == 0){
for (graphWide = 0; graphWide < 40;graphWide++){
printw("~");
}
printw("\n");
printInterPointrt(graphLength);
}
else if (graphLength == 19){
printInterPointrt(graphLength);
for (graphWide = 0; graphWide < 39;graphWide++){
printw("~");
}
}
else {
printInterPointrt(graphLength);
}
}
printw("\n");
printw("KEY_VALUE:%d\n",keyValue);
printw("Food Position:%d %d\n",food.hang,food.lie);
}
void deleteSnakeNode()
{
struct Snake *p = head;
head = head->next;
free(p);
}
void addSnakeBody()
{
struct Snake *new = (struct Snake*)malloc(sizeof(struct Snake));
switch(dir){
case DOWN:
new->hang = tail->hang + 1;
new->lie = tail->lie;
break;
case UP:
new->hang = tail->hang - 1;
new->lie = tail->lie ;
break;
case LIGHT:
new->hang = tail->hang;
new->lie = tail->lie - 1;
break;
case RIGHT:
new->hang = tail->hang;
new->lie = tail->lie + 1;
break;
}
new->next = NULL;
tail->next = new;
tail = new;
}
void SnakeInitPosition()
{
dir = RIGHT;
head = (struct Snake *)malloc(sizeof(struct Snake));
head->hang = 0;
head->lie = 1;
head->next = NULL;
tail = head;
addSnakeBody();
addSnakeBody();
}
int judgeSnakedie()
{
struct Snake *p;
p = head;
if(tail->lie == 39 || tail->lie == 0 || tail->hang < 0 || tail->hang == 20){
return 1;
}
while(p->next != NULL){
if(p->hang == tail->hang && p->lie == tail->lie){
return 1;
}
p=p->next;
}
return 0;
}
void moveSnakeBody()
{
addSnakeBody();
deleteSnakeNode();
if( judgeFoodPosition(tail->hang,tail->lie) ){
addSnakeBody();
initFood();
}
if(judgeSnakedie()){
SnakeInitPosition();
}
}
void* changeSnakePosition()
{
while(1){
moveSnakeBody();
GraphInterface();
usleep(300000);
refresh();
}
}
void conDirtion(int dir1)
{
if (abs(dir) != abs(dir1)){
dir = dir1;
}
}
void* testButtonValue()
{
while(1){
keyValue = getch();
switch(keyValue){
case KEY_DOWN:
conDirtion(DOWN);
break;
case KEY_UP:
conDirtion(UP);
break;
case KEY_LEFT:
conDirtion(LIGHT);
break;
case KEY_RIGHT:
conDirtion(RIGHT);
break;
}
}
}
void NcurseGraphInit()
{
initscr();
keypad(stdscr,1);
noecho();
}
int main(void)
{
pthread_t th1;
pthread_t th2;
NcurseGraphInit();
SnakeInitPosition();
initFood();
GraphInterface();
pthread_create(&th1,NULL,changeSnakePosition,NULL);
pthread_create(&th2,NULL,testButtonValue,NULL);
while(1);
getch();
endwin();
return 0;
}
三、运行效果
|