简单的snake小游戏

跟着视频做了一个贪吃蛇的项目,主要是了解逻辑,发现还差得远呢,继续努力

# include<iostream>
# include<ctime>
# include<Windows.h>
# include<conio.h>
/*conio.h不是C标准库中的头文件,在C standard library,ISO C 和POSIX标准中均没有定义。
conio是Console Input / Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输
入和数据输出的函数,主要是一些用户通过按键盘产生的对应操作,比如getch()函数等等。*/

using namespace std;


const int width = 28;
const int height = 14;
int x, y, fruitx, fruity, score;
bool gameOver;
enum eDirection{STOP=0,LEFT,RIGHT,UP,DOWN};
eDirection dir;
int xTail[50], yTail[50];//蛇尾部(除去头部)
int nTail = 0;

void Setup();
void Draw();
void Input();
void Logic();

int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(50);
    }
    cout << endl;
    system("pause");
    return 0;
}

void Setup()
{
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    srand(time(0));
    fruitx = rand() % width;
    fruity = rand() % height;
    score = 0;
}

void Draw()
{
    system("cls");//clear the screen
    for (int i = 0;i < width+2 ;i++)//宽为width+2
        cout << "#";
    cout << endl;

    for (int i = 0;i < height;i++)//出除去首尾两列
    {
        for (int j = 0;j < width;j++)
        {
            //#######
            //#     #
            //#     #
            //#######
            if (j == 0)
                cout << "#";

            if (i == y&&j == x)
                cout << "O";
            else if (i == fruity&&j == fruitx)
                cout << "F";

            else
            {
                bool printBlank = true;
                for (int k = 0;k < nTail;k++)
                {
                    if (xTail[k] == j&&yTail[k] == i)
                    {
                        printBlank = false;
                        cout << "o";
                    }
                }
                if (printBlank)
                    cout << " ";
            }

            if (j == width - 1)
                cout <<  "#";

        }
        cout << endl;
    }
    for (int i = 0;i < width+2 ;i++)
        cout << "#";
    cout << endl << "Score:" << score;
    cout << endl << "食物坐标:" << "(" << fruitx << "," << fruity << ")" << endl;
}

void Input()
{
    if (_kbhit())
    {
        /*
        w
      a   d
        s
        */
        switch (_getche())
        {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
            break;
        case 'x':
            gameOver = true;
            break;
        default:
            break;
        }
    }
}


void Logic()
{
    int prevX = xTail[0];
    int prevY = yTail[0];
    int prev2X, prev2Y;
    xTail[0] = x;
    yTail[0] = y;
    for (int i = 1;i < nTail;i++)
    {
        prev2X = xTail[i];
        prev2Y = yTail[i];
        xTail[i] = prevX;
        yTail[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }

    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }
    if (x <0 || x > width || y <0 || y >height)
        gameOver = true;
    if (fruitx == x&&fruity == y)
    {
        nTail++;

        score += 10;
        fruitx = rand() % width;
        fruity = rand() % height;
    }

}

这里写图片描述
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值