贪吃蛇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();
/**/


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值