貪喫蛇self2

這個版本的所有更新都寫註釋了,只是注意要試玩的話不用再全屏了,開始時那個就行。

/*
	更新計劃:
	調整屏幕大小
	對原來的算法進行優化,除去一些bug;
	增加開始畫面;
	完善圖形;
	增加顏色;
	增加邊框;
	選擇等級;
*/
/*
	需要增加下面三個函數
	void gotoxy(int x, int y)//設置光標位置
	{
		COORD pos = {x,y};	 //定義一個字符在控制檯屏幕上的座標POS  
		SetConsoleCursorPosition(hConsole, pos);	//定位光標位置的函數<windows.h>
	}
	例如:gotoxy(ix*2, iy);之後再直接用printf就行。

	void Hide_Cursor()//隱藏光標 固定函數,可以使光標不閃來閃去的 
	{
		CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 
		SetConsoleCursorInfo(hConsole, &cursor_info);	 
	}

	void SetColor(int color)//設置顏色
	{
		SetConsoleTextAttribute(hConsole, color);
		//是API設置字體顏色和背景色的函數 格式:SetConsoleTextAttribute(句柄,顏色);
	}
    例如SetColor(0xf);            //oxf代表分配的內存地址  setcolor:34行自定義設置顏色的函數 

	還要加個這東西:
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);	//獲取標準輸出的句柄 <windows.h>
	//句柄 :標誌應用程序中的不同對象和同類對象中的不同的實例 方便操控,

	附上顏色所對應的數字0 = 黑色       8 = 灰色
						1 = 藍色       9 = 淡藍色
						2 = 綠色       A = 淡綠色
						3 = 湖藍色     B = 淡淺綠色
						4 = 紅色       C = 淡紅色
						5 = 紫色       D = 淡紫色
						6 = 黃色       E = 淡黃色
						7 = 白色       F = 亮白色
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

int map[20][80];		//用於顯示
int hx[80],hy[80];		//記錄蛇身
int x,y;				//肉的位置
int die;
int length;
int cax,cay,kca=0;
int hard;

void gotoxy(int x, int y)
{
	COORD pos = {x,y};
	SetConsoleCursorPosition(hConsole, pos);
}

void Hide_Cursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 
	SetConsoleCursorInfo(hConsole, &cursor_info);	 
}

void SetColor(int color)//設置顏色
{
	SetConsoleTextAttribute(hConsole, color);
}

void setfood()
{
	do
	{	
		x=rand()%20;
		y=rand()%79;
	}while(x==0||y==0);
	map[x][y]=2;
}

void init()
{
	int i,j,k;
	length=3;
	Hide_Cursor();
	memset(map,0,sizeof(map));
	for(i=0;i<length;i++)
	{
		hx[i]=1;
		hy[i]=i+2;
		map[1][i+2]=1;
	}
	setfood();
	gotoxy(20,10);
	SetColor(0x0f);
	printf("Which level do you want to choose?");
	gotoxy(20,11);
	SetColor(0x0f);
	printf("(you can choose level from 1 to 5)");
	gotoxy(35,12);
	SetColor(0x0f);
	scanf("%d",&hard);
	system( "cls" );
	gotoxy(20,10);
	SetColor(0x0f);
	printf("Let's start(Please press any key)");
}

int judge()
{
	if((map[hx[length-1]][hy[length-1]]==1)||(hx[length-1]==0||
		hy[length-1]==0||hx[length-1]==20||hy[length-1]==78))
		return 1;
	else
		return 0;
}

void move(char key)
{
	int i,j,k;
	int lx=hx[length-1],ly=hy[length-1];
	if(key=='a')
	{
		hx[length-1]=hx[length-1];
		hy[length-1]=hy[length-1]-1;
		if(judge())
		{die=1;return;}
		else
		map[hx[length-1]][hy[length-1]]=1;
	}
	else if(key=='d')
	{
		hx[length-1]=hx[length-1];
		hy[length-1]=hy[length-1]+1;
		if(judge())
		{die=1;return;}
		else
		map[hx[length-1]][hy[length-1]]=1;
	}
	else if(key=='w')
	{
		hx[length-1]=hx[length-1]-1;
		hy[length-1]=hy[length-1];
		if(judge())
		{die=1;return;}
		else
		map[hx[length-1]][hy[length-1]]=1;
	}
	else if(key=='s')
	{
		hx[length-1]=hx[length-1]+1;
		hy[length-1]=hy[length-1];
		if(judge())
		{die=1;return;}
		else
		map[hx[length-1]][hy[length-1]]=1;
	}
	if(hx[length-1]==x&&hy[length-1]==y)
	{
		hx[length]=x;
		hy[length]=y;
		hx[length-1]=lx;
		hy[length-1]=ly;
		map[x][y]=1;
		length++;
		setfood();
	}
	else
	{
		map[hx[0]][hy[0]]=0;
		cax=hx[0],cay=hy[0];
		kca=1;
		for(i=0;i<length-2;i++)
		{
			hx[i]=hx[i+1];
			hy[i]=hy[i+1];
		}
		hx[length-2]=lx;
		hy[length-2]=ly;
	}
}

void madewall()
{
	int i,j;
	for(i=0;i<79;i+=2)
	{
		gotoxy(i,0);
		SetColor(8);
		printf("■");
	}
	for(i=1;i<=19;i++)
	{
		gotoxy(0,i);
		SetColor(8);
		printf("■");
	}
	for(i=1;i<=19;i++)
	{
		gotoxy(78,i);
		SetColor(8);
		printf("■");
	}
	for(i=0;i<79;i+=2)
	{
		gotoxy(i,20);
		SetColor(8);
		printf("■");
	}
}

void show()
{
	int i,j;
	if(kca)
	{
		gotoxy(cay,cax);
		printf(" ");
		kca=0;
	}
	madewall();
	gotoxy(hy[length-1],hx[length-1]);
	SetColor(0x0f);
	printf("*");
	gotoxy(y,x);
	SetColor(0x0f);
	printf("⊙");
}

char judge(char key,char lastkey)
{
	if(key=='a'&&lastkey=='d')
		return lastkey;
	else if(key=='d'&&lastkey=='a')
		return lastkey;
	else if(key=='w'&&lastkey=='s')
		return lastkey;
	else if(key=='s'&&lastkey=='w')
		return lastkey;
	else
		return key;
}

int main()
{
	int time=GetTickCount();
	int lasttime=time;
	srand( (unsigned int)GetTickCount() );
	char key='d';
	char lastkey=key;
	init();
	die=0;
	while(!kbhit());
	char sta=getch();
	system( "cls" );
	int speed=500-hard*100;
	while(1)
	{
		if(kbhit())
		{
			key=getch();
			if(key=='a'||key=='s'||key=='d'||key=='w')
			{
				key=judge(key,lastkey);
				lastkey=key;
				move(key);
				if(die)
				{
					gotoxy(30,10);
					SetColor(0x0f);
					printf("YOU lose\n");
					gotoxy(20,11);
					break;
				}
				show();
				Sleep(speed);
			}
		}
		else
		{
			if(GetTickCount()-lasttime<500)
				Sleep(1);
			lasttime=GetTickCount();
			move(lastkey);
			if(die)
			{
				gotoxy(20,10);
				SetColor(0x0f);
				printf("You lose\n");
				break;
			}
			show();
			Sleep(speed);
		}
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章