贪吃蛇[c++]

在这里插入图片描述

#include <iomanip>
#include <cstdlib>
#include <conio.h>
#include <ctime>
#include <windows.h>
#include <iostream>
#include <cstdio>

using namespace std;

const int N(27);
const int MaxLen(103);

//颜色函数
void color(int a)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}

//位置函数
void gotoxy(int x, int y)
{
	COORD pos;
	pos.X=2 * x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

//隐藏光标
void hide()
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	GetConsoleCursorInfo(hOut,&cci);
	cci.bVisible=false;
	SetConsoleCursorInfo(hOut,&cci);
}

struct snake
{
    int x;
    int y;
};

class Game
{
public:
    Game(); // 初始化len = 3,随机生成食物,生成围墙,grow = false(用来判断是否要删除蛇的尾巴)
    //~Game();
    void Move(char);
    void NewFood();
    int Getlen() const {return len;}
private:
    int** Wall;
    snake snake_arr[MaxLen];
    int len;
    bool grow;
};

int main()
{
    hide();
    cout << "Press Enter To Start...";
    getchar();
    system("cls");
    srand(time(0));
    char key('d'), ch;
    Game gg;
    while (1)
	{
		Sleep(200 - (gg.Getlen() * 1.5));
		if (kbhit())
			ch=getch();
		if (ch == 'w' && key != 's')
			key = 'w';
		if (ch == 's' && key != 'w')
			key = 's';
		if (ch == 'a' && key != 'd')
			key = 'a';
		if (ch == 'd' && key != 'a')
			key = 'd';
		gg.Move(key);
	}
    gotoxy(0, N);
    return 0;
}

Game::Game()
{
    grow = false;
    len = 3;
    // 动态二维数组
    Wall = new int*[N];
    for (int i = 0; i < N; ++i)
        Wall[i] = new int[N];
    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < N; ++j)
        {
            if (i == 0 || i == N - 1 || j == 0 || j == N - 1)
                Wall[i][j] = 1, cout << setw(2) << '#';
            else
                Wall[i][j] = 0, cout << setw(2) << ' ';
        }
        cout << endl;
    }
    snake_arr[0].x = N / 2;
    snake_arr[0].y = N / 2;
    snake_arr[1].x = N / 2 - 1;
    snake_arr[1].y = N / 2;
    snake_arr[2].x = N / 2 - 2;
    snake_arr[2].y = N / 2;
    for (int i = 0; i < 3; ++i)
    {
        Wall[snake_arr[i].x][snake_arr[i].y] = 1;
        gotoxy(snake_arr[i].x, snake_arr[i].y);
        cout << setw(2) << '*';
    }
    NewFood();
}

void Game::NewFood()
{
    int food_x = rand() % (N - 2) + 1;
    int food_y = rand() % (N - 2) + 1;
    for (int i = 0; i < len; ++i)
    {
        if (food_x == snake_arr[i].x && food_y == snake_arr[i].y)
        {
            i = 0;
            food_x = rand() % (N - 2) + 1;
            food_y = rand() % (N - 2) + 1;
        }
    }
    gotoxy(food_x, food_y);
    color(4);
	cout << setw(2) << '@';
	color(7);
	Wall[food_x][food_y] = 2;
}

void Game::Move(char key)
{
    if (!grow) // 如果蛇没变长就删除尾巴,达到移动的效果
    {
        Wall[snake_arr[len - 1].x][snake_arr[len - 1].y] = 0;
        gotoxy(snake_arr[len - 1].x, snake_arr[len - 1].y);
        cout << setw(2) << ' ';
    }
    if (grow)
        grow = false;
    for (int i = len - 1; i > 0; --i)
        snake_arr[i] = snake_arr[i - 1];
    if (key == 'w')
        snake_arr[0].y--;
    else if (key == 'a')
        snake_arr[0].x--;
    else if (key == 's')
        snake_arr[0].y++;
    else
        snake_arr[0].x++;
    if (Wall[snake_arr[0].x][snake_arr[0].y] == 2)
    {
        len++;
        NewFood();
        grow = true;
    }
    if (Wall[snake_arr[0].x][snake_arr[0].y] == 1)
    {
        system("cls");
        cout << "Game Over!";
        exit(0);
    }
    Wall[snake_arr[0].x][snake_arr[0].y] = 1;
    gotoxy(snake_arr[0].x, snake_arr[0].y);
    cout << setw(2) << '*';
}


猜你喜欢

转载自blog.csdn.net/weixin_43231262/article/details/88544175