所以我试图制作一个受youtube教程启发的游戏,但有点复杂,我在设置功能中创建了蛇,但由于无法从其他功能中获取其成员而将其移开了是全球性的,我根本无法接受,我该如何解决? 在stuff.h标头中,我有colio.h和iostream
bool gameOver;
std::array<std::array<int, 20>, 20> grid;
snake jerry(15, 9);
class object
{
protected:
int m_x, m_y;
object(int y, int x) :m_x(x), m_y(y) {};
public:
const int getX() { return m_x; };
const int getY() { return m_y; };
};
class snake:public object
{
private:
enum direction
{
STOP,
LEFT,
RIGHT,
UP,
DOWN,
};
direction dir;
public:
snake(int y, int x) :object(y, x) {};
const void moveSnake(char c)
{
if (c == 'a') { dir = LEFT; };
if (c == 'd') { dir = RIGHT; };
if (c == 's') { dir = DOWN; };
if (c == 'w') { dir = UP; }
};
const void changeCoords(int y,int x) { m_x = x, m_y = y; };
};
class fruit :public object
{
private:
public:
fruit(int y, int x) :object(y,x) {};
};
void setup()
{
for (int j = 0; j < 20; ++j)
{
for (int i = 0; i < 20; ++i)
{
if (j == 0 || j == 19 || i == 0 || i == 19) // 0=space , 1=# , 2=snake, 3= fruit , 4= snake tail
{
grid[j][i] = 1;
}
else
{
grid[j][i] = 0;
}
}
}
grid[jerry.getY()][jerry.getX]=2;
fruit fr(rand() % 20, rand() % 20);
grid[fr.getY()][fr.getX()] = 3;
}
void draw()
{
for (int j = 0; j < 20; ++j)
{
for (int i = 0; i < 20; ++i)
{
if (grid[j][i]==1)
{
std::cout << "#";
}
else if (grid[j][i]==0)
{
std::cout << " ";
}
else if (grid[j][i] == 2)
{
std::cout << "O";
}
else if (grid[j][i] == 3)
{
std::cout << "F";
}
}
std::cout << std::endl;
}
system("cls");
}
void input()
{
if (_kbhit())
{
jerry.moveSnake(_getch);
}
}
void logic()
{
}
int main()
{
setup();
while (gameOver==false)
{
draw();
input();
logic();
}
return 0;
}