⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake.c

📁 linux下 贪食蛇代码的书写 利用到linux的图形库 时钟中断 等系统调用功能
💻 C
字号:

#include <stdio.h>
#include <stdlib.h>

#include <signal.h>
#include <time.h>
#include <sys/time.h>

#include <curses.h>

#include "link_snake.h"

#define  StartX		1
#define  StartY		1
#define  EndX		51
#define  EndY		21
#define  Score_X	64
#define  Score_Y	6

#define  MAXSEC		0
#define  MAXUSEC	700000

#define  UP			1
#define  LEFT		2
#define  DOWN		3
#define  RIGHT		4

#define	 FALSE		0
#define	 TRUE		1


char str1[30]       =   "###    SCORE    ###";
char str2[30]		=   "YOU LOST THE GAME  ";
char str3[30]		=   "  YOU		WIN     ";

char *number_0[5]	=   {"0000","0  0","0  0","0  0","0000"};
char *number_1[5]	=	{"   0","   0","   0","   0","   0"};
char *number_2[5]	=	{"0000","   0","0000","0   ","0000"};
char *number_3[5]	=	{"0000","   0","0000","   0","0000"};
char *number_4[5]	=	{"0  0","0  0","0000","   0","   0"};
char *number_5[5]	=	{"0000","0   ","0000","   0","0000"};
char *number_6[5]	=	{"0000","0   ","0000","0  0","0000"};
char *number_7[5]	=	{"0000","   0","   0","   0","   0"};
char *number_8[5]	=   {"0000","0  0","0000","0  0","0000"};
char *number_9[5]	=	{"0000","0  0","0000","   0","   0"};

struct  Snake_Head{
	int x;
	int y;
	int orient;
} head={12,12,UP};	//Truely is (12,11)  !!!!!

int  score		=	0;
int IF_OVERRIDE =   FALSE;	// To judge the head is touch the wall
int NOT_FIRST	=	FALSE;	// To judge whether the snake is first run
int FOOD_OVER	=	FALSE;	// To judge if the food is in snake body
int I_EAT		=	FALSE;	// if the head  eat the food 
int IS_DIE		=	FALSE;

int X_Seed		=	0;
int Y_Seed		=	0;


struct body *head_body ;  //head is different from head_body :)


void initial(void );
void Draw_Envir(void );
void Draw_Score(int );
char **switch_number(int );

void Draw_Food (void );
void Draw_SnakeHead(void );
void Draw_SnakeBody(); 

void store_snake(int ,int);
void Judge_Eat(int ,int );


void Die_Check(void );
//////////////////////////////////////////////////////////////////////////

void Snake_Handle()
{	
	Draw_Score(score);
	Draw_SnakeHead();
	Draw_SnakeBody();
	
	if(IS_DIE)
	{
//show die info
		mvprintw(18,58,"%s",str2);
		getch();
		
		endwin();
		exit(0);
	}
///	
	if(I_EAT)
		Draw_Food();  
	refresh();
}

int main()
{
	int key_word;

	struct itimerval time_value;
	time_value.it_interval.tv_sec	= MAXSEC;
	time_value.it_interval.tv_usec	= MAXUSEC;	//1sec = 1000000 usec
	time_value.it_value.tv_sec		= MAXSEC;
	time_value.it_value.tv_usec		= MAXUSEC;

//To Creat the snake
	head_body=creat();
	born_snake(head_body);


	initial();
	Draw_Envir();
	Draw_Score(score);
	Draw_SnakeHead( );
	Draw_SnakeBody();

	Draw_Food();
////		
	getch();
	NOT_FIRST=TRUE;
////	

	setitimer(ITIMER_REAL,&time_value,(struct itimerval*)0);
	signal(SIGALRM,Snake_Handle);

	while((key_word=getch())!='a')  
	{
	  switch(key_word) {
		  
	  case KEY_UP:
		  if(head.orient!=DOWN)
			  head.orient=UP;
			break;
	
	  case KEY_DOWN:
		  if(head.orient!=UP)
			  head.orient=DOWN;
			break;
	
	  case KEY_RIGHT:
		  if(head.orient!=LEFT)
			  head.orient=RIGHT;
			break;

	  case KEY_LEFT:
		  if(head.orient!=RIGHT)
			  head.orient=LEFT;
			break;
		
	  }
	}

	endwin();
	exit(0);
}



void initial()
{
	initscr();
	cbreak();
	nonl();
	noecho();
	curs_set(0);	//set _ unvisual
	intrflush(stdscr,FALSE);
	keypad(stdscr,TRUE);
	refresh();		
}


void Draw_Envir()
{
	int i=0;
///large line
	for(i=StartX;i<=EndX;i=i+2)
		 mvaddch(StartY,i,'#');

	for(i=StartY;i<=EndY;i++)
		 mvaddch(i,StartX,'#');

	for(i=StartX;i<=EndX;i=i+2)
		 mvaddch(EndY,i,'#');

	for(i=StartY;i<=EndY;i++)
	 	 mvaddch(i,EndX,'#');
///show score
	mvprintw(3,59,"%s",str1);
}


void Draw_Score(int number)
{
	if(number>=100)
	{
	mvprintw(18,58,"%s",str3);
	getch();

	endwin();
	exit(0);
	}

	char **p=NULL;

	int shi=number/10;
	int ge =number%10;

	int n=0;
	int y=Score_Y;
	int x=Score_X;

	p= switch_number(shi);

	while (n<=4) {
		mvprintw(y, x, "%s", *(p+n));
		n++;
		y++;
	}

	p= switch_number(ge);
	n=0;
	y=Score_Y ;
	x=Score_X+7;
	
	while (n<=4) {
		mvprintw(y, x, "%s", *(p+n));
		n++;
		y++;
	}
}

char **switch_number(int number)
{
	char **num_pt= NULL;
	
	switch(number) {
	case 0:
		num_pt=number_0;
		break;
	case 1:
		num_pt=number_1;
		break;
	case 2:
		num_pt=number_2;
		break;
	case 3:
		num_pt=number_3;
		break;
	case 4:
		num_pt=number_4;
		break;
	case 5:
		num_pt=number_5;
		break;
	case 6:
		num_pt=number_6;
		break;
	case 7:
		num_pt=number_7;
		break;
	case 8:
		num_pt=number_8;
		break;
	case 9:
		num_pt=number_9;
		break;
	default:
		endwin();
		printf("\nErroy\n");
		getchar();
		exit(0);
	}
	return num_pt;
}

void Draw_SnakeHead()
{	
	struct Snake_Head head_temp=head;
	struct body	 *last_temp=last_node(head_body);		//To save the last node
	

    mvaddch(head.y, head.x,' ');

	switch(head.orient) {
	case UP:
		head.y--;
		break;

	case LEFT:
		head.x--;
		head.x--;
		break;

	case DOWN:
		head.y++;
		break;

	case RIGHT:
		head.x++;
		head.x++;
		break;
	}

///judge

	if(head.x==0||head.x==52||head.y==1||head.y==21)
	
	{	IF_OVERRIDE=FALSE;
		IS_DIE	   =TRUE ;	}///Judge to die in "FIRST" WAY

	else
		IF_OVERRIDE=TRUE;

	if(IF_OVERRIDE&&NOT_FIRST)	
		store_snake(head_temp.x,head_temp.y);		//Update the link date

	Die_Check();			///Judge to die in "SECOND" WAY

/// judge if outside	
	if (head.x==0)
		head.x=2;
	if (head.x==52)
		head.x=50;
	if (head.y==1)
		head.y=2;
	if (head.y==21)
		head.y=20;
	
	Judge_Eat(last_temp->x,last_temp->y);

	mvaddch(head.y, head.x,'@');

}

void Draw_Food()
{
	
	srand((int)time(0));

do {
	
	X_Seed=((int)(25.0*rand()/(double)(RAND_MAX)))*2+2;
	Y_Seed=(int)(19.0*rand()/(double)(RAND_MAX))+2;

//judge not in the body
	
	if (X_Seed==head.x&&Y_Seed==head.y)
		FOOD_OVER=TRUE;
	else if(seek_snake(head_body,X_Seed,Y_Seed))	
		FOOD_OVER=TRUE;
	else
		FOOD_OVER=FALSE;
	
} while(FOOD_OVER>0);	

	mvaddch(Y_Seed,X_Seed,'#');	
	
}


void Draw_SnakeBody()
{
	struct body *p1;
	p1=head_body;

	while (p1->next!=NULL)
	{
		mvaddch(p1->y,p1->x,'@');	
		p1=p1->next;	   
	}
//Node 1 meet Node last
	if((head.x==p1->x)&&(head.y==p1->y))
		return;
	mvaddch(p1->y,p1->x,' ');
}


void store_snake(int x_head,int y_head)
{
	struct body *p1=head_body;
	
//To Del the Last Node
	while (p1->next->next!=NULL) {
		p1=p1->next;		
	}
	p1->next=NULL;
	
//To Add the first Node
	p1=(struct body *)malloc(LEN_STR);
	p1->x		= x_head;
	p1->y		= y_head;
	p1->next	= head_body;
	
	head_body=p1;
}


void Judge_Eat(int x_last,int y_last)
{
	if(X_Seed==head.x&&Y_Seed==head.y)
	{
		I_EAT=TRUE;
		score++;
		increase_snake(head_body,x_last,y_last);
	}
	else
		I_EAT=FALSE;
}


void Die_Check()
{
//// IF touch snake self

	if(seek_snake(head_body,head.x,head.y))
		IS_DIE	   =TRUE ;	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -