c++:OOP:如何在单独的 header 文件中更新数组?
c++ : OOP : How do you update an array in a separate header file?
我是 c++ 的新手,正在尝试创建一个基本的迷宫游戏。目前,我正在尝试让玩家能够在迷宫中移动,其中玩家的移动功能会更新网格数组中的角色。网格数组在 Grid.h 中,播放器在 Player.h 中,我在每个文件中都包含了 headers,但它们仍然无法访问彼此的成员变量。我尝试使用 'extern',但也没有用。
我的老师建议将 m_grid 数组发送给构造函数,但没有指定哪个构造函数或在任何情况下如何执行。任何帮助将不胜感激,谢谢。
Grid.h
#include <iostream>
#include "Player.h"
class Grid
{
public:
Grid();
~Grid();
void Print_grid();
char m_grid[18][32] =
{
{"_______________________________"},
{"| _______ ____________ |"},
{" @ |"},
{"|_ _____________ __________|"},
{"| | |"},
{"| | |__ | | | | |"},
{"| | | | |___ | | | |"},
{"| | _____| | | ___| | |"},
{"| | | | | |"},
{"| |_________ _____ |"},
{"| _______ __|"},
{"|_______________ |"},
{"|_____ _______________| __|"},
{"| | _______ |"},
{"| | ____________ ________|"},
{"| | # |"},
{"| | | __|________ |"},
{"|______|______________________|"}
};
};
Player.h
#pragma once
#include "Grid.h"
#include "Enemy.h"
extern char y;
extern char x;
extern char m_grid[18][32];
class Player
{
public:
Player();
~Player();
static const char avatar = '@';
void Set_difficulty();
void Move(int speed);
int m_speed{ 0 };
struct Vector2
{
int y{ 0 };
int x{ 1 };
int new_y{ 0 };
int new_x{ 0 };
};
Vector2* playerPos = nullptr;
int m_score{ 0 };
int m_highscore{ 0 };
void Print_score();
private:
char m_difficulty = ' ';
};
Player.cpp
#include <iostream>
#include "Player.h"
Player::Player()
{
m_grid[playerPos.y][playerPos.x] = avatar;
}
Player::~Player()
{
}
void Player::Set_difficulty()
{
std::cout << "Pick your difficulty:\nH -> Hard\nN -> Normal\nE -> Easy\n\nInput : ";
std::cin >> m_difficulty;
if (m_difficulty == 'e' || m_difficulty == 'E')
{
m_speed = 3;
}
else if (m_difficulty == 'n' || m_difficulty == 'N')
{
m_speed = 2;
}
else
{
m_speed = 1;
}
}
void Player::Move(int speed)
{
if (GetAsyncKeyState(VK_DOWN))
{
if (m_grid[playerPos->y+1][playerPos->x] == ' ' || m_grid[playerPos->y + 1][playerPos->x] == '*') //if desired space is empty or has a coin
{
playerPos->new_y = playerPos->y + speed; //new player pos = current pos + speed
m_grid[playerPos->y][playerPos->x] == ' '; //old space is now empty
}
}
if (GetAsyncKeyState(VK_UP))
{
if (m_grid[playerPos->y-1][playerPos->x] == ' ' || m_grid[playerPos->y-1][playerPos->x] == '*')
{
playerPos->new_y = playerPos->y - speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
if (GetAsyncKeyState(VK_RIGHT))
{
if (m_grid[playerPos->y][playerPos->x+1]== ' ' || m_grid[playerPos->y][playerPos->x+1] == '*')
{
playerPos->new_x = playerPos->x + speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
if (GetAsyncKeyState(VK_LEFT))
{
if (m_grid[playerPos->y][playerPos->x-1] == ' ' || m_grid[playerPos->y][playerPos->x-1] == '*')
{
playerPos->new_x = playerPos->x - speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
}
void Player::Print_score()
{
}
首先:grid.h和player.h相互包含。应该避免这种情况,在这种情况下,grid.h 不需要 player.h.
实际问题:您的播放器没有可以使用的Grid对象。理想情况下,Grid 和 Player 应该是 Game class 的成员,并且对 Grid 对象的引用应该传递给 Player。所以它可以使用 Grid::m_grid。就像现在一样,您使用另一个仅声明为外部的 m_grid,因此没有真正的实例化。如果没有 Game class,这可以在 main() 中完成一个简单的练习,但是您需要声明一个 Grid 对象来访问它,如果玩家想要基于网格移动,则需要引用它。
我是 c++ 的新手,正在尝试创建一个基本的迷宫游戏。目前,我正在尝试让玩家能够在迷宫中移动,其中玩家的移动功能会更新网格数组中的角色。网格数组在 Grid.h 中,播放器在 Player.h 中,我在每个文件中都包含了 headers,但它们仍然无法访问彼此的成员变量。我尝试使用 'extern',但也没有用。
我的老师建议将 m_grid 数组发送给构造函数,但没有指定哪个构造函数或在任何情况下如何执行。任何帮助将不胜感激,谢谢。
Grid.h
#include <iostream>
#include "Player.h"
class Grid
{
public:
Grid();
~Grid();
void Print_grid();
char m_grid[18][32] =
{
{"_______________________________"},
{"| _______ ____________ |"},
{" @ |"},
{"|_ _____________ __________|"},
{"| | |"},
{"| | |__ | | | | |"},
{"| | | | |___ | | | |"},
{"| | _____| | | ___| | |"},
{"| | | | | |"},
{"| |_________ _____ |"},
{"| _______ __|"},
{"|_______________ |"},
{"|_____ _______________| __|"},
{"| | _______ |"},
{"| | ____________ ________|"},
{"| | # |"},
{"| | | __|________ |"},
{"|______|______________________|"}
};
};
Player.h
#pragma once
#include "Grid.h"
#include "Enemy.h"
extern char y;
extern char x;
extern char m_grid[18][32];
class Player
{
public:
Player();
~Player();
static const char avatar = '@';
void Set_difficulty();
void Move(int speed);
int m_speed{ 0 };
struct Vector2
{
int y{ 0 };
int x{ 1 };
int new_y{ 0 };
int new_x{ 0 };
};
Vector2* playerPos = nullptr;
int m_score{ 0 };
int m_highscore{ 0 };
void Print_score();
private:
char m_difficulty = ' ';
};
Player.cpp
#include <iostream>
#include "Player.h"
Player::Player()
{
m_grid[playerPos.y][playerPos.x] = avatar;
}
Player::~Player()
{
}
void Player::Set_difficulty()
{
std::cout << "Pick your difficulty:\nH -> Hard\nN -> Normal\nE -> Easy\n\nInput : ";
std::cin >> m_difficulty;
if (m_difficulty == 'e' || m_difficulty == 'E')
{
m_speed = 3;
}
else if (m_difficulty == 'n' || m_difficulty == 'N')
{
m_speed = 2;
}
else
{
m_speed = 1;
}
}
void Player::Move(int speed)
{
if (GetAsyncKeyState(VK_DOWN))
{
if (m_grid[playerPos->y+1][playerPos->x] == ' ' || m_grid[playerPos->y + 1][playerPos->x] == '*') //if desired space is empty or has a coin
{
playerPos->new_y = playerPos->y + speed; //new player pos = current pos + speed
m_grid[playerPos->y][playerPos->x] == ' '; //old space is now empty
}
}
if (GetAsyncKeyState(VK_UP))
{
if (m_grid[playerPos->y-1][playerPos->x] == ' ' || m_grid[playerPos->y-1][playerPos->x] == '*')
{
playerPos->new_y = playerPos->y - speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
if (GetAsyncKeyState(VK_RIGHT))
{
if (m_grid[playerPos->y][playerPos->x+1]== ' ' || m_grid[playerPos->y][playerPos->x+1] == '*')
{
playerPos->new_x = playerPos->x + speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
if (GetAsyncKeyState(VK_LEFT))
{
if (m_grid[playerPos->y][playerPos->x-1] == ' ' || m_grid[playerPos->y][playerPos->x-1] == '*')
{
playerPos->new_x = playerPos->x - speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
}
void Player::Print_score()
{
}
首先:grid.h和player.h相互包含。应该避免这种情况,在这种情况下,grid.h 不需要 player.h.
实际问题:您的播放器没有可以使用的Grid对象。理想情况下,Grid 和 Player 应该是 Game class 的成员,并且对 Grid 对象的引用应该传递给 Player。所以它可以使用 Grid::m_grid。就像现在一样,您使用另一个仅声明为外部的 m_grid,因此没有真正的实例化。如果没有 Game class,这可以在 main() 中完成一个简单的练习,但是您需要声明一个 Grid 对象来访问它,如果玩家想要基于网格移动,则需要引用它。