本文主要是介绍贪吃蛇身子改进加贪吃蛇向右移动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 蛇移动的思想:
其实就是删除头节点 ,增加尾节点;一句代码搞定
struct Snake *p; p =head; head = head -> next; free(p) 防止造成多的空间节点
2.增加尾节点代码思想:
2.1 .开辟new 节点的空间 struct Snake *new = (struct Snake *)malloc (sizeof(struct Snake));
2.2 . 新节点的行 new ->hang = tail -> hang;
新节点的列 new ->lie =tail -> lie+1;
增加new节点 :tail -> next = new;
新的尾巴 : tail = new ;
void addNode()
{struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));new -> hang = tail -> hang;new -> lie = tail -> lie+1;tail -> next = new;tail = new;
}void deleteNode()
{struct Snake *p;p = head;head = head ->next;free(p); // fang zhi zao cheng hen duo kong yu de jie dian
}void moveSnake()
{struct Snake *p;struct Snake *new;addNode();deleteNode();
}
int main()
{int ctr;initNcurses();initSnakebody();gamePic();while(1){ctr = getch();if(ctr == KEY_RIGHT){moveSnake();gamePic();}}getch();endwin();return 0;
}
这篇关于贪吃蛇身子改进加贪吃蛇向右移动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!