2024.5.7

#define _CRT_SECURE_NO_WARNINGS
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include<iostream>
#include<Windows.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include <tchar.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")//这是代码用到的一些头文件


#define MAP_HEIGHT 30
#define MAP_WIDE 30
#define SIZE 16//使用宏定义了地图大小和每个单位大小,方便后续修改

typedef struct Snakes
{
    int x;
    int y;
    struct Snakes* next;
}snake;//定义了蛇身


snake* head, * tail;
snake* head1, * tail1;//定义了两套头尾节点。这为了实现双人模式

struct Food
{
    int x;
    int y;
}food;//定义食物


int SPEED = 150;//控制蛇移动的速度
int shu = 0;//用于排序的中间变量
int grow = 0;//用于是否吃到食物的判断
int yuan[100];//用于排序的中间变量
int a[100];//用于排序的中间变量
int aa = -1;//用于游戏次数的统计
int b11 = 0;//用于排序的中间变量
int an1 = 0;//用于实现游戏暂停与开始的中间变量
int score = 0;
int score1 = 0;
int score2 = 0;//用于计算得分
int memory = 1;//用于记忆上一次的游戏模式

char ch = 75;//蛇1的初始方向为向右
char chh = 'W';//蛇2的初始方向为上


wchar_t username[1000];//获取用户名
TCHAR str[100];
TCHAR str1[100];
TCHAR str2[100];
TCHAR str3[100];
TCHAR str4[100];
TCHAR str5[100];
TCHAR str6[100];//为了把数字转化为字符串的中间变量


void BGM()
{
    PlaySound(TEXT("平凡之路.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}//播放音乐
void login()                                                 
{
    char name[100];
    initgraph(640, 480);
    IMAGE tu4;
    initgraph(640, 480);
    loadimage(&tu4, _T("tu5.jpeg"), 640, 480);
    putimage(0, 0, &tu4);
    FlushBatchDraw();
    settextcolor(WHITE);
    setfont(53, 0, _T("宋体"));
    setbkmode(TRANSPARENT);
    outtextxy(10, 40, _T("Welcome TO Greedy Snake!"));
    FlushBatchDraw();
    errno_t err;
    InputBox(username, 100, _T("请输入你的昵称"));
    FILE* fb = fopen("username.txt", "a");
    fprintf(fb, "%S ", username);
    fprintf(fb, "%c", '\n');
    fclose(fb);
} //使用文件记录玩家的用户名
void Count()
{
    FILE* fp11 = fopen("count.txt", "r");
    fscanf(fp11, "%d", &aa);
    aa = aa + 1;
    fclose(fp11);
    FILE* fp111 = fopen("count.txt", "w");
    fprintf(fp111, "%d", aa);
    fclose(fp111);
}//单独使用一个文件记录游戏的次数,方便后续分数的比较


void BUTTON1()
{
    int x = 150, y = 20;
    int w = 300, h = 100;
    TCHAR text[20] = L"开始";
    setbkmode(TRANSPARENT);
    settextstyle(100, 0, text);
    settextcolor(BLACK);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}//打印一个按钮,后面的BUTTON系列函数功能和这个函数一样
void BUTTON2()
{
    int x = 150, y = 350;
    int w = 300, h = 100;
    TCHAR text[20] = L"排名";
    setbkmode(TRANSPARENT);
    settextstyle(100, 0, text);
    settextcolor(BLACK);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON3()
{
    int x = 150, y = 180;
    int w = 300, h = 100;
    TCHAR text[20] = L"难度";
    settextstyle(100, 0, text);
    settextcolor(BLACK);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON4()
{
    int x = 150, y = 20;
    int w = 300, h = 100;
    TCHAR text[20] = L"简单";
    setbkmode(TRANSPARENT);
    settextstyle(100, 0, text);
    settextcolor(WHITE);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON5()
{
    int x = 150, y = 350;
    int w = 300, h = 100;
    TCHAR text[20] = L"困难";
    setbkmode(TRANSPARENT);
    settextstyle(100, 0, text);
    settextcolor(WHITE);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON6()
{
    int x = 150, y = 180;
    int w = 300, h = 100;
    TCHAR text[20] = L"中等";
    setbkmode(TRANSPARENT);
    settextstyle(100, 0, text);
    settextcolor(WHITE);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}

void BUTTON7()
{
    int x = 150, y = 20;
    int w = 300, h = 100;
    TCHAR text[20] = L"一般";
    setbkmode(TRANSPARENT);
    settextstyle(100, 0, text);
    settextcolor(WHITE);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON8()
{
    int x = 150, y = 350;
    int w = 300, h = 100;
    TCHAR text[20] = L"双人";
    setbkmode(TRANSPARENT);
    settextstyle(100, 0, text);
    settextcolor(WHITE);
    int tx = x + (w - textwidth(text)) / 2;
    int ty = y + (h - textheight(text)) / 2;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}

void DrawMap()
{
    setbkcolor(WHITE);
    cleardevice(); 
    for (int i = 0; i < MAP_WIDE; i++)
    {
        setfillcolor(BLUE);
       
        fillrectangle(i * SIZE, 0, (i + 1) * SIZE, SIZE);
       
        fillrectangle(i * SIZE, (MAP_HEIGHT - 1) * SIZE, (i + 1) * SIZE, MAP_HEIGHT * SIZE);
    }
    for (int i = 0; i < MAP_HEIGHT; i++)
    {
        setfillcolor(BLUE);
      
        fillrectangle(0, i * SIZE, SIZE, (i + 1) * SIZE);
        
        fillrectangle((MAP_WIDE - 1) * SIZE, i * SIZE, MAP_WIDE * SIZE, (i + 1) * SIZE);
    }
}//使用两个循环分别打印上下和左右围墙
void PrintNode(int x, int y)
{
    IMAGE tu1;
    IMAGE tu2;
   

    loadimage(&tu1, _T("b1.jpg"), 16, 16);
    loadimage(&tu2, _T("b2.jpg"), 16, 16);
    putimage(16 * x - 8, 16 * y - 8, &tu2, NOTSRCERASE);

    putimage(16 * x - 8, 16 * y - 8, &tu1, SRCINVERT);
    
}//以给的坐标为中心打出一个蛇身,为了美观这里使用了掩码图来两次贴图,后面的PrintNode系列函数功能类似,只不过是打印不同的皮肤
void PrintNode1(int x, int y)
{
    IMAGE tu1;
    IMAGE tu2;


    loadimage(&tu1, _T("a1.jpg"), 16, 16);
    loadimage(&tu2, _T("a2.jpg"), 16, 16);
    putimage(16 * x - 8, 16 * y - 8, &tu2, NOTSRCERASE);

    putimage(16 * x - 8, 16 * y - 8, &tu1, SRCINVERT);

}
void PrintNode2(int x, int y)
{
    IMAGE tu1;
    IMAGE tu2;


    loadimage(&tu1, _T("sun1.jpg"), 16, 16);
    loadimage(&tu2, _T("sun2.jpg"), 16, 16);
    putimage(16 * x - 8, 16 * y - 8, &tu2, NOTSRCERASE);

    putimage(16 * x - 8, 16 * y - 8, &tu1, SRCINVERT);

}
void PrintNode3(int x, int y)
{
    IMAGE tu1;
    IMAGE tu2;


    loadimage(&tu1, _T("apple1.png"), 16, 16);
    loadimage(&tu2, _T("apple2.png"), 16, 16);
    putimage(16 * x - 8, 16 * y - 8, &tu2, NOTSRCERASE);

    putimage(16 * x - 8, 16 * y - 8, &tu1, SRCINVERT);

}
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 savescore() {
    FILE* sksk;
    sksk = fopen("排名.txt", "a");
    fprintf(sksk, "%d\n", score);
    fclose(sksk);
}//把分数存入文件


void InitMap()
{
    
    head = (snake*)malloc(sizeof(snake));
    head->x = (MAP_WIDE) / 2;
    head->y = (MAP_HEIGHT) / 2;
    snake* p = (snake*)malloc(sizeof(snake));
    snake* q = (snake*)malloc(sizeof(snake));
    p->x = head->x + 1;
    p->y = head->y;
    q->x = head->x + 2;
    q->y = head->y;
    head->next = p;
    p->next = q;
    tail = q;
    tail->next = NULL;
    snake* temp = head;
    while (temp != NULL)
    {
        PrintNode(temp->x, temp->y);
        temp = temp->next;
    }


  
    head1 = (snake*)malloc(sizeof(snake));
    head1->x = (MAP_WIDE) / 2+3;
    head1->y = (MAP_HEIGHT) / 2+3;
    snake* p1 = (snake*)malloc(sizeof(snake));
    snake* q1 = (snake*)malloc(sizeof(snake));
    p1->x = head1->x + 1;
    p1->y = head1->y;
    q1->x = head1->x + 2;
    q1->y = head1->y;
    head1->next = p1;
    p1->next = q1;
    tail1 = q1;
    tail1->next = NULL;
    snake* temp1 = head1;
    while (temp1 != NULL)
    {
        PrintNode1(temp1->x, temp1->y);
        temp1 = temp1->next;
    }

   

    
    srand((int)time(NULL));
    food.x = rand() % (MAP_WIDE - 10) + 2;
    food.y = rand() % (MAP_HEIGHT - 10) + 2;
    if (food.x > 28 || food.y > 28)
    {
        food.x = food.x - 3;
        food.y = food.y - 3;
    }
    PrintNode2(food.x, food.y);
}//初始化地图
void ChangeBody()
{
    snake* p = head;
    int midx, midy, _midx, _midy;
    midx = p->x;
    midy = p->y;
    while (p->next != NULL)
    {
        _midx = p->next->x;
        _midy = p->next->y;
        p->next->x = midx;
        p->next->y = midy;
        midx = _midx;
        midy = _midy;
        p = p->next;
    }


    snake* p1 = head1;

    midx = p1->x;
    midy = p1->y;
    while (p1->next != NULL)
    {
        _midx = p1->next->x;
        _midy = p1->next->y;
        p1->next->x = midx;
        p1->next->y = midy;
        midx = _midx;
        midy = _midy;
        p1 = p1->next;
    }
}//把除了头节点的节点往前移一位
void MoveSnake()
{

    char oldch = ch;
    char oldchh = chh;
    if (_kbhit())
    {
        ch = _getch();
        if (ch != 'W' && ch != 'A' && ch != 'S' && ch != 'D')
        {
            ch = _getch();
        }
      
        chh = ch;
       
            
          
          
        
    }

  
  
    if ((ch == 77) && (oldch == 75))
       
        ch = oldch;
    if ((oldch == 72) && (ch == 80))
        ch = oldch;
    if ((oldch == 77) && (ch == 75))
        ch = oldch;
    if ((oldch == 80 && ch == 72))
        ch = oldch;
    if (ch != 72 && ch != 80 && ch != 75 && ch != 77)
        ch = oldch;

    if ((chh == 'W') && (oldchh == 'S'))
        
        chh = oldchh;
    if ((oldchh == 'S') && (chh == 'W'))
        ch = oldch;
    if ((oldchh == 'A') && (chh == 'D'))
        chh = oldchh;
    if ((oldchh == 'D' && chh == 'A'))
        chh = oldchh;
    if (chh != 'W' && chh != 'A' && chh != 'S' && chh != 'D')
        chh = oldchh;
   snake* p = head;
   snake* p1 = head1;
   
    int a = tail->x, b = tail->y;
    int c = tail1->x, d = tail1->y;
    
    ChangeBody();


    switch (ch)
    {        
    case 77:
        if (head->x >= 30)
        {
            head->x = 0;

        }
        head->x += 1;
        break;
    
    case 75:
        if (head->x <= 0)
        {
            head->x = 30;

        }
        head->x -= 1;
        break;
    
    case 72:
        if (head->y <= 0)
        {
            head->y = 30;

        }
        head->y--;
        break;
        
    case 80:
        if (head->y >= 30)
        {
            head->y = 0;                        

        }
        head->y++;
        break;
  
    default:
        break;
    }

    switch (chh)
    {       
    
    
    case 'D':
        if (head1->x >= 30)
        {
            head1->x = 0;
           
        }
        head1->x += 1;
        break;
      
    case 'A':
        if (head1->x<=0)
        {
            head1->x = 30;
        }
        head1->x -= 1;
        break;
   
    case 'W':
        if (head1->y <= 0)
        {
            head1->y = 30;
        }
       
        head1->y--;
        break;
     
    case 'S':
        if (head1->y >= 30)
        {
            head1->y = 0;
        }
        head1->y++;
        break;
    default:
        break;
    }
  
    if (grow)
    {
        snake* newnode;
        snake* newnode1;

        newnode = (snake*)malloc(sizeof(snake));
        newnode->x = a;
        newnode->y = b;
        tail->next = newnode;
        tail = newnode;
        tail->next = NULL;

        newnode1 = (snake*)malloc(sizeof(snake));
        newnode1->x = c;
        newnode1->y =d;
        tail1->next = newnode1;
        tail1= newnode1;
        tail1->next = NULL;

        grow = 0;
    }
 
    p = head;
   
    while (p != NULL)
    {
        PrintNode(p->x, p->y);
        p = p->next;
    }

    p1 = head1;
   
    while (p1 != NULL)
    {
        PrintNode1(p1->x, p1->y);
        p1= p1->next;
    }

   
}//实现蛇的方向控制以及链表的更新
void UpdataFood()
{
    
    snake* judge = head;
    while (judge->next != NULL)
    {
        if (judge->x == food.x && judge->y == food.y)
        {

            food.x = rand() % (MAP_WIDE - 10) + 2; 
            food.y = rand() % (MAP_HEIGHT - 10) + 2; 
            if (food.x > 28 || food.y > 28)
                continue;
            PrintNode2(food.x, food.y);
            score1 = score1 + 10;
            grow = 1;
            break;
        }
        judge = judge->next;
    }

    snake* judge1 = head1;
    while (judge1->next != NULL)
    {
        if (judge1->x == food.x && judge1->y == food.y)
        {

            food.x = rand() % (MAP_WIDE - 10) + 2; 
            food.y = rand() % (MAP_HEIGHT - 10) + 2; 
            if (food.x > 28 || food.y > 28)
                continue;
            PrintNode2(food.x, food.y);
            score2 = score2 + 10;
            grow = 1;
            break;
        }
        judge1 = judge1->next;
    }

}//更新食物
void defeat() {
    initgraph(640, 480);
    setbkmode(TRANSPARENT);
    setbkcolor(WHITE);
    cleardevice();
    settextstyle(100, 0, _T("宋体"));
    settextcolor(RGB(250, 149, 242));
    outtextxy(110, 100, _T("游戏结束"));
    settextstyle(40, 0, _T("仿宋"));
    settextcolor(BLUE);
    if (score1 > score2)
    {
        outtextxy(250, 270, _T("玩家2胜"));
    }
    if (score1 < score2)
    {
        outtextxy(250, 270, _T("玩家1胜"));
    }
    if (score1 ==score2)
    {
        outtextxy(250, 270, _T("平局"));
    }
    score1 = 0;
    score2 = 0;
   
  
    settextstyle(30, 0, _T("仿宋"));
    outtextxy(150, 400, _T("退出"));
    outtextxy(430, 400, _T("返回"));
    FlushBatchDraw();
    while (1)
    {
        if (MouseHit()) {
            ExMessage cl;
            cl = getmessage();
            if (cl.message == WM_LBUTTONDOWN) {
                FlushBatchDraw();
                if (cl.x >= 150 && cl.x <= 210 && cl.y >= 400 && cl.y <= 430) {
                    
                    exit(0);
                }
                if (cl.x >= 430 && cl.x <= 490 && cl.y >= 400 && cl.y <= 430) {
                    
                    break;
                }
            }
        }
    }


}//失败后的结算画面
//后面出现的在以单词加数字为名的函数功能与不带数字的函数功能类似,只是为了实现一般和双人两个不同的模式
void InitMap1()
{

    head = (snake*)malloc(sizeof(snake));
    head->x = (MAP_WIDE) / 2;
    head->y = (MAP_HEIGHT) / 2;
    snake* p = (snake*)malloc(sizeof(snake));
    snake* q = (snake*)malloc(sizeof(snake));
    p->x = head->x + 1;
    p->y = head->y;
    q->x = head->x + 2;
    q->y = head->y;
    head->next = p;
    p->next = q;
    tail = q;
    tail->next = NULL;
    snake* temp = head;
    while (temp != NULL)
    {
        PrintNode3(temp->x, temp->y);
        temp = temp->next;
    }

    srand((int)time(NULL));
    food.x = rand() % (MAP_WIDE - 10) + 2;
    food.y = rand() % (MAP_HEIGHT - 10) + 2;
    if (food.x > 28 || food.y > 28)
    {
        food.x = food.x - 3;
        food.y = food.y - 3;
    }
    PrintNode3(food.x, food.y);
}
void ChangeBody1()
{
    snake* p = head;
    int midx, midy, _midx, _midy;
    midx = p->x;
    midy = p->y;
    while (p->next != NULL)
    {
        _midx = p->next->x;
        _midy = p->next->y;
        p->next->x = midx;
        p->next->y = midy;
        midx = _midx;
        midy = _midy;
        p = p->next;
    }


   
}
void MoveSnake1()
{

    char oldch = ch;//记录原来的方向

    if (_kbhit())
    {
        ch = _getch();
        ch = _getch();
        if (ch == ' ')
        {
            an1 = 1;

            ch = oldch;
        }
    }

  
    if ((ch == 77) && (oldch == 75))
        
        ch = oldch;
    if ((oldch == 72) && (ch == 80))
        ch = oldch;
    if ((oldch == 77) && (ch == 75))
        ch = oldch;
    if ((oldch == 80 && ch == 72))
        ch = oldch;
    if (ch != 72 && ch != 80 && ch != 75 && ch != 77)
        ch = oldch;
    
    snake* p = head;
    
    int a = tail->x, b = tail->y;
    
    ChangeBody1();
    

    switch (ch)
    {        
    case 77:
        head->x += 1;
        break;
        
    case 75:
        head->x -= 1;
        break;
        
    case 72:
        head->y--;
        break;
        
    case 80:
        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;
    }
    
    p = head;

    while (p != NULL)
    {
        PrintNode3(p->x, p->y);
        p = p->next;
    }


  

}
void UpdataFood1()
{
    snake* judge = head;
    while (judge->next != NULL)
    {
        if (judge->x == food.x && judge->y == food.y)
        {

            food.x = rand() % (MAP_WIDE - 10) + 2;
            food.y = rand() % (MAP_HEIGHT - 10) + 2;
            if (food.x > 28 || food.y > 28)
                continue;
            PrintNode3(food.x, food.y);
            score = score + 10;
            grow = 1;
            
            break;
        }
        judge = judge->next;
    }


}
void defeat1() {
    initgraph(640, 480);
    setbkmode(TRANSPARENT);
    setbkcolor(WHITE);
    cleardevice();
    settextstyle(100, 0, _T("宋体"));
    settextcolor(RGB(250, 149, 242));
    outtextxy(110, 100, _T("游戏结束"));
    settextstyle(40, 0, _T("仿宋"));
    settextcolor(BLUE);
    outtextxy(180, 270, _T("您的得分:"));
    outtextxy(360, 270, str1);
    settextstyle(30, 0, _T("仿宋"));
    outtextxy(150, 400, _T("退出"));
    outtextxy(430, 400, _T("返回"));
    FlushBatchDraw();
    while (1)
    {
        if (MouseHit()) {
            ExMessage cl;
            cl = getmessage();
            if (cl.message == WM_LBUTTONDOWN) {
                FlushBatchDraw();
                if (cl.x >= 150 && cl.x <= 210 && cl.y >= 400 && cl.y <= 430) {
                    savescore();
                    exit(0);
                }
                if (cl.x >= 430 && cl.x <= 490 && cl.y >= 400 && cl.y <= 430) {
                    savescore();
                    break;
                }
            }
        }
    }


}

void PLAY()
{
   
    initgraph(640, 480);
    InitMap();
    time_t start;
    time(&start);
    struct tm start_tm;
    localtime_s(&start_tm, &start);
    while (1)
    {
       
        setbkcolor(WHITE);
        cleardevice();

        IMAGE tu7;
        loadimage(&tu7, _T("star.jpg"), 480 , 480 );
        putimage(0, 0, &tu7);
       
        settextcolor(BLACK);
        setfont(30, 0, _T("宋体"));
        
        
        settextcolor(BLACK);
        outtextxy(500, 80, L"倒计时:");
        settextcolor(BLACK);
        outtextxy(500, 160, L"玩家1:");
        settextcolor(BLACK);
        outtextxy(500, 250, L"玩家2:");
        settextcolor(GREEN);
       
        

        time_t current;
        time(&current);
        struct tm current_tm;
        localtime_s(&current_tm, &current);
        int now = 20- (current_tm.tm_hour * 3600 + current_tm.tm_min * 60 + current_tm.tm_sec) + (start_tm.tm_hour * 3600 + start_tm.tm_min * 60 + start_tm.tm_sec);


        _stprintf_s(str, _T("%d"), now);
        settextcolor(GREEN);
        outtextxy(500, 120, str);


        if (now == 0)
        {
            break;
        }
        MoveSnake();
        PrintNode2(food.x, food.y);
      
        UpdataFood();
        settextcolor(GREEN);
        _stprintf_s(str5, _T("%d"), score2);
        outtextxy(500, 190, str5);
        _stprintf_s(str6, _T("%d"), score1);
        outtextxy(500, 280, str6);
       
        FlushBatchDraw();
        Sleep(SPEED);

    }
   
    setbkcolor(BLACK);
    cleardevice();
    Count();
    defeat();

}//这是双人模式的大循环函数
void PLAY1()
{
    FILE* fp11111 = fopen("count.txt", "r");
    fscanf(fp11111, "%d", &b11);
    fclose(fp11111);
    int i, j, max = 0;
    login();
    initgraph(640, 480);
    DrawMap();
    InitMap1();

    time_t start;
    time(&start);
    struct tm start_tm;
    localtime_s(&start_tm, &start);

    while (Finish())
    {

        setbkcolor(WHITE);
        cleardevice();

        DrawMap();
        IMAGE tu7;
        loadimage(&tu7, _T("A1.jpeg"), 480 - 32, 480 - 32);
        putimage(16, 16, &tu7);
        FILE* fpp = fopen("排名.txt", "r");
        settextcolor(BLACK);
        setfont(30, 0, _T("宋体"));
        outtextxy(500, 0, L"得分:");
        settextcolor(GREEN);
        outtextxy(500, 40, '0');
        settextcolor(BLACK);
        outtextxy(500, 80, L"倒计时:");
        settextcolor(BLACK);
        outtextxy(500, 160, L"最高分:");
        settextcolor(BLACK);
        outtextxy(500, 250, L"玩家名:");
        settextcolor(GREEN);
        outtextxy(500, 300, username);
        for (i = 0; i < b11; i++)
        {
            fscanf(fpp, "%d", &a[i]);
            if (a[i] > max)
            {
                max = a[i];
            }
            fscanf(fpp, "%*[^\n]%*c");
        }
        _stprintf_s(str4, _T("%d"), max);
        settextcolor(GREEN);
        outtextxy(500, 200, str4);
        fclose(fpp);

        time_t current;
        time(&current);
        struct tm current_tm;
        localtime_s(&current_tm, &current);
        int now = 99 - (current_tm.tm_hour * 3600 + current_tm.tm_min * 60 + current_tm.tm_sec) + (start_tm.tm_hour * 3600 + start_tm.tm_min * 60 + start_tm.tm_sec);


        _stprintf_s(str, _T("%d"), now);
        settextcolor(GREEN);
        outtextxy(500, 120, str);


        if (now == 0)
        {
            break;
        }
        MoveSnake1();
        PrintNode3(food.x, food.y);
       
        UpdataFood1();
        settextcolor(GREEN);
        _stprintf_s(str1, _T("%d"), score);
        outtextxy(500, 40, str1);

        FlushBatchDraw();
        Sleep(SPEED);

    }

    setbkcolor(BLACK);
    cleardevice();
    Count();
    defeat1();

}//这是单人模式的大循环


void RANK()
{
    int cf = 0;
    IMAGE tu3;
    initgraph(640, 480);
    loadimage(&tu3, _T("tu4.jpeg"), 640, 480);
    putimage(0, 0, &tu3);
    FlushBatchDraw();
    int i;
    int x = 1;
    settextcolor(BLACK);
    setbkmode(TRANSPARENT);
    settextstyle(30, 0, _T("华文琥珀"));
    outtextxy(250, 20, L"玩家");
    outtextxy(380, 20, L"得分");
    outtextxy(160, 20, L"名次");
    settextcolor(BLUE);
    settextstyle(30, 0, _T("仿宋"));
    outtextxy(430, 400, _T("返回"));
    settextstyle(30, 0, _T("华文琥珀"));
    outtextxy(180, 50, '1');
    outtextxy(180, 50 + 100, '2');
    outtextxy(180, 50 + 200, '3');
    outtextxy(180, 50 + 300, '4');
    outtextxy(180, 50 + 400, '5');

    int 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 < b11; i++)
    {
        fscanf(fp, "%d", &a[i]);
        fscanf(fp, "%*[^\n]%*c");

    }
    fclose(fp);
    for (i = 0; i < 100; i++)
    {
        yuan[i] = a[i];
    }


    for (i = 0; i < b11; i++)
    {
        for (j = i + 1; j < b11; j++)
        {
            if (a[j] > a[i])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
        if (b11 < 5)
        {
            shu = b11;
        }
        else
        {
            shu = 5;
        }
    }for (i = 0; i < shu; i++)
    {
        b[i] = a[i];
    }
    FILE* fp1 = fopen("排名.txt", "r");
    for (i = 0; i < shu; i++)
    {
        for (j = 0; j < b11; j++)
        {

            if (b[i] == yuan[j] && cf == 1)
            {

                c[++i] = j;
            }
            if (b[i] == yuan[j] && cf != 1)
            {
                c[i] = j;
                cf = 1;
            }


        }
        cf = 0;

        rewind(fp1);
    }

    fclose(fp1);
    FILE* fp3 = fopen("排名.txt", "r");
    FILE* fp2 = fopen("username.txt", "r");
    for (i = 0; i < shu; i++)
    {
        for (j = 0; j < c[i]; j++)
        {
            fscanf(fp2, "%*[^\n]%*c");
            fscanf(fp3, "%*[^\n]%*c");
        }
        fscanf(fp2, "%S", &name);
        fscanf(fp3, "%S", &q);
        settextcolor(BLACK);
        setbkmode(TRANSPARENT);
        settextstyle(30, 0, _T("微软雅黑"));
        outtextxy(250, 50 + 100 * i, name);
        settextstyle(30, 0, _T("华文琥珀"));
        outtextxy(380, 50 + 100 * i, q);
        rewind(fp2);
        rewind(fp3);
    }
    FlushBatchDraw();
   
    fclose(fp2);
    fclose(fp3);
    while (1)
    {
        if (MouseHit()) {
            ExMessage cl;
            cl = getmessage();
            if (cl.message == WM_LBUTTONDOWN) {
                FlushBatchDraw();
                if (cl.x >= 430 && cl.x <= 490 && cl.y >= 400 && cl.y <= 430) {
                    break;
                }
            }
        }
    }
}//实现排名
void choice()
{
    int count = 0;
    initgraph(640, 480);

    IMAGE tu2;
    loadimage(&tu2, _T("tu3.jpeg"), 640, 480);
    putimage(0, 0, &tu2);
    FlushBatchDraw();
    settextcolor(BLUE);
    setbkmode(TRANSPARENT);
    settextstyle(30, 0, _T("仿宋"));
    outtextxy(430, 400, _T("返回"));

    BUTTON4();
    BUTTON5();
    BUTTON6();
    setbkmode(TRANSPARENT);
    settextcolor(RED);
    if (SPEED == 200)
        outtextxy(200, 20, _T("简单"));
    if (SPEED == 150)
        outtextxy(200, 180, _T("中等"));
    if (SPEED == 120)
        outtextxy(200, 350, _T("困难"));
    FlushBatchDraw();
    ExMessage msg1;

    while (true) {

        if (MouseHit()) {
            msg1 = getmessage();
            if (msg1.message == WM_LBUTTONDOWN) {

                if (msg1.x >= 200 && msg1.x <= 400 && msg1.y >= 20 && msg1.y < 120)
                {
                    setbkmode(TRANSPARENT);
                    SPEED = 200;
                    settextcolor(RED);
                    outtextxy(200, 20, _T("简单"));
                    settextcolor(WHITE);
                    outtextxy(200, 180, _T("中等"));
                    outtextxy(200, 350, _T("困难"));
                    FlushBatchDraw();
                }
                if (msg1.x >= 200 && msg1.x <= 400 && msg1.y >= 180 && msg1.y < 280)
                {
                    setbkmode(TRANSPARENT);
                    SPEED = 150;
                    settextcolor(RED);
                    outtextxy(200, 180, _T("中等"));
                    settextcolor(WHITE);
                    outtextxy(200, 350, _T("困难"));
                    outtextxy(200, 20, _T("简单"));
                    FlushBatchDraw();
                }
                if (msg1.x >= 200 && msg1.x <= 400 && msg1.y >= 350 && msg1.y < 450)
                {
                    setbkmode(TRANSPARENT);
                    SPEED = 120;
                    settextcolor(RED);
                    outtextxy(200, 350, _T("困难"));
                    settextcolor(WHITE);
                    outtextxy(200, 20, _T("简单"));
                    outtextxy(200, 180, _T("中等"));
                    FlushBatchDraw();
                }

                if (msg1.x >= 430 && msg1.x <= 490 && msg1.y >= 400 && msg1.y <= 430) {
                    break;
                }
            }
        }
    }


    


}//实现难度选择
void choice1()
{
    int count = 0;
    initgraph(640, 480);

    IMAGE tu2;
    loadimage(&tu2, _T("tu3.jpeg"), 640, 480);
    putimage(0, 0, &tu2);
    FlushBatchDraw();
    settextcolor(BLUE);
    setbkmode(TRANSPARENT);
    settextstyle(30, 0, _T("仿宋"));
    outtextxy(430, 400, _T("返回"));

    BUTTON7();
    BUTTON8();
 
    setbkmode(TRANSPARENT);
    settextcolor(RED);
    if (memory == 1)
        outtextxy(200, 20, _T("一般"));
   
    if (memory == 3)
        outtextxy(200, 350, _T("双人"));
    FlushBatchDraw();
    ExMessage msg1; 

    while (true) {

        if (MouseHit()) {
            msg1 = getmessage();
            if (msg1.message == WM_LBUTTONDOWN) {

                if (msg1.x >= 200 && msg1.x <= 400 && msg1.y >= 20 && msg1.y < 120)
                {
                    setbkmode(TRANSPARENT);
                   
                    settextcolor(RED);
                    outtextxy(200, 20, _T("一般"));
                    memory = 1;
                    settextcolor(WHITE);
                    outtextxy(200, 180, _T("双人"));
                   
                    PLAY1();
                }
                if (msg1.x >= 200 && msg1.x <= 400 && msg1.y >= 180 && msg1.y < 280)
                {
                   
                }
                if (msg1.x >= 200 && msg1.x <= 400 && msg1.y >= 350 && msg1.y < 450)
                {
                    setbkmode(TRANSPARENT);
                   
                    settextcolor(RED);
                    outtextxy(200, 350, _T("双人"));
                    memory = 3;
                    settextcolor(WHITE);
                    outtextxy(200, 20, _T("一般"));
                    PLAY();
                    FlushBatchDraw();
                   
                }

                if (msg1.x >= 430 && msg1.x <= 490 && msg1.y >= 400 && msg1.y <= 430) {
                    break;
                }
            }
        }
    }

}//实现模式选择

int main()
{
    FILE* fp11111 = fopen("count.txt", "r");
    fscanf(fp11111, "%d", &b11);

    fclose(fp11111);
   
    BGM();
    char a[1000];
    int i, j, num = 0, max = 0;

    initgraph(640, 480);

    BeginBatchDraw();
    setbkmode(TRANSPARENT);
    IMAGE tu1;
    loadimage(&tu1, _T("A1.jpeg"), 640, 480);
    putimage(0, 0, &tu1);
    FlushBatchDraw();
    BUTTON1();
    BUTTON2();
    BUTTON3();
    
    ExMessage msg; 

    while (true) {

        setbkmode(TRANSPARENT);
        loadimage(&tu1, _T("A1.jpeg"), 640, 480);
        putimage(0, 0, &tu1);
        BUTTON1();
        BUTTON2();
        BUTTON3();

        FlushBatchDraw();
        if (peekmessage(&msg, EM_MOUSE)) {
            switch (msg.message)
            {
            case WM_LBUTTONDOWN:
                if (msg.x >= 200 && msg.x <= 400 && msg.y >= 20 && msg.y < 120)
                {
                    if (msg.x >= 200 && msg.x <= 400 && msg.y >= 20 && msg.y < 120)

                        choice1();

                    score = 0;
                    continue;

                }
                if (msg.x >= 200 && msg.x <= 400 && msg.y >= 350 && msg.y < 450)
                {
                    FILE* fp11111 = fopen("count.txt", "r");
                    fscanf(fp11111, "%d", &b11);

                    fclose(fp11111);

                    RANK();
                    continue;

                }
                if (msg.x >= 200 && msg.x <= 400 && msg.y >= 180 && msg.y < 280)
                {
                    choice();
                    continue;

                }break;

            default:break;
            }
        }
    }
    defeat();
    system("pause");

}//这是总的游戏主体的循环
 

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值