C++实现贪吃蛇(3.0!)

150 阅读3分钟

上代码:

操作说明:↑8,↓2,←4,→6

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#define Map_long 20
#define Map_high 15
typedef struct snake{
    int goods_class;
    int direction;
}SNAKE;
SNAKE Snake[Map_high][Map_long] = { { 0 } };
unsigned die = 0;  
unsigned snake_move = 0;  
unsigned eat_food = 0;  
unsigned snake_long = 3;  
void Move(int ch);  
void HideCursor();  
int main(void)
{
    system("mode con:cols=43 lines=20"); 
    HideCursor();
    unsigned i, j;
    unsigned food = 0;  
    unsigned food_x, food_y;  
    unsigned score = 0;  
    char ch = '\0'; 
    srand((unsigned)time(NULL));
    for (i = 0; i < Map_long; i++)
    {
        Snake[0][i].goods_class = 1;
        Snake[Map_high - 1][i].goods_class = 1;
    }
    for (i = 0; i < Map_high; i++)
    {
        Snake[i][0].goods_class = 1;
        Snake[i][Map_long - 1].goods_class = 1;
    }
    for (i = 0; i < snake_long; i++)
        Snake[Map_high / 2][Map_long / 2 + i].goods_class = snake_long + i;

    while (1)
    {
        while (!_kbhit())
        {
            switch (ch)
            {
            case 72:Move(8); break;
            case 75:Move(4); break;
            case 77:Move(6); break;
            case 80:Move(2); break;
            default:break;
            }

            if (eat_food == 2)  
            {
                food = 0;  
                eat_food = 0;  
                snake_long++;  
                score++;  
            }
            do{
                if (food == 1)
                    break;
                food_x = rand() % Map_long;
                food_y = rand() % Map_high;
                if (Snake[food_y][food_x].goods_class == 0)
                {
                    Snake[food_y][food_x].goods_class = 2;
                    food++;
                    break;
                }
            } while (1);

            printf("Grade:%u\n", score);
            for (i = 0; i < Map_high; i++)
            {
                if (die == 1)
                    break;

                for (j = 0; j < Map_long; j++)
                {
                    switch (Snake[i][j].goods_class)
                    {
                    case 0:printf("  "); break;
                    case 1:printf("■"); break;
                    case 2:printf("★"); break;
                    case 3:printf("●"); break;
                    default:printf("◎"); break;
                    }
                }
                putchar('\n');
            }
            printf("Up↑ Down↓ Left← Right→ Other Pause.");

            Sleep(50);
            system("cls");

            if (die == 1)
                break;
        }
        if (die == 1)
            break;

        do{
            ch = _getch();
        } while (ch != -32 && ch != 80 && ch != 72 && ch != 77 && ch != 75);
        ch = _getch();
    }
    system("cls");
    printf("Game Over,your grade is %u.\n", score);
    getchar();
    return 0;
}

