Linux下基于Ncurse图形库,运用Linux线程的贪吃蛇小游戏

**基于Linux系统下Ncurse图形库,运用Linux系统下多线程的贪吃蛇小游戏**

#include<curses.h>
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct snake{
	int hang;
	int lie;
	struct snake* next;
};
struct snake food;
struct snake* head = NULL;
struct snake* tail = NULL;
int key;
int dir;
int mark = 0;
int chushiHead(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;


}
//chu shi hua cheng ji
void initmark(){
	mark = 0;

}
//chu shi hua shi wu
void initfood()
{
	int x;
	int y;
	x = rand()%20;
	y = rand()%20;
	food.hang = x;
	food.lie = y;




}
//zai tu shang chu xian shi wu
int chuxianshiwu(int i,int j)
{
	if(food.hang==i&&food.lie==j){
		return 1;
	}
	return 0;
}
//Ncurses chu shi hua
void initNcurse(){

	initscr();
	keypad(stdscr,1);
	noecho();
}
//hua tu
void draw(){


	int hang;
	int lie;
	move(0,0);
	for(hang = 0;hang<=19;hang++){
		if(hang == 0){
			for(lie = 0;lie<=19;lie++)
			{printw("--");
			}
			printw("\n");
		}
		if(hang>=0&&hang<=19){
			for(lie = 0;lie<=20;lie++){
				if(lie == 0||lie == 20)
				{printw("|");}
				else if(chushiHead(hang,lie)){
					printw("[]");}
				else if(chuxianshiwu(hang,lie)){

					printw("##");
				}
				else{printw("  ");}
			} 

			printw("\n");
			if(hang == 19){
				for(lie = 0;lie<=19;lie++)
				{printw("--");
				}
				printw("\n");
				printw("This greedy snake was completed by JZT|2020.10.14\n");
				printw("Your mark is |[%d]|\n",mark);
			}




		}
	}
}
// tian jia jie dian
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;

}
//chu shi hua snake
void initsnake(){
	struct snake *p;//qing chu yi qian de neicun

	dir = RIGHT;
	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();
	addnode();
}
// shan chu jie dian
void deletenode(){


	head = head->next;

}
//yi dong snake
void movesnake(){

	addnode();
	if(food.hang==tail->hang&&food.lie==tail->lie){
		initfood();
		mark+=10;
		return ;}
	else{
		deletenode();
	}
	//zhuang qiang si wang
	if(tail->hang<0||tail->hang==20||tail->lie==0||tail->lie==20){
		initsnake();
		initmark();

	}
	struct snake* p;
	p=head;
	while(p->next!=NULL){
		if(p->hang==tail->hang&&p->lie==tail->lie){
			initsnake();
			initmark();
			initfood();
		}
		p=p->next;
	}

}
//shua xin ye mian(pthread)
void* shuaxinJIEMIAN(){
	while(1){
		movesnake();
		draw();
		refresh();
		usleep(90000);

	}
}
//gai bian fang xiang
void turndirection(int direction){
	if(abs(dir)!=abs(direction)){
		dir=direction;
	}

}
//gai bian fang xiang
void* changedirection(){
	while(1){
		key = getch();
		switch(key){
			case KEY_UP:
				turndirection(UP);
				break;
			case KEY_DOWN:
				turndirection(DOWN);
				break;
			case KEY_LEFT:
				turndirection(LEFT);
				break;
			case KEY_RIGHT:
				turndirection(RIGHT);
				break;

		}
	}
}
//(main) fang fa
int main(){
	//ding  yi pthread
	pthread_t t1;
	pthread_t t2;
	initNcurse();
	initsnake();
	initfood();
	draw();
	pthread_create(&t1,NULL,shuaxinJIEMIAN,NULL);
	pthread_create(&t2,NULL,changedirection,NULL);
	while(1);
	getch();
	endwin();
	return 0;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值