我对c ++有基本的了解,并且正在尝试创建一个可在84 x 48像素的LCD屏幕上运行的简单Snake游戏。游戏运行正常,但有一件事我已经坚持了一段时间,而且我以前的帖子中找不到任何帮助:
到目前为止,蛇的运动是自由的,即它逐个像素地移动,这意味着,蛇由3 x 3像素的正方形组成,大多数时候它不会自动与水果对齐。我想实现一个"网格系统",所以将我的84 x 48游戏空间变成28 x 16网格,其中蛇移动并吃掉水果而玩家不必多次调整其轨迹与果实正确对齐。
这是我的代码中我将蛇打印到屏幕上的部分:
void Snake::head_init(int size)
{
_size = size;
_x = (GRIDX/2 - _size/2)*_size;
_y = (GRIDY/2 - _size/2)*_size;
printf("Snake initially at x = %i, y = %i\n", _x,_y);
}
void Snake::draw(N5110 &lcd)
{
lcd.drawRect(_x,_y,_size,_size, FILL_BLACK); // draw head
for(int k = 0; k < _tail_length; k++) {
lcd.drawRect(_tail_x[k],_tail_y[k],_size,_size, FILL_BLACK); // print tail
}
}
GRIDX = 28,GRIDY = 16,_size = 3
我试图在打印时将_x和_y乘以蛇的大小,但这并不能给我我想要的结果。
另外,我希望水果能够在相同的28 x 16网格中产卵。 以下是我现在的水果印刷方式:
void Fruit::init(int size)
{
_size = size;
srand(time(NULL)); // initialise random number generator for fruit positions
_x = rand() % 80 + 1; // generate a random x coordinate for fruit within the frame x range
_y = rand() % 43 + 1; // generate a random y coordinate for fruit within the frame y range
printf("Fruit initially at x = %i, y = %i \n", _x, _y); // print the random fruit coordinates over terminal
}
void Fruit::draw(N5110 &lcd)
{
lcd.drawSprite(_x,_y,3,3,(int *)fruit); // draw fruit at random position
}
非常感谢任何帮助