[英]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:开始:
\n
bGameW 没有\n
\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 的数字(并且您会),坏事将会发生(并且可能已经发生)。 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.