Linux下C语言编写贪吃蛇小游戏源码

#include<curses.h>
#include<stdlib.h>


#define UP     1
#define DOWN  -1
#define LEFT   2
#define RIGHT -2

struct snake
{
	int hang;
	int lie;
	struct snake *next;
};

struct snake *head= NULL;
struct snake *tall= NULL;

int key;
int dir;


struct snake food;

void initfood()
{
	int x = rand()%19;
	int y = rand()%19;

	food.hang = x;
	food.lie = y;
}


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;
}

int hasfood(int i,int j)
{
	if(food.hang == i && food.lie ==j){
		return 1;
	}
	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 if(hasfood(hang,lie)){
					printw("##");
				}
				else{
					printw("  ");
				}
			}
			printw("\n");
		}
			

			if(hang==19){
				for(lie=0;lie<20;lie++){
					printw("--");
			        }
			
				printw("\n");
				printw("By liuzhihao,food.hang=%d,food.lie=%d\n",food.hang,food.lie);
			}

        	}
}


void addnode()
{
	struct snake *new =(struct snake *)malloc(sizeof(struct snake));
	new->next = NULL;

	switch(dir){
	
		case UP:
			new->hang = tall->hang-1;
			new->lie =tall->lie;
			break;
	        case DOWN:
                        new->hang = tall->hang+1;
                        new->lie =tall->lie;
                        break;
                case LEFT:
                        new->hang =tall->hang;
			new->lie = tall->lie-1;
			break;
                case RIGHT:
                        new->hang = tall->hang;
                        new->lie =tall->lie+1;
                        break;
	}
	tall->next = new;
	tall = new;
}

void initsnake()
{	
	struct snake *p;
	dir = RIGHT;
	while(head!=NULL){
		p=head;
		head = head -> next;
		free(p);
	}
	initfood();

	head = (struct snake *)malloc(sizeof(struct snake));
	head->hang = 1;
	head -> lie = 1;
	head -> next = NULL;
	
	tall = head;
	

	addnode();
	addnode();
 	addnode();
	addnode();

}

void delenode()
{
	struct snake *p;
	p = head;
	head = head -> next;

	free(p);
}

int ifsnakedie()
{
	struct snake *p;
	p=head;
	if (tall->hang< 0 || tall->lie == 0 || tall -> hang == 20 || tall -> lie == 20 ){
		return 1;
	}
	
	while(p->next!=NULL){
		if(p->hang == tall -> hang && p -> lie == tall -> lie){
			return 1;
		}
		p = p->next;
	}
	return 0;

}


void movesnake()
{
	addnode();
	if(hasfood(tall -> hang, tall-> lie)){
		initfood();
	}
	else {
		delenode();
	}
	if(ifsnakedie()){
		initsnake();
	}


}

void refreshjiemian()
{
	while(1){
		
		movesnake();
		gamepic();
		refresh();
		usleep(100000);

	}
}

void turn(int direction)
{
	if(abs(dir)!=abs(direction)){
		dir = direction;
	}
}
void changedir()
{
	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;

		}
	}

}


int main()
{
	pthread_t t1;
	pthread_t t2;

	initncurse();
	initsnake();	
	gamepic();

	pthread_create(&t1,NULL,refreshjiemian,NULL);
	pthread_create(&t2,NULL,changedir,NULL);

	while(1);
	getch();
	endwin();
	return 0;
}

编译时使用gcc 文件名 -lcurses

食物随机显示有不足之处,还有改进之处,不过上述代码基本运行没有问题

Linux下C语言贪吃蛇小游戏.mp4

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值