貪喫蛇self3

這個版本其實沒更新啥,就是根據學長的建議寫成了工程的形式,還有處理了一些自己發現的bug

這個是main.c裏的

/*
	本次目標:
	1.實現工程化 
	2、解決長寬不一樣的問題
	3,優化部分代碼 
*/
#include "tan.h"

int main()
{
	int time=GetTickCount();
	int lasttime=time;
	srand( (unsigned int)GetTickCount() );
	char key='d';
	char lastkey=key;
	init();
	while(!kbhit());
	char sta=getch();
	system( "cls" );
	int speed=lev();
	while(1)
	{
		if(kbhit())
		{
			lasttime=GetTickCount();
			key=getch();
			if(key=='a'||key=='s'||key=='d'||key=='w')
			{
				key=judge(key,lastkey);
				lastkey=key;
				move(key);
				if(jdie())
				{
					gotoxy(30,10);
					SetColor(0x0f);
					printf("YOU lose\n");
					gotoxy(20,11);
					break;
				}
				show();
			}
		}
		if(GetTickCount()-lasttime>speed)
		{
			lasttime=GetTickCount();
			move(lastkey);
			if(jdie())
			{
				gotoxy(30,10);
				SetColor(0x0f);
				printf("You lose\n");
				gotoxy(20,11);
				break;
			}
			show();
		}
	}
	return 0;
}
這個是tan.c裏的

/**
*tan.c
**/
///header file include
#include"tan.h"

///variable definition
int level[6]={10000,300,200,150,100,50};

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

///function definition
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()%19;
		y=rand()%78;
	}while(x==0||y==0||y&1||map[x][y]==1);
	map[x][y]=2;
}

void init()
{
	int i,j,k;
	die=0;
	length=3;
	Hide_Cursor();
	memset(map,0,sizeof(map));
	for(i=0;i<length;i++)
	{
		hx[i]=1;
		hy[i]=i*2+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]-2;
		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]+2;
		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 jdie()
{
	if(die)
		return 1;
	else
		return 0;
}

int lev()
{
	return level[hard];
}


這個是tan.h裏的

///header file include
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
///macro definition

///variable definition

///function definition
void gotoxy(int x, int y);
void Hide_Cursor();
void SetColor(int color);//設置顏色
void setfood();
void init();
int judge();
void move(char key);
void madewall();
void show();
char judge(char key,char lastkey);
int jdie();
int lev();
/**/


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