ccccccccccccc

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include<iostream>
#include<string.h>
#include <mmsystem.h>//导入声音头文件
#pragma comment(lib,"Winmm.lib")
void BGM()
{
    PlaySound(TEXT("平凡之路.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
     
    
}

#define MAP_HEIGHT 30
#define MAP_WIDE 30
#define SIZE 16
#define SPEED 200

//定义蛇的结点
typedef struct Snakes
{
    int x;//蛇的结点的位置坐标
    int y;
    struct Snakes* next;
}snake;

snake* head, * tail;
//定义食物的结构
struct Food
{
    int x;
    int y;
}food;

int grow = 0; //grow =0 表示没吃到食物,grow = 1表示吃到食物
TCHAR str[100];
TCHAR str1[100];
TCHAR str2[100];
int score = 0;
int count=1;
char ch = 'a';//初始方向向左
void login()                                                  //登录界面//
{
    initgraph(1200, 900);
    setbkcolor(BLUE);
    cleardevice();
    settextcolor(WHITE);
    setfont(100, 0, _T("华文行楷"));
    outtextxy(0.32 * 30, 150, _T("WELCOME"));
    wchar_t username[10];
    errno_t err;
    FILE* fp;
    InputBox(username, 100, _T("请输入你的昵称"));
    err = fopen_s(&fp, "username.txt", "a");
    fprintf(fp, "%s\t", username);
    fclose(fp);
}

void DrawMap()//打印地图
{
    setbkcolor(YELLOW);
    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);

    }
    settextcolor(GREEN);
    _stprintf_s(str1, _T("%d"), score);

    outtextxy(500, 20, str1);
}
//打印结点
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(YELLOW);
    fillcircle(x * SIZE, y * SIZE, SIZE / 2);
    setcolor(YELLOW);
    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);
}
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; //生成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, 20, str1);
            //outtextxy(400,20,L"123");
            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();//新的方向
    }
    //控制方向:向左的时候不能向右转,向上的时候不能向下,向右不能向左,向下不能向上
    if ((ch == 'd' || ch == 'D') && (oldch == 'a' || oldch == 'A'))
        //保持原来的方向不变
        ch = oldch;
    if ((oldch == 'w' || oldch == 'W') && (ch == 'S' || ch == 's'))
        ch = oldch;
    if ((oldch == 'd' || oldch == 'D') && (ch == 'A' || ch == 'a'))
        ch = oldch;
    if ((oldch == 'S' || oldch == 's' && ch == 'w' || ch == 'W'))
        ch = oldch;
    if (ch != 's' && ch != 'S' && ch != 'W' && ch != 'w' && ch != 'a' && ch != 'A' && ch != 'd' && ch != 'D')
        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 '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;
}

int main()
{
    BGM();
    //login();
    char a[1000];
    int i, j,num=0,max=0;
    initgraph(640, 480);
    setbkcolor(WHITE);
    cleardevice();
    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);
    ExMessage msg; //声明一个消息指针
    while (true) {
        if (peekmessage(&msg, EM_MOUSE)) {
            switch (msg.message)
            {
            case WM_LBUTTONDOWN:
                if (msg.x >= 100 && msg.x <= 400 && msg.y >=100&& msg.y <200)
                {
                    
                    //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();
                        UpdataFood();
                    }
                    //int num = 999;
                    FILE* fa = fopen("排名.txt", "a");
                    fprintf(fa, "%d", score);
                    fprintf(fa, "%c", '\n');
                    fclose(fa);
                    count++;
                    //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[i] - '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();
                } 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;
            }
        }
    }

   /* while (1);
   
    initgraph(640, 480);
    DrawMap();
    InitMap();
    while (Finish())
    {
        MoveSnake();
        UpdataFood();
    }
    //printf("游戏结束!\n您的得分为:%d", score * 10);
    setbkcolor(BLACK);
    cleardevice();
   
    _stprintf_s(str, _T("%d"), score);
    
    outtextxy(100, 200,str);
    _getch();*/
    system("pause");

}
 

  • 21
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值