** 简单贪吃蛇实现(c语言Linux环境下):**
前言:为什么使用ncurse库,贪吃蛇跟着输入键移动,不需要用回车就可以移动。
主要思路是:
1.创建地图(CreateMap());
2.创建蛇形(initsnake(),hassnaknode());
3.让蛇随着键盘移动(getkeyword(),movesnake(),addnode(),deletenode());
4.不断地刷新页面,让接键盘输入移动和页面变化看起来一致(refreshscreen());
4.蛇碰到地图边缘或要到自身初始化(ifsnakedie());
5.食物随机(initfood(),hasfood());
include<curses.h>
int main()
{
initscr(); //nucrse界面初始化
printw("This is a curses window\n");
getch();
endwin();
return 0;
}
1.keypad函数获取键盘的功能键。
include<curses.h>
int main()
{
int key;
initscr(); //nucrse界面初始化
keypad(stdscr,1);
while(1){
key = getch();
switch(key){
case KEY_DOWN:
printw("DOWM\n");
break;
case KEY_UP:
printw("UP\n");
break;
case KEY_LEFT:
printw("LEFT\n");
break;
case KEY_RIGHT:
printw("RIGHT\n");
break;
}
}
endwin();
return 0;
}
2.地图的实现:
#include<curses.h>
void initNcurse()
{
initscr();
keypad(stdscr,1);
}
void CreatemMap()
{
move(0,0);//移动光标位置move(y,x)
int hang;
int lie;
for(hang=0;hang<22;hang++){
if(hang==0||hang==21){
for(lie=0;lie<20;lie++){
printw("--");
}
}
else if(hang>0&&hang<22){
for(lie=0;lie<=19;lie++){
if(lie==0){
printw("| ");
}else if(lie==19){
printw(" |");
}else{
printw(" ");
}
}
}
printw("\n");
}
}
int main()
{
initNcurse();
getmap();
getch();
endwin();
return 0;
}
2.蛇的形状
void CreateMap()
{
int hang;
int lie;
for(hang=0;hang<=21;hang++){
if(hang==0||hang==21){
for(lie=0;lie<=20;lie++){
printw("--");
}
}
else if(hang>0&&hang<21){
for(lie=0;lie<=21;lie++){
if(lie==0||lie==21){
printw("|");
}
else if(lie>0&&lie<21){
if(hassnakenode(hang,lie)){
printw("[]");
}
else if(hasfood(hang,lie)){
printw("##");
}
else{
printw(" ");
}
}
}
}
printw("\n");
}
}
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->hang = tail->hang;
new->lie = tail->lie+1;
tail->next = new;
tail = new;
}
void initsnake()
{
head = (struct snake*)malloc(sizeof(struct snake));
head->hang = 2;
head->lie = 2;
head->next = NULL;
tail = head;
addNode();
}
3获取键值完成蛇的移
void addNode()
{
struct snake *new = (struct snake*)malloc(sizeof(struct snake));
new->next = NULL;
switch(dir){
case RIGHT:
new->hang = tail->hang;
new->lie = tail->lie+1;
break;
case LEFT:
new->hang = tail->hang;
new->lie = tail->lie-1;
break;
case UP:
new->hang = tail->hang-1;
new->lie = tail->lie;
break;
case DOWN:
new->hang = tail->hang+1;
new->lie = tail->lie;
break;
tail->next = new;
tail = new;
}
}
void deleteNode()
{
struct snake *p;//p的创建为了删除前面那个节点,释放空间
p = head;
head = head->next;
free(p);
}
void* getkeyword()
{
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;
}
}
}
void turn(int direction)
{
if(dir==RIGHT && direction !=LEFT){
dir = direction;
}
if(dir==LEFT && direction !=RIGHT){
dir = direction;
}
if(dir==UP && direction !=DOWN){
dir = direction;
}
if(dir==DOWN && direction !=UP){
dir = direction;
}
}
void movesnake()
{
addNode();
if(hasfood(tail->hang,tail->lie)){
initfood();
}
else {
deleteNode();
}
if(ifsnakedie()){
initsnake();
}
}
int ifsnakedie()
{
struct snake *p;
p = head;
if(tail->hang==0||tail->hang==21||tail->lie==0||tail->lie==21)
{
return 1;
}
while(p->next!=NULL){
if(p->hang==tail->hang && p->lie==tail->lie){
return 1;
}
return 0;
}
4.刷新页面
void* refreshscreen()
{
while(1){
movesnake();
CreateMap();
refresh();
usleep(100000);//睡眠
}
}
5…食物
食物用##表示,在createMap中,输出
void initfood()
{
int x = rand()%20;
int y = rand()%20;
if(x==0){
x++;
}
if(y==0){
y++;
}
food.hang = x;
food.lie = y;
}
int hasfood(int i,int j)
{
if(food.hang==i&&food.lie==j){
return 1;
}
return 0;
}
主函数:
int main()
{//调用线程,让键盘输入蛇的移动,和刷新页面同时进行,让看起来蛇真的在跟着键盘输入移动。
pthread_t t1;
pthread_t t2;
initncurse();
initsnake();
CreateMap();
pthread_create(&t1,NULL,&refreshscreen,NULL);
pthread_create(&t2,NULL,&getkeyword,NULL);
while(1);
getch();
endwin();
return 0;
}