由参数C++SDL实例化的播放器类

Player classes instantiated by argument C++ SDL

本文关键字:播放器 实例化 参数 C++SDL      更新时间:2023-10-16

所以,再次制作一个pong克隆,并遇到一个凸起。我正在为我的状态机创建game类gamestate,在定义中,我实例化了我为不同的玩家划桨创建的类的两个类,并给它们两个不同的参数,这些参数将决定该类是成为玩家一还是玩家二对象。

但程序似乎不允许我在类定义中传递参数。我试图将类实例化放在game类构造函数中,但事件/逻辑/渲染函数无法访问桨板类。这让我认为它必须在game类定义中。这是出现问题的代码。

class Ball
{
public:
void events();
void logic();
void render();
Ball(int server);
~Ball();
};
class Paddle
{
private:
SDL_Rect Paddle_Bound;
int yVel;
SDL_Rect *clip;
int player_key;
public:
void events();
void logic();
void render();
Paddle(int player);
~Paddle();
};
class Game : public GameState
{
private:
int server;
TTF_Font *score = NULL;
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball Ball(2);
public:
void events();
void logic();
void render();
Game();
~Game();
};

写的行

Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball Ball(2);

是那些引发错误的人,上面写着"需要一个类型说明符"。现在我知道这意味着它想要类似int player的东西,但我需要传递一个数字,这样构造函数就可以决定它将是哪个播放器,最终决定用什么键在屏幕上上上下移动类。因此,为了非常清楚,我希望能够将一个参数传递给类实例化,这样类构造函数就可以将类变成左或右桨。那么,我该如何在另一个类中使用参数实例化一个类呢?

谢谢!

EDIT:对于构造函数的问题,我有另一个例子,它是完全编码的,我不能把类初始值设定项放在构造函数中。因此,基本上在标题屏幕中有一个按钮,它的属性存储在Button类中,我创建该类来管理按钮。

class GameState
{
public:
virtual void events() = 0;
virtual void logic() = 0;
virtual void render() = 0;
virtual ~GameState(){};
};
class Button
{
public:
SDL_Rect button_clip[2];
SDL_Rect button;
SDL_Surface *button_sprite = NULL;
Button();
};
class Title : public GameState
{
private:
SDL_Surface *Title_Message = NULL;
SDL_Rect *clip;
// THIS IS WHERE I HAVE TO PLACE THIS LINE 
//Button Title_Button;
public:
void events();
void logic();
void render();
Title();
~Title();
};
Title::Title()
{
//THIS IS WHERE IT SHOULD BE ACCORDING TO WHAT YOU'RE TELLING ME
Button Title_Message;
//text/sprites
Title_Message = TTF_RenderText_Solid(font, "PONG", color);
Title_Button.button_sprite = load_image("Start.png");
//region for button
Title_Button.button.x = 200;
Title_Button.button.y = 350;
Title_Button.button.w = 100;
Title_Button.button.h = 50;
//clips not hover
Title_Button.button_clip[0].x = 0;
Title_Button.button_clip[0].y = 0;
Title_Button.button_clip[0].w = 100;
Title_Button.button_clip[0].h = 50;
//clips hover
Title_Button.button_clip[1].x = 0;
Title_Button.button_clip[1].y = 50;
Title_Button.button_clip[1].w = 100;
Title_Button.button_clip[1].h = 50;
}
Title::~Title()
{
SDL_FreeSurface(Title_Message);
SDL_FreeSurface(Title_Button.button_sprite);
}
void Title::events()
{
int x = 0;
int y = 0;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_MOUSEMOTION)
{
x = event.motion.x;
y = event.motion.y;
if ((x > Title_Button.button.x) && (x < (Title_Button.button.x + Title_Button.button.w)) && (y > Title_Button.button.y) && (y < (Title_Button.button.y + Title_Button.button.h)))
{
clip = &Title_Button.button_clip[1];
}
else
{
clip = &Title_Button.button_clip[0];
}
}
if (event.type == SDL_QUIT)
{
quit = true;
}
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
if ((x > Title_Button.button.x) && (x < (Title_Button.button.x + Title_Button.button.w)) && (y > Title_Button.button.y) && (y < (Title_Button.button.y + Title_Button.button.h)))
{
set_next_state(GAME);
}
}
}
}
}
void Title::logic()
{
}
void Title::render()
{
apply_surface(Title_Button.button.x, Title_Button.button.y, Title_Button.button_sprite, screen, clip);
apply_surface((SCREEN_WIDTH - Title_Message->w) / 2, 100, Title_Message, screen);
}

这里的问题是,当我把Title_Message放在void Title::Title()构造函数中时,事件/逻辑/渲染函数根本不知道它存在。但是当我把它放在类定义中时,我不能通过它传递参数

值应该在构造函数定义中初始化:而不是在类定义中初始化。

class Game : public GameState
{
private:
int server;
TTF_Font *score;
Paddle PlayerOne;
Paddle Playertwo;
Ball ball;  // same class name and variable name won't work
public:
void events();
void logic();
void render();
Game();
~Game();
};
...
Game::Game()
:   score(NULL)
,   PlayerOne(1)
,   PlayerTwo(2)
,   ball(2)
{
...
}

编辑:对于标题消息内容

class Title : public GameState
{
private:
SDL_Surface *Title_Message = NULL;
SDL_Rect *clip;
// THIS IS WHERE I HAVE TO PLACE THIS LINE 
Button Title_Button;
Button Title_Message;
public:
void events();
void logic();
void render();
Title();
~Title();
};
Title::Title()
{
//text/sprites
Title_Message = TTF_RenderText_Solid(font, "PONG", color);
Title_Button.button_sprite = load_image("Start.png");

您不能在方法之外执行此操作。在构造函数中执行此操作

class Game : public GameState
{
private:
int server;
TTF_Font *score;
Paddle PlayerOne(1);
Paddle Playertwo(2);
Ball ballObject(2);
public:
void events();
void logic();
void render();
Game(): PlayerOne(1), Playertwo(2), ballObject(2){};
~Game();

};

编辑:已经发布了一个答案,很抱歉发布了同样的答案。我不太会使用这个界面。因此,计算代码块的生成需要时间。

编辑2:重复您的编辑。您必须在类本身中声明对象。只是你不能在声明中实例化它。你需要学习c++类和普通类是如何工作的。

class class1
{
private:
class class2Obj(args);
public:
class1();
~class1();
}

上面的例子是错误的。要做到这一点,你应该像这样做

class class1
{
private:
class2 class2Obj;
public:
class1();
~class1();
}
class1::class1()
{
class2Obj(args);
}