实现贪吃蛇四方向的风骚走位
实现代码
#include <curses.h>
#include <stdlib.h>
struct snake{
int hang;
int lie;
struct snake *next;
};
struct snake *head;
struct snake *tail;
int key;
int dir; //全局变量
#define UP 1 //这个是宏定义,主要就是让人更好理解 ,也为了后期的方向做了铺垫
#define DOWN -1
#define LEFT 2
#define RIGHT -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 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{
printw(" ");
}
}
printw("\n");
}
if(hang == 19){
for(lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
}
printw("by shijintao\n");
printw("key =%d\n",key);
}
void addNode()
{
struct snake *new;
new =(struct snake *)malloc(sizeof(struct snake));
new->next=NULL;
switch(dir){
case UP:


最低0.47元/天 解锁文章
5573





