天天看点

贪吃蛇(C语言版)

还有比较多的bug,但是没空更改了,这只是写给自己看的。以后用到了就能直接拷过来。

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>

#define MAXLINE	32
#define MAXROW	16
#define LEFT	0
#define RIGHT	1
#define UP		2
#define DOWN	3

void gotoxy(int x,int y){
	HANDLE h1; 
	COORD pos;
	pos.X=x;
	pos.Y=y;
	h1=GetStdHandle(STD_OUTPUT_HANDLE); 
	SetConsoleCursorPosition(h1,pos); 
//	printf("hello world!\n");
}
int getdir(int *p){
	char c=getch();
	if(c == 'w' && *p != DOWN)
		return UP;
	else if(c == 's' && *p != UP)
		return DOWN;
	
	else if(c == 'a' && *p != RIGHT)
		return LEFT;
	else if(c == 'd' && *p != LEFT)
		return RIGHT;
	else if(c == 'p'){
		gotoxy(8,4);
		printf("!pause!");
		while(getch() == 'b'){
			gotoxy(8,4);
			printf("       ");
			return *p;
		}
	}
//	else 
//		return *p;
}

void initsnak(){
	int i;
	gotoxy(0,0);
	for(i=0;i<MAXLINE;i++){
		gotoxy(i,0);
		printf("-");
		gotoxy(i,MAXROW);
		printf("-");
	}
	for(i=0;i<MAXROW;i++){
		gotoxy(MAXLINE,i);
		printf("|");
		gotoxy(0,i);
		printf("|");
	}
	gotoxy(MAXLINE+5,3);
	printf("方向键:w,s,a,d:分别是上、下、左、右;\n");
	gotoxy(MAXLINE+5,4);
	printf("规则说明:\n");
	gotoxy(MAXLINE+5,5);
	printf("	蛇头不能触碰四周墙壁和蛇身!否则游戏结束\n");
	gotoxy(MAXLINE+5,6);
	printf("暂时没有增加选关功能,\n");
	gotoxy(MAXLINE+5,7);
	printf("	是从最容易开始,难度慢慢增加!\n");
	gotoxy(MAXLINE+5,9);
	printf("快乐游戏,快乐学习\n");
}

int main(){


	int X[255],Y[255],foodx,foody,score,level;
	int lens ,dir,xa,ya,i,lastkey,totalscore;
S:	X[0]=10;Y[0]=6;
	X[1]=9;Y[1]=6;
	dir = RIGHT; 
	totalscore=0;
	lens=2;score=0;level=0;xa=0;ya=0;
	initsnak();
	srand((int )time(0));
	foodx=rand()%MAXLINE;
	foody=rand()%MAXROW;
	if(foodx <= 0 ||foodx >= MAXLINE){
		foodx=1;
	}
	if(foody <= 0 || foody >= MAXROW){
		foody = 1;
	}
	gotoxy(foodx,foody);
	printf("O");
	while(1){
		lastkey=dir;
		if(kbhit()){//读取键值
			dir=getdir(&lastkey);
		//	printf("dir = *%d*",dir);
		}
		//runing(X,Y,&len);
		for(i=0;i<lens;i++){
			gotoxy(X[i],Y[i]);
			printf("X");
		}
		if(level >25)level =25;
		Sleep(300-level*10);
		
	
		gotoxy(X[lens-1],Y[lens-1]);
		printf(" ");

	
		
		if(X[0] == foodx && Y[0] == foody){
			foodx=rand()%MAXLINE;
			foody=rand()%MAXROW;
			if(foodx == 0 ||foodx == MAXLINE){
				foodx=1;
			}
			if(foody == 0 || foody == MAXROW){
				foody = 1;
			}
			gotoxy(foodx,foody);
			printf("O");
			lens++;
			score++;
			totalscore++;
			if(score >= 2){
				score = 0;
				level++;
			}
		}
		for(i=lens-1;i>0;i--){
			X[i]=X[i-1];
			Y[i]=Y[i-1];
		}
		gotoxy(10,20);
		printf("head(x,y)=(%d,%d)",X[0],Y[0]);
		gotoxy(10,21);
		printf("totalscore: %d  ,  level: %d",totalscore,level+1);
		if(dir == LEFT){
			X[0]--;
		}
		else if(dir == RIGHT){
			X[0]++;
		}
		else if(dir == UP){
			Y[0]--;
		}
		else if(dir == DOWN){
			Y[0]++;
		}
		//判断结束,还有判断是否和当前长度的蛇身有碰撞
		for(i=lens-1;i>0;i--){
			if(X[0] == X[i] && Y[0] == Y[i]){
				end = 1;
			}
		}
		if(flag == 1 || \
			X[0] >= (MAXLINE) || Y[0] >= (MAXROW) || X[0] <=0 ||Y[0] <=0){
			gotoxy(11,4);
			printf("game over\n");
			gotoxy(8,5);
			printf("play agin:(Y or N)\n");
			if(getchar() == 'Y'){
				system("cls");
				goto S;
			}
			else return 0;
		}

	}
	return 0;
}