简体   繁体   English

如何为 C++ 2D Snake 游戏生成随机水果

[英]How to generate random fruit for C++ 2D Snake game

I am having trouble generating random fruit for my Snake game.我无法为我的 Snake 游戏生成随机水果。 (I am very new to programming and this is my first language). (我对编程很陌生,这是我的第一语言)。

When I run my code all works fine so far (except from some minor issues).当我运行我的代码时,到目前为止一切正常(除了一些小问题)。 I'm using Visual Studio C++ in an empty project.我在一个空项目中使用 Visual Studio C++。 Here is my full code (I'm not displaying my #includes ):这是我的完整代码(我没有显示我的#includes ):

using namespace std;
bool gameOver = false;
int gameScore;
int fruitX;
int fruitY;

string bGameW = "###########";
string bGameL = "#         #\n";

class gameStart
{
public:
void start() 
{   

    cout << bGameW;
    cout << bGameL;
    cout << bGameL;
    cout << bGameL;
    cout << bGameL;
    cout << bGameL;
    cout << bGameL;
    cout << bGameL;
    cout << bGameL;
    cout << bGameW; 

}

void generateFruit() 
{
    srand(time(NULL));
    fruitX = rand() % 21;
    fruitY = rand() % 21;

    bGameW.insert(fruitX, "F");
    bGameL.insert(fruitY, "F");

}

void clearscreen() 
{
    system("cls");
}
private:
};


 int main () 
 {  
 gameStart gameObj;

 gameObj.generateFruit();
 gameObj.clearscreen();
 gameObj.start();

 return 0;
 }

To generate the random the random fruit for the string.为字符串生成随机的随机水果。 I use a string to make the game board, then I create random values for the fruit (X and Y) then I append them into my game board.我使用字符串制作游戏板,然后为水果(X 和 Y)创建随机值,然后将 append 放入我的游戏板。

But the issue is: I want to make only one fruit with a random X and Y and append it into my game board to display it.但问题是:我只想用随机的 X 和 Y 和 append 制作一个水果到我的游戏板中显示它。 But my current code is this:但我目前的代码是这样的:

bGameW.insert(fruitX, "F");
bGameL.insert(fruitY, "F");

This code makes 2 fruits with 1 at a random X and 1 at a random Y. I want to turn these 2 fruits into 1 fruit, with 1 random X and 1 random Y.此代码生成 2 个水果,其中 1 个随机 X 和 1 个随机 Y。我想将这 2 个水果变成 1 个水果,1 个随机 X 和 1 个随机 Y。

There's a whole host of things worth commenting on.有很多值得评论的事情。 Here goes:开始:

  1. bGameW has no \n bGameW 没有\n
  2. bGameW and bGameL are 10 and 11 characters long (both with be 11 after you add the other \n ). bGameW 和 bGameL 的长度分别为 10 和 11 个字符(在添加另一个\n后两者都是 11)。 Your RNG is generating numbers between 0 and 20... if you ever generate a number > 11 (and you will), Bad Things will happen (and probably have).您的 RNG 正在生成 0 到 20 之间的数字……如果您曾经生成大于 11 的数字(并且您会),坏事将会发生(并且可能已经发生)。
  3. Snake games like this let you eat multiple fruit, which is why people brought up the whole "don't call srand more than once" thing, as you'll be calling generate fruit every time someone eats the old one.像这样的贪吃蛇游戏可以让你吃多个水果,这就是为什么人们提出了“不要多次调用 srand”这件事,因为每次有人吃旧水果时你都会调用生成水果。 OTOH, that mostly removes the "calling srand multiple times per second can return the same value" problem too. OTOH,这也主要消除了“每秒多次调用 srand 可以返回相同的值”的问题。
  4. Rather than writing out those two lines, bGameW and bGameL, I recommend that you build a character array that holds your entire game display (NOT your game state, just the display of that state).与其写出这两行 bGameW 和 bGameL,我建议您构建一个包含整个游戏显示的字符数组(不是您的游戏 state,只是该状态的显示)。 You then clear it and redraw it every move.然后你清除它并重新绘制每一步。 You'll need a game area, walls, something that tracks where your snake is, and the current fruit.你需要一个游戏区、墙壁、追踪你的蛇在哪里的东西,以及当前的水果。 You then 'render' all these things into your character-array-game-display.然后,您将所有这些东西“渲染”到您的角色阵列游戏显示中。 Don't forget to clear it every time you redraw it or you'll get all kinds of problems.每次重画时不要忘记清除它,否则会遇到各种问题。
  5. Rather than redrawing everything, you could use something like the "curses" library to clear and redraw specific character locations on the screen.您可以使用诸如“curses”库之类的东西来清除和重绘屏幕上的特定角色位置,而不是重绘所有内容。 You'd then face problems 'clearing' and redrawing different spots.然后,您将面临“清除”和重新绘制不同点的问题。

As far as programming style goes, you will find that so called "magic numbers" (like 21 in your case) can lead to bugs.就编程风格而言,您会发现所谓的“幻数”(如您的案例中的 21)会导致错误。 What people generally do instead is to define a const with the appropriate value, and then define things in terms of that const.人们通常做的是定义一个具有适当值的const ,然后根据该 const 定义事物。

// constants are often named in ALL_UPPER_CASE_WITH_UNDERSCORES
// to help you recognize them.
const int PLAY_AREA_HEIGHT = 10;
const int PLAY_AREA_WIDTH = 10;
const char EMPTY_PLAY_SQUARE = '.';
// have you learned about 2d arrays yet?  Arrays at all?  
char playAreaDisplay[PLAY_AREA_HEIGHT][PLAY_AREA_WIDTH];

void wipePlayArea()
{
  for (int heightIdx = 0; heightIdx < PLAY_AREA_HEIGHT; ++heightIdx)
  {
    for (int widthIdx = 0; widthIdx < PLAY_AREA_WIDTH; ++widthIdx)
    {
      playAreaDisplay[heightIdx][widthIdx] = EMPTY_PLAY_SQUARE;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM