1. 程式人生 > >貪吃蛇(C語言版)

貪吃蛇(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;
}