void Move(int ch)
{
    int i, j, n;
    int direction;  

    for (n = 0; n < snake_long; n++)
    for (i = 0; i < Map_high; i++)
    {
        if (snake_move == 1)
        {
            snake_move = 0;
            break;
        }
        if (eat_food == 2)
            break;
        for (j = 0; j < Map_long; j++)
        if (Snake[i][j].goods_class == 3 + n)
        {
            if (Snake[i][j].goods_class == 3 && Snake[i][j].direction == 0)
            {
                Snake[i][j].direction = ch;
                Snake[i][j + 1].direction = 4;
                Snake[i][j + 2].direction = 4;
            }
            else if (Snake[i][j].goods_class == 3)  
                Snake[i][j].direction = ch;
            if (Snake[i][j].goods_class == 3)
            {
                if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class != 0 && Snake[i - 1][j].goods_class != 2)
                    die++;

                else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class != 0 && Snake[i + 1][j].goods_class != 2)
                    die++;

                else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class != 0 && Snake[i][j - 1].goods_class != 2)
                    die++;

                else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class != 0 && Snake[i][j + 1].goods_class != 2)
                    die++;
            }
            if (Snake[i][j].goods_class == 3)
            {
                if (Snake[i][j].direction == 8 && Snake[i - 1][j].goods_class == 2)
                    eat_food++;

                else if (Snake[i][j].direction == 2 && Snake[i + 1][j].goods_class == 2)
                    eat_food++;

                else if (Snake[i][j].direction == 4 && Snake[i][j - 1].goods_class == 2)
                    eat_food++;

                else if (Snake[i][j].direction == 6 && Snake[i][j + 1].goods_class == 2)
                    eat_food++;
            }

            if (die == 1)
                break;

            if (Snake[i][j].goods_class == 3 + snake_long - 1)
            {
                if (eat_food == 1)  
                {
                    if (Snake[i][j].direction == 8)
                    {
                        direction = Snake[i - 1][j].direction;
                        Snake[i - 1][j] = Snake[i][j];
                        Snake[i - 1][j].direction = direction;
                        Snake[i][j].goods_class = 3 + snake_long;
                    }
                    else if (Snake[i][j].direction == 2)
                    {
                        direction = Snake[i + 1][j].direction;
                        Snake[i + 1][j] = Snake[i][j];
                        Snake[i + 1][j].direction = direction;
                        Snake[i][j].goods_class = 3 + snake_long;
                    }
                    else if (Snake[i][j].direction == 4)
                    {
                        direction = Snake[i][j - 1].direction;
                        Snake[i][j - 1] = Snake[i][j];
                        Snake[i][j - 1].direction = direction;
                        Snake[i][j].goods_class = 3 + snake_long;
                    }
                    else if (Snake[i][j].direction == 6)
                    {
                        direction = Snake[i][j + 1].direction;
                        Snake[i][j + 1] = Snake[i][j];
                        Snake[i][j + 1].direction = direction;
                        Snake[i][j].goods_class = 3 + snake_long;
                    }
                    snake_move++;
                    eat_food++;
                    break;
                }
                else{
                    if (Snake[i][j].direction == 8)
                    {
                        Snake[i][j].direction = Snake[i - 1][j].direction;
                        Snake[i - 1][j] = Snake[i][j];
                        Snake[i][j].goods_class = 0;
                    }
                    else if (Snake[i][j].direction == 2)
                    {
                        Snake[i][j].direction = Snake[i + 1][j].direction;
                        Snake[i + 1][j] = Snake[i][j];
                        Snake[i][j].goods_class = 0;
                    }
                    else if (Snake[i][j].direction == 4)
                    {
                        Snake[i][j].direction = Snake[i][j - 1].direction;
                        Snake[i][j - 1] = Snake[i][j];
                        Snake[i][j].goods_class = 0;
                    }
                    else if (Snake[i][j].direction == 6)
                    {
                        Snake[i][j].direction = Snake[i][j + 1].direction;
                        Snake[i][j + 1] = Snake[i][j];
                        Snake[i][j].goods_class = 0;
                    }
                    snake_move++;
                    break;
                }
            }

            /*蛇头的移动*/
            if (Snake[i][j].goods_class == 3)
            {
                if (Snake[i][j].direction == 8)
                    Snake[i - 1][j] = Snake[i][j];

                else if (Snake[i][j].direction == 2)
                    Snake[i + 1][j] = Snake[i][j];

                else if (Snake[i][j].direction == 4)
                    Snake[i][j - 1] = Snake[i][j];

                else if (Snake[i][j].direction == 6)
                    Snake[i][j + 1] = Snake[i][j];
                snake_move++;
                break;
            }

            /*蛇身的移动*/
            if (Snake[i][j].direction == 8)
            {
                direction = Snake[i - 1][j].direction;
                Snake[i - 1][j] = Snake[i][j];
                Snake[i - 1][j].direction = direction;
            }
            else if (Snake[i][j].direction == 2)
            {
                direction = Snake[i + 1][j].direction;
                Snake[i + 1][j] = Snake[i][j];
                Snake[i + 1][j].direction = direction;
            }
            else if (Snake[i][j].direction == 4)
            {
                direction = Snake[i][j - 1].direction;
                Snake[i][j - 1] = Snake[i][j];
                Snake[i][j - 1].direction = direction;
            }
            else if (Snake[i][j].direction == 6)
            {
                direction = Snake[i][j + 1].direction;
                Snake[i][j + 1] = Snake[i][j];
                Snake[i][j + 1].direction = direction;
            }
            snake_move++;
            break;
        }
    }
}

void HideCursor()
{
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}