Witam,
Jestem początkującym, dlatego proszę o wyrozumiałość
Piszę pacmana w sfml cpp i napotkałem pewien problem, mianowicie
class pos // klasa pomocnicza
{
public:
int x;
int y;
const int Scale_times = 18;
const int Scale_plus = 8;
pos() = default;
pos(int px, int py);
friend ostream& operator<<(ostream& fout, const pos& print);
};
class Plansza
{
protected:
int mMap[28][31] = { 0 };
pos Duszki_pos[4];
pos Pacman_pos;
vector<pos> punkty;
int Pkt_total;
public:
Plansza();
void wypisz_sie();
Plansza(string name);
void wczytaj(string nazwa);
void uzupelnij_swoje_dane();
enum TileType
{
Pill = 1,
Wall = 3,
PacMan = 4,
Tunnel = 7,
GhostSpawn = 9
};
void makeWallsMap(sf::RenderTarget& target);
void drawHorizontalLine(sf::RenderTarget& rt, int x1, int x2, int y);
void drawVerticalLine(sf::RenderTarget& rt, int y1, int y2, int x);
};
class Pacman: public Plansza, public Drawable
{
int x = 0;
int y = 0;
public:
Pacman();
Vector2f getPosition();
private:
sf::CircleShape ksztalt;
const float promien{ 10.0f };
const float kulka_Velocity{ 3.0f };
sf::Vector2f velocity{ kulka_Velocity, kulka_Velocity }; //wektor predkosci 2d
virtual void drawPac(sf::RenderTarget& target, sf::RenderStates state) const;
};
Przy takiej strukturze danych, tworze obiekt Pacman gracz(); ale przy wywołaniu gracz.drawPac(…) kompilator pokazuje mi, że wyrażenie musi mieć typ klasy i nie bardzo wiem dlaczego może tak się dziać. Do tego mam pytanie odnośnie klasy Drawable, czy dziedziczenie jest konieczne pomimo tego, że i tak nadpisuje wirtualnie tą funkcję w klasie? Nie nadpisując klasa staje się wirtualna.
Main :
RenderWindow window{ VideoMode(800,800), "Okienko", Style::Default};
Plansza Map("Level1.txt");
Map.wypisz_sie();
Pacman g();
while (window.isOpen())
{
Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
window.close();
}
g.drawPac(); // tutaj jest blad
Map.makeWallsMap(window);
window.display();
}
return 0;
}
Konstruktor klasy :
Pacman::Pacman()
{
cout << "Konstruktor klasy!" << endl;
int x = Pacman_pos.x * Pacman_pos.Scale_times + Pacman_pos.Scale_plus;
int y = Pacman_pos.y * Pacman_pos.Scale_times + Pacman_pos.Scale_plus;
ksztalt.setPosition(x, y);
ksztalt.setRadius(this->promien);
ksztalt.setFillColor(sf::Color::White);
ksztalt.setOrigin(this->promien, this->promien);
}