这个版本的所有更新都写注释了,只是注意要试玩的话不用再全屏了,开始时那个就行。
/*
更新计划:
调整屏幕大小
对原来的算法进行优化,除去一些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;
}