//先清空所有结点,再打印,实现动态效果
snake* p = head;
while (p != NULL)
{
DeleteNode(p->x, p->y);
p = p->next;
}
//记载下尾结点的坐标,为吃到食品生成新结点做准备
int a = tail->x, b = tail->y;
//除头结点外 ,剩余结点前面结点的坐标赋值给后面的结点
ChangeBody();
//更新头结点的坐标
switch (ch)
{ //向右边转
case 'd':
case 'D':
head->x += 1;
break;
//左转
case 'a':
case 'A':
head->x -= 1;
break;
//向上
case 'w':
case 'W':
head->y--;
break;
//向下
case 's':
case 'S':
head->y++;
break;
default:
break;
}
//如果吃到食品,就用尾插法插入一个结点
if (grow)
{
snake* newnode;
newnode = (snake*)malloc(sizeof(snake));
newnode->x = a;
newnode->y = b;
tail->next = newnode;
tail = newnode;//更新尾结点
tail->next = NULL;
grow = 0;//更新grow的值
}
//重新打印所有结点
p = head;
while (p != NULL)
{
PrintNode(p->x, p->y);
p = p->next;
}
Sleep(SPEED);//控制速度
}
bool Finish()//判断是否竣事
{
//蛇撞墙,或者蛇头撞到身上,则游戏竣事
if (head->x <= 1 || head->x >= (MAP_WIDE - 1) || head->y <= 1 || head->y >= (MAP_HEIGHT - 1))
return 0;
snake* p = head->next;
while (p != NULL)
{
if (head->x == p->x && head->y == p->y)
return 0;
p = p->next;
}
return 1;
}
void BUTTON1()
{
int x = 100, y = 100;
int w = 300, h = 100;
TCHAR text[20] = L"start";
setbkmode(TRANSPARENT);
setfillcolor(GREEN);
fillroundrect(x, y, x + w, y + h, 10, 10);
// TCHAR s1[] = L"Arial BLACK";
settextstyle(100, 0, text);
settextcolor(WHITE);
int tx = x + (w - textwidth(text)) / 2;
int ty = y + (h - textheight(text)) / 2;
outtextxy(tx, ty, text);
}
void BUTTON2()
{
int x = 100, y = 300;
int w = 300, h = 100;
TCHAR text[20] = L"RANK";
setbkmode(TRANSPARENT);
setfillcolor(GREEN);
fillroundrect(x, y, x + w, y + h, 10, 10);
// TCHAR s1[] = L"Arial BLACK";
settextstyle(100, 0, text);
settextcolor(WHITE);
int tx = x + (w - textwidth(text)) / 2;
int ty = y + (h - textheight(text)) / 2;
outtextxy(tx, ty, text);
}
void PLAY()
{
//login();
initgraph(640, 480);
DrawMap();
InitMap();
//FILE* fb = fopen("排名.txt", "r");
//for (i = 0; i<10;i++)
//{
// fgets(a, 200, fb);
//outtextxy(20, 30 * j, a);
// for (j = 0; j < strlen(a)-1; j++)
// {
// num = num * 10 + int(a[j] - '0');
//
// }
// if (num >= max)
// {
// max = num;
//
//}
//printf("%d", max);
//num = 0;
//memset(a, 0, sizeof(a));
//}
// while (1)
//_stprintf_s(str2, _T("%d"), max);
//settextcolor(GREEN);
//outtextxy(300, 20, str2);
//while (1);
//fclose(fb);
while (Finish())
{
MoveSnake();
while (a1)
{
ch1 = _getch();
if (ch1 == ' ')
{
a1 = 0;
}
};
UpdataFood();
}
//int num = 999;
FILE* fa = fopen("排名.txt", "a");
fprintf(fa, "%d", score);
fprintf(fa, "%c", '\n');
fclose(fa);
//printf("游戏竣事!\n您的得分为:%d", score * 10);
/* FILE* fb = fopen("排名.txt", "r");
for (i = 0; i++; i < 100)
{
fgets(a, 10, fb);
for (j = 0; j < strlen(a); j++)
{
num = num * 10 + int(a - '0');
}
if (num > max)
{
max = num;
}
//printf("%d", max);
_stprintf_s(str2, _T("%d"),max);
outtextxy(400, 20, str2);
}
//while (1);
fclose(fb);*/
setbkcolor(BLACK);
cleardevice();
_stprintf_s(str, _T("%d"), score);
outtextxy(100, 200, str);
_getch();
}
void RANK()
{
int i;
initgraph(640, 480);
setbkcolor(WHITE);
cleardevice();
settextcolor(GREEN);
outtextxy(100, 20, L"玩家");
outtextxy(200, 20, L"得分");
outtextxy(50, 20, L"名次");
outtextxy(50, 50, '1');
outtextxy(50, 50+100, '2');
outtextxy(50, 50+200, '3');
outtextxy(50, 50+300, '4');
outtextxy(50, 50+400, '5');
int a[100],b[5],c[5], max = 0, temp = 0,p=0;
wchar_t name[100],q[100];
int j,k,b1=0,count=0;
FILE* fp = fopen("排名.txt", "r");
for (i = 0; i < 10; i++)
{
fscanf(fp, "%d", &a);
fscanf(fp, "%*[^\n]%*c");
}
fclose(fp);
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 9; j++)
{
if (a[j] > a)
{
temp = a;
a = a[j];
a[j] = temp;
}
}
}for (i = 0; i < 5; i++)
{
b = a;
}
FILE* fp1 = fopen("排名.txt", "r");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 10; j++)
{
fscanf(fp, "%d", &b1);
if (b==b1)
{
c = j;
}