c‘c‘c‘c‘c‘cccccccccccc‘c‘c‘c


#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;//定义食物的结构

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, a2 = 1, a3 = 0;;//grow =0 表示没吃到食物,grow = 1表示吃到食物
int score = 0;
int count = 1;
char ch1 = 1;
char ch = 75;//初始方向向左

wchar_t username[1000];

TCHAR str[100];
TCHAR str1[100];
TCHAR str2[100];
TCHAR str3[100];
TCHAR str4[100];

void BGM()
{
    PlaySound(TEXT("平凡之路.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);


}
void login()                                                  //登录界面//
{
    char name[100];
    initgraph(640, 480);
    setbkcolor(RGB(137,25,237));
    cleardevice();
    settextcolor(BLACK);
    setfont(53, 0, _T("宋体"));
    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 DrawMap()//打印地图
{
    setbkcolor(WHITE);
    cleardevice(); //调用清屏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)
{
    setfillcolor(RED);
    fillcircle(x * SIZE, y * SIZE, SIZE/2);
    setcolor(WHITE);
    circle(x * SIZE, y * SIZE, SIZE/2);
}
//删除结点
void DeleteNode(int x, int y)
{
    setfillcolor(WHITE);
    fillcircle(x * SIZE, y * SIZE, SIZE/2);
    setcolor(WHITE);
    circle(x * SIZE, y * SIZE, SIZE/2);

}

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;
    }
    //初始化食物
    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;
    }
    PrintNode(food.x, food.y);
}
float Distance(int x1,int y1,int x2,int y2)
{

    return(sqrt((x1 - x2)*(x1-x2) + (y1 - y2)*(y1-y2)));


}
//Distance(judge->x, judge->y, food.x, food.y) <= 2
void UpdataFood()
{
    //如果蛇的结点与食物的坐标相等了,则需要创造新的食物
    snake* judge = head;
    while (judge->next != NULL)//遍历所有结点
    {
        if (judge->x == food.x && judge->y == food.y)
        {
            //setfillcolor(WHITE);
            //solidcircle(food.x*16, food.y*16, 16);
            food.x = rand() % (MAP_WIDE - 10) + 2; //生成2~MAP_WIDE - 2的随机数
            food.y = rand() % (MAP_HEIGHT - 10) + 2; //生成2~MAP_HEIGHT - 2的随机数
            if (food.x >28 || food.y >28)
                continue;
            PrintNode(food.x, food.y);
            score=score+10;
            grow = 1;
            settextcolor(GREEN);
            _stprintf_s(str1, _T("%d"), score);

            outtextxy(500,40, str1);
            
            break;
        }
        judge = judge->next;
    }

}
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;
    }
}

void MoveSnake()
{
    
    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;
    while (p != NULL)
    {
        DeleteNode(p->x, p->y);
        p = p->next;
    }
    //记录下尾结点的坐标,为吃到食物生成新结点做准备
    int a = tail->x, b = tail->y;
    //除头结点外 ,剩余结点前面结点的坐标赋值给后面的结点
    ChangeBody();
    //更新头结点的坐标

    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;//更新grow的值
    }
    //重新打印所有结点
    p = head;
    setfillcolor(BLACK);
    fillcircle(p->x * SIZE, p->y * SIZE, SIZE / 2);
    setcolor(WHITE);
    circle(p->x * SIZE,p-> y * SIZE, SIZE / 2);
    p = p->next;
    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 = 150, y = 20;
    int w = 300, h = 100;
    TCHAR text[20] = L"开始";
    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;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON2()
{
    int x = 150, y = 350;
    int w = 300, h = 100;
    TCHAR text[20] = L"排名";
    setbkmode(TRANSPARENT);
    //setfillcolor(GREEN);
    //fillroundrect(x, y, x + w, y + h, 10, 10);
    
    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 BUTTON3()
{
    int x = 150, y = 150;
    int w = 300, h = 100;
    TCHAR text[20] = L"难度";
   // 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;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON4()
{
    int x = 150, y = 20;
    int w = 300, h = 100;
    TCHAR text[20] = L"简单";
    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;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON5()
{
    int x = 150, y = 350;
    int w = 300, h = 100;
    TCHAR text[20] = L"困难";
    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;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}
void BUTTON6()
{
    int x = 150, y = 150;
    int w = 300, h = 100;
    TCHAR text[20] = L"中等";
    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;
    setbkmode(TRANSPARENT);
    outtextxy(tx, ty, text);
}

void PLAY()
{
    FILE* fp11111 = fopen("count.txt", "r");
    fscanf(fp11111, "%d", &b11);
    fclose(fp11111);
    int i,j,max=0;
    login();
    initgraph(640, 480);
    DrawMap();
    InitMap();
    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 start;
    time(&start);
    struct tm start_tm;
    localtime_s(&start_tm, &start);//倒计时准备

    while (Finish())
    {
        
        FlushBatchDraw();
        time_t current;
        // 更新current
        time(&current);
        // 计算倒计时
        struct tm current_tm;
        localtime_s(&current_tm, &current);
        // 倒计时为99秒
        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;
        }
        MoveSnake();
        while (an1)
        {
            ch1 = _getch();
            if (ch1 == ' ')
            {
                an1 = 0;
            }
        };
        UpdataFood();
      
    }
    //int num = 999;
    FILE* fa = fopen("排名.txt", "a");
    fprintf(fa, "%d", score);
    fprintf(fa, "%c", '\n');
    fclose(fa);

    setbkcolor(BLACK);
    cleardevice();

   
   
    Count();
    
}
void RANK()
{
    int cf = 0;
  

 
    
    int i;
    int x = 1;
    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 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);
        outtextxy(100, 50+100*i, name);
        outtextxy(200, 50+100*i, q);
        FlushBatchDraw();
        rewind(fp2);
        rewind(fp3);
    }
    fclose(fp2);
    fclose(fp3);
    while (1)
    {
        if (getchar())
        {
            break;
        }
    } 
}

void choice()
{
    int count = 0;
    initgraph(640,480);
   
   
  
    setbkcolor(RGB(37,142,255));
    cleardevice();
    
    BUTTON4();
    BUTTON5();
    BUTTON6();
    FlushBatchDraw();
    ExMessage msg1; //声明一个消息指针
    
    while (true) {

        
        
        if (peekmessage(&msg1, EM_MOUSE)) {
            switch (msg1.message)
            {
            case WM_LBUTTONDOWN:
                if (msg1.x >= 150 && msg1.x <= 450 && msg1.y >= 20 && msg1.y < 120)
                {
                    SPEED = 300;
                    count++;
                    
                   
                }
                if (msg1.x >= 150 && msg1.x <= 450 && msg1.y >= 350 && msg1.y < 450)
                {
                    SPEED = 100;
                    count++;
                   

                }
                if (msg1.x >= 150 && msg1.x <= 450 && msg1.y >= 150 && msg1.y < 250)
                {
                    SPEED = 200;
                    count++;
                    
                }break;

                //case WM_MOUSEMOVE:
                    //if (msg.x >= 240 && msg.x <= 240 + 50 && msg.y >= 50 && msg.y <= 50 + 40)
                   // {

                   //}break;
                        //tipInfo(tipInfoRect, title[0]);
                    //if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 140 && msg.y <= 140 + 50)
                        //tipInfo(tipInfoRect, title[1]);
                   // if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 230 && msg.y <= 230 + 50)
                        //tipInfo(tipInfoRect, title[2]);
                   // if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 320 && msg.y <= 320 + 50)
                       // tipInfo(tipInfoRect, title[3]);

            default:break;
            }
            if (count == 1)
            {
                break;

            }
            
        }
        
    }

    
    //FlushBatchDraw();

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

    fclose(fp11111);
    IMAGE bjut;
    BGM();
    char a[1000];
    int i, j,num=0,max=0;
    
    initgraph(640, 480);
    
    BeginBatchDraw();
    setbkmode(TRANSPARENT);
    loadimage(&bjut, _T("R-C.jpeg"), 640, 480);
    putimage(0, 0, &bjut);
    FlushBatchDraw();
    BUTTON1();
    BUTTON2();
    BUTTON3();
//C:\\Users\\licha\\Desktop\\贪吃蛇是\\R - C.jpeg
    ExMessage msg; //声明一个消息指针
    
    while (true) {
      
        setbkmode(TRANSPARENT);
        loadimage(&bjut, _T("R-C.jpeg"), 640, 480);
        putimage(0, 0, &bjut);
        BUTTON1();
        BUTTON2();
        BUTTON3();

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

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

                    fclose(fp11111);
              
                    RANK();
                    continue;

                } 
                if (msg.x >= 150 && msg.x <= 450 && msg.y >= 150 && msg.y < 250)
                {
                    choice();
                    continue;

                }break;

            //case WM_MOUSEMOVE:
                //if (msg.x >= 240 && msg.x <= 240 + 50 && msg.y >= 50 && msg.y <= 50 + 40)
               // {
                   
               //}break;
                    //tipInfo(tipInfoRect, title[0]);
                //if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 140 && msg.y <= 140 + 50)
                    //tipInfo(tipInfoRect, title[1]);
               // if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 230 && msg.y <= 230 + 50)
                    //tipInfo(tipInfoRect, title[2]);
               // if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 320 && msg.y <= 320 + 50)
                   // tipInfo(tipInfoRect, title[3]);
             
            default:break;
            }
        }
    }

   
    system("pause");

}
 

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-04-14 02:44:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-14 02:44:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-14 02:44:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-14 02:44:01       18 阅读

热门阅读

  1. 设计模式详解(十四)——策略模式

    2024-04-14 02:44:01       12 阅读
  2. 亚信安慧AntDB的多维度支持

    2024-04-14 02:44:01       11 阅读
  3. 斐波那契数列递归实现和for循环实现

    2024-04-14 02:44:01       10 阅读
  4. 46.全排列

    2024-04-14 02:44:01       12 阅读
  5. 负载均衡:高效分配网络资源的艺术

    2024-04-14 02:44:01       14 阅读
  6. C语言中的关键字

    2024-04-14 02:44:01       16 阅读
  7. 如何在Linux中查找名为`mysql_backup.sh`的文件

    2024-04-14 02:44:01       15 阅读
  8. 使用Python实现支持向量机算法

    2024-04-14 02:44:01       13 阅读