C语言小游戏之贪吃蛇简易版

C语言小游戏之贪吃蛇简易版,第1张

C语言小游戏之贪吃蛇简易版
#include 
#include 
#include 
#include 
#include 

#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 *tail = NULL;

struct Snake food;

int key;//键盘读取的数据(上下左右的键值)
int dir;//自定义的方向(山下左右)

void initNcurses()
{
	initscr();//ncurses界面的初始化函数

	keypad(stdscr,1);//指定窗口 激活功能键(不激活功能键得不到我们想要的键值)

	noecho();
}

void AbsKey(int direction)//取绝对值的函数,解决小蛇前后、左右掉头的问题
{
	if(abs(dir) != abs(direction)){
		dir = direction;
	}
}

void AddNode()
{
	struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));

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

void DeleteNode()
{
	head = head->next;
}

void InitFood()
{
	int x = rand()%20;
	int y = rand()%20;

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

int IfHasFood(int hang,int lie)
{
	if(food.hang == hang && food.lie == lie){
		return 1;
	}
	return 0;
}

void initSnake()
{
	dir = RIGHT;

	InitFood();

	head = (struct Snake *)malloc(sizeof(struct Snake));

	head->hang = 1;
	head->lie = 2;
	head->next = NULL;

	tail = head;

	AddNode();
	AddNode();
}

int HasSnake(int hang,int lie)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == hang && p->lie == lie){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void MoveSnake()
{
	AddNode();

	if(tail->hang != food.hang || tail->lie != food.lie){
		DeleteNode();
	}else{
		InitFood();
	}
}

int IfSnakeDie()
{
	struct Snake *p = head;

	while(p->next != NULL){
		if(p->hang == tail->hang && p->lie == tail->lie){
			return 1;	
		}
		p = p->next;
	}
	
	if(tail->lie == 20 || tail->lie == 0 || tail->hang == 20 || tail->hang < 0){
		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 == 20){
			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(HasSnake(hang,lie) == 1){
					printw("[]");
				}else if(IfSnakeDie() == 1){
					initSnake();
				}else if(IfHasFood(hang,lie) == 1){
					printw("##");
				}else{
					printw("  ");
				}
			}
			printw("n");
		}
	}
	printw("made by hubei!");
}

void *RefreshGamePic()
{
	while(1){
		gamePic();
		MoveSnake();

		refresh();
		usleep(100000);
	}
}

void *ChangeDir()
{
	while(1){
		key = getch();

		switch(key){
			case KEY_UP:
				AbsKey(UP);
				break;
			case KEY_DOWN:
				AbsKey(DOWN);
				break;
			case KEY_LEFT:
                AbsKey(LEFT);
                break;
			case KEY_RIGHT:
               	AbsKey(RIGHT);
         		break;
			default:
				break;			
		}
	}
}

int main(int argc,char *argv[])
{
	pthread_t p1;
	pthread_t p2;

	initNcurses();

	initSnake();

	pthread_create(&p1,NULL,RefreshGamePic,NULL);

	pthread_create(&p2,NULL,ChangeDir,NULL);

	while(1);//防止程序退出

	endwin();//程序退出,调用函数来恢复shell终端的显示,如果没有这句话,shell终端字出现乱码,坏掉

	return 0;
}

学会C中的结构体、函数和指针再结合简单线程的使用即可做出。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3971655.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存