C ++ Snake one-dimensional

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>

int g_Dir = 3;
#define UP         0
#define DOWN  1
#define LEFT       2
#define RIGHT   3

struct FOOD
{
    int X = 0;
    int Y = 0;
    bool State = 0;
}Food;

// directional control 
void SnekeMove ()
{
    if (::GetAsyncKeyState(VK_UP) & 1)
        g_Dir = 0;
    if (::GetAsyncKeyState(VK_DOWN) & 1)
        g_Dir = 1;
    if (::GetAsyncKeyState(VK_LEFT) & 1)
        g_Dir = 2;
    if (::GetAsyncKeyState(VK_RIGHT) & 1)
        g_Dir = 3 ;
}

// main function 
int main ()
{
    srand((unsigned int)time(NULL));
    int W = 20;
    int H = 20;
    int Len = 3;
    int Map[20 * 20] = { 0 };
    int Snake[50] = { 0 };
    //Snake[0] = 2;//为蛇头
    
    for (int i = 0; i < Len; i++)
    {
        Snake[i] = Len - i - 1;
    }
    Food.X = W / 2;
    Food.Y = H / 2;
    Map[Food.Y * W + Food.X] = 2;

    const char *SNAKE[] = { "  ", "" ,""};
    while (true)
    {
        system("cls");
        for (int i = 0; i < Len; i++)
        {
            // 2 1 0
            // 1 1 1

            // 321
             // 0111
             // It should be noted that 
            the Map [Snake [I]] = . 1 ;
        }
        Map[Food.Y * W + Food.X] = 2;
        if (Map[Snake[0]] == Map[Food.Y * W + Food.X])
        {
            Map[Food.Y * W + Food.X] = 0;
            do
            {
                bool FoodState = false;
                Food.X = rand() % W;
                Food.Y = rand() % H;
                for (int i = 0; i < Len; i++)
                {
                    if (Map[Food.Y * W + Food.X] != Map[Snake[i]])
                    {
                        FoodState = true;
                    }
                }
                if (FoodState)
                {
                    Map[Food.Y * W + Food.X] = 2;
                    break;
                }
            } while (true);
            Only ++ ;
        }

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                // snake head coordinates on the map 
                IF (I + J == * W is Snake [ 0 ])
                {
                    if (i == H - 1 || j == W - 1)
                    {
                        // wall 
                        System ( " PAUSE " );
                         return  0 ;
                    }
                }
            }
        }

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                std::cout << SNAKE[Map[i * W + j]];
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
        //for (int i = 0; i < Len; i++)
        //{
        //    std::cout << Snake[i]<<"  ";
        //}
        //std::cout << std::endl;
        std::cout <<"蛇的长度为 : "<<Len<<std::endl;
        memset(Map, 0, sizeof(Map));

        SnekeMove ();


        for (int i = Len; i > 0; i--)
        {
            // 210
             // Default right move
             @ 2210
             @ behind +
             // 3210
             // move completely once 
            Snake [I] = Snake [I - . 1 ];
             / *
            For example down
            // 3 2 1 0
            // 23 3 2 1 0
            */
        }
        if (g_Dir == UP)
        {
            Snake[0] -= W;
        }
        if (g_Dir == DOWN)
        {
            Snake[0] += W;
        }
        if (g_Dir == LEFT)
        {
            Snake[0]--;
        }
        if (g_Dir == RIGHT)
        {
            Snake[0]++;
        }
        


        Sleep(50);
    }


    //https://www.bilibili.com/video/av29007126/?spm_id_from=333.788.videocard.1
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/YZFHKMS-X/p/11780544.html