首页 > 解决方案 > 如何让我的蛇在 C++ 中运行或移动继续移动?

问题描述

我是 C++ 的初学者,我制作了我的第一个游戏,蛇游戏。我在没有任何图形库的情况下完成了它。

到目前为止一切都很好,我的蛇跑得很好,吃水果,分数也在增加。

目前我的蛇只是在按下键时运行,但现在我想连续运行我的蛇,只需用键改变它的方向,就像我们在旧蛇游戏中看到的那样。

到目前为止,我已经尝试了很多事情,比如循环等等,但这些事情并没有按照我想要的方式工作。

有我的代码-

#include<iostream>
#include<conio.h>
#include<Windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int n_Tail;
enum eDirection {STOP = 0, LEFT , RIGHT ,UP , DOWN};
eDirection dir;

void setup()
{
    gameOver = false;
    dir = STOP; 
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
}

void draw()
{
    system("cls");
    for(int i = 0 ;i < width+1; i++)
    {
        cout << "#"; //for Upper wall
    }
    cout << "\n";

    for (int i  = 0; i < height; i++)
    {
        for (int j = 0; j < width ; j++)
        {
            if (j==0)
            {
                cout <<"#";

            }
            if (i == y && j == x)
            {
                cout <<"0";
            }
             else if (i == fruitY && j == fruitX)
            {
                cout <<"f";
                width - 1;
            }
            else if (j== width -1)
            {
                cout << "#";            
            }
            else
            {
                bool print = false;
                for (int k = 0; k <n_Tail; k++)     
                {
                    if (tailX[k] == j && tailY[k] ==  i )
                    {
                        cout << "o";
                        print = true;
                    }
                }
                if (!print)
                {
                   cout <<" ";
                    
                }
            }
            
        }
        cout << "\n";
    }
                                    
    for (int i = 0; i < width+1; i++)
    {
        cout << "#"; //for lower wall 
    }
    cout <<"\n";
    cout << "Score = " << score;

}

void input()
{
    
        switch (_getch())
        {

            case 'a': dir = LEFT;
                break;

            case 'w': dir = UP;
                break;

            case 's': dir = DOWN;
                break;

            case 'd': dir = RIGHT;
                break;
        }
    

}

void logics()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;

    for (int i = 1; i < n_Tail; i++)
    {   

        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[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:
        y--;



    }
        
    for (int i = 0; i < n_Tail; i++)
    {
        if (tailX[i] == x && tailY[i] == y)
        {
            gameOver = true;
        }
    }
    //if (x> width||x<0||y>height||y<0)
    //{
    //gameOver = true;
    //}
    if (x > width-2)x = 0; else if (x < 0)x = width - 2;
    if (y > height-1)y = 0; else if (y < 0)y = height - 1;
    {

    }


    if (x == fruitX && y == fruitY)
    {
        score = score + 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        n_Tail++;

    }
}
int main()
{
    setup();
    while (!gameOver)
    {
        draw();
        input();
        logics();
        Sleep(10);

    }
}

边跑边玩

有人请帮助我,这样我就可以继续平静地学习 C++。

标签: c++c++11

解决方案


正如我确定您在使用调试器时注意到的那样。不?没有调试器? 这是你的必备读物!

您正在使用 _getch 读取输入。getch 被阻塞——这意味着它会等到你按下一个键。这不是你真正想要的。

这篇文章解释了如何制作一个非阻塞版本。


推荐阅读