snake_c

如何开始界面,点击后进入游戏界面?

void Welcome(){
	printf("\n\n\t欢迎来到贪吃蛇的世界\n");
	printf("\t\t\t----by adair\n\n\n");
	printf("\t按下空格开始游戏\n");
	system("pause");		//暂停屏幕,等待按键
	system("cls");			//清除屏幕
}

如何设置console的cursor的位置?

void gotoxy(int x,int y){
	COORD pos;	pos.X = x; pos.Y = y;	//在windows.h中 
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput,pos);		//设置位置
    
    CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = FALSE;			//光标不可见
	cursor.dwSize = sizeof(cursor);
	SetConsoleCursorInfo(hOutput,&cursor);
}

如何定义一个随机数?

time(NULL);		//获取从1970到现在的秒
srand(time(NULL));	//定义随机种子,
int temp = rand();	//获取的随机数和种子有关

    

结构体了解?

struct point1 {
	int x;
	int y;
};p1,p2;

struct point2 {
	int x;
	int y;
};
struct point2 p1,p2;

struct {
	int x;
	int y;
};p1,p2;

贪吃蛇源码:

#include
#include 
#include
#include

#define highth 25
#define width 35


typedef struct Snake{
	int x;
	int y;
	struct Snake* next;
}snake;
struct Food{
	int x;
	int y;
}food;

void Welcome();
void gotoxy(int x,int y);
void printxy(int x,int y);
void deletexy(int x,int y);
void CreateGraph();
void CreateSnake();
void CreateFood();
void StartScreen();

bool Judge();
void ChangeBody();
void  StartGame();

char dir = 'x';
snake* head;

int main(){
	StartScreen();
	StartGame();
	return 0;
}
//基本静态属性 
void Welcome(){
	system("color 0B");
	printf("\n\n\t欢迎来到贪吃蛇的世界\n");
	printf("\t\t\t----by adair\n\n\n");
	printf("\t按下空格开始游戏\n");
	system("pause");
	system("cls");
}
void gotoxy(int x,int y){
	COORD pos;	pos.X = x; pos.Y = y;
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput,pos);	
	
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = FALSE;
	cursor.dwSize = sizeof(cursor);
	SetConsoleCursorInfo(hOutput,&cursor);
}
void printxy(int x,int y){
	gotoxy(x,y);
	printf("■");
}
void deletexy(int x,int y){
	gotoxy(x,y);
	printf("  ");
}
void CreateGraph(){
	for(int i = 0; i<highth ;i++){
		printxy(0,i);
		printxy(width*2-2,i);
	}
	for(int i = 0; i<width*2 ;i+=2){
		printxy(i,0);
		printxy(i,highth-1);
	}
	
}
void CreateSnake(){
	head = (snake*)malloc(sizeof(snake));
	snake *p = (snake*)malloc(sizeof(snake));
	snake *q = (snake*)malloc(sizeof(snake));
	head->x = 20;	head->y = 10;	head->next = p;	 
	p->x = 18;		p->y = 	10;		p->next = q;	 	
	q->x = 16;		q->y = 10;		q->next =NULL;
	p = head;
	while(p){
		printxy(p->x,p->y);
		p = p->next;
	}
}
void CreateFood(){
	bool flag = false;
	while(!flag){
		flag = true;
		srand(time(NULL));
		int x = rand()%(width*2-4)+2;
		int y = rand()%(highth-2)+1;
		if (x%2==1) x--;
		food.x = x; food.y = y;	
		snake* p = head;		
		while(p){
			if(p->x == food.x && p->y == food.y){
				flag = false;
				break;
			}
			p = p->next;
		}
		if(flag){
			gotoxy(food.x,food.y);
			printf("★");
		}					
	}
}
void StartScreen(){
	Welcome();
	CreateGraph();
	CreateSnake();
	CreateFood();
} 

//动态属性
bool Judge(){
	int x = head->x;
	int y = head->y;
	if(x>1 && x<width*2-2 && y>0 && y<highth-1) {
		snake* p = head->next;
		while(p){
			if(x==p->x && y==p->y) return false;
			p = p->next;
		}
		return true;
	}
	
	 
	return false;
}
void ChangeBody(){
	int x = head->x;
	int y = head->y;
	switch(dir){
		case 'w': y--;break;
		case 's': y++;break;
		case 'a': x-=2;break;
		case 'd': x+=2;break;
		default: break;
	}
	if(x!=head->x ||y!=head->y){
		snake* newhead = (snake*)malloc(sizeof(snake));
		newhead->x = x; newhead->y = y; newhead->next = head;
		head = newhead;
		printxy(head->x,head->y);
		snake* p = head;
		while(p->next->next){
			p = p->next;
		}
		deletexy(p->next->x,p->next->y);
		free(p->next);
		p->next = NULL;		//没有释放最后一个位置; 
		Sleep(150); 
	}
}
void Eating(){	
	if(head->x == food.x && head->y == food.y){	
		snake* newbody = (snake*)malloc(sizeof(snake));	
		snake *p = head;	
		while(p->next){
			p = p->next;
		}
		newbody->x = p->x;newbody->y = p->y; newbody->next = NULL;
		p->next = newbody; 
		CreateFood();	
	}	
}
void Finish(){
	system("cls");
	printf("\n\t\t就这?就这?\n\n");
	system("pause");
}
void StartGame(){
 	while(Judge()){
 		if(_kbhit()){
		 	dir = _getch();
		 }
		ChangeBody();
		Eating();
	 }
	 Finish();
 }

你可能感兴趣的:(c语言,开发语言)