c语言实现小游戏--贪吃蛇

环境:ubuntu

有事联系qq邮箱:2643211783@qq.com;

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

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

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

int key;
int dir;
struct snake *head = NULL;
struct snake *tail = NULL;
struct snake food;

//随机生成食物
void initFood(){
	int x = (rand()%20+1) ;
	int y = (rand()%19+1) ;
 	
	food.hang = x;
	food.lie = y;
	
} 

//判断该坐标是否存在蛇身
int haveSnakeBody(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 getGrap(){
	
	int hang;
	int lie;

	move(0,0);

	for(hang=0;hang<22;hang++){
		if(hang == 0 || hang == 21){
			for(lie=0;lie<20;lie++){
				printw("--");
			}
			printw("\n");
		}
		if(hang>0 && hang<20){
			for(lie=0;lie<=20;lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(haveSnakeBody(hang,lie)){
					printw("[]");
				}else if (hasFood(hang,lie)){
					printw("##");
				}else{
					printw("  ");
				}	
			}
			printw("\n");
		}
	}
	printw("by chuan");
}

void addBody(){
	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;
}

//初始化蛇的身子
void initBody(){
	struct snake *p;
	dir = RIGHT; 
	
	if(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;

	 tail = head ;

	addBody();
	addBody();
}

//移动后删除尾巴,实现移动
void deleteBody(){
	struct snake *p;
	p = head;
	head = head->next;
	free(p);
}

//蛇死亡的条件
int ifSnakeDie(){
	struct snake *p;
	p = head;
	if(tail->hang == 0 || tail->hang == 21 || tail->lie ==  0 || tail->lie == 20){
		return 1;
	}
	while(p-next){
		if(p->hang == tail->hang && p->lie == tail->lie){
			return 1
		}
		p = p->next;
	}
	return 0;
}

//实现蛇移动的
void moveBody(){
	addBody();
	if(hasFood(tail->hang,tail->lie)){
		initFood();
	}else{
		deleteBody();
	}
	if(ifSnakeDie()){
		initBody();
	}
}

//每100000微秒刷新界面
void *refreshJieMian(){
	while(1){

			moveBody();
			getGrap();
			refresh();
			usleep(100000);
	}
}

//让蛇无法转180度
void turn(int dirction){
	if(abs(dir) != abs(dirction)){
		dir = dirction;
	}
}

//控制蛇转向
void *changeDir(){
	while(1){
		key = getch();
		switch(key){

			case KEY_UP:
				turn(UP);
				break;
			case KEY_DOWN:
				turn(DOWN);
				break;
			case KEY_LEFT:
				turn(LEFT);
				break;
			case KEY_RIGHT:
				turn(RIGHT);
				break;
		}


	}
}


int main ()
{
	pthread_t t1;
	pthread_t t2;
	
	
	initscr();
	keypad(stdscr,1);
	noecho();//curses运行是有可能会显示键盘输入的值
			//加noecho();可以解决
	
	initBody();

	getGrap();
	
    //创建两个线程
	pthread_create(&t1,NULL,refreshJieMian,NULL);
	pthread_create(&t2,NULL,changeDir,NULL);
	
	while(1);
	getch();
	endwin();
     return 0;
 }

文件名:snake.c

编译: gcc snake.c -lcurses -lpthread

运行:./a.out

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值