用于区分播放器键的类管理

时间:2013-12-30 02:06:19

标签: c++ class sdl game-development

制作pong克隆,并且在运动和课程方面遇到一些麻烦。所以我有一个类Game来管理类Paddle的两个实例,并在Game类的构造函数中启动两个不同的paddle类中的变量并设置变量{{1根据类将响应哪些键进行动作,{}为player1。然后在2类事件函数内部,我使用Paddleif(player == 1)来判断何时检查键以更改球拍的y速度。我遇到的问题是因为if(player == 2)类的事件函数看起来像这样:

Game

事件函数和void Game::events() { PlayerOne.events(); PlayerTwo.events(); Game_Ball.events(); } 类的类定义如下所示:

Paddle

正在发生的事情混淆了钥匙。通常PlayerOne使用“O / K”移动而PlayerTwo使用“W / S”移动,但有时我会按任意一个而相反的类会移动。它似乎没有意义。这是class Paddle { public: int player; SDL_Surface *sprite = NULL; float x, y, w, h; float yVel; SDL_Rect *clip; void events(); void logic(Uint32 deltaTicks); void render(); Paddle(); ~Paddle(); }; void Paddle::events() { while (SDL_PollEvent(&event)) { Uint8 *keystates = SDL_GetKeyState(NULL); if (player == 1) { if (keystates[SDLK_o] == 1) { yVel -= 100; } if (keystates[SDLK_k] == 1) { yVel += 100; } } else { if (keystates[SDLK_o] == 0) { yVel += 100; } if (keystates[SDLK_k] == 0) { yVel -= 100; } } if (player == 2) { if (keystates[SDLK_w] == 1) { yVel -= 100; } if (keystates[SDLK_s] == 1) { yVel += 100; } } else { if (keystates[SDLK_w] == 0) { yVel += 100; } if (keystates[SDLK_s] == 0) { yVel -= 100; } } } } 类构造函数和定义

Game

class Game : public GameState { private: int server; TTF_Font *score = NULL; Paddle PlayerOne; Paddle PlayerTwo; Ball Game_Ball; public: SDL_Rect clip[2]; void events(); void logic(); void render(); Game(); ~Game(); }; Game::Game() { //RESOURSES PlayerOne.sprite = load_image("Game_sprite.png"); PlayerTwo.sprite = load_image("Game_sprite.png"); clip[0].x = 0; clip[0].y = 0; clip[0].w = 20; clip[0].h = 20; clip[1].x = 0; clip[1].y = 20; clip[1].w = 20; clip[1].h = 100; //PLAYER ONE PlayerOne.x = 420; PlayerOne.y = 100; PlayerOne.w = 60; PlayerOne.h = 100; PlayerOne.clip = &clip[1]; PlayerOne.yVel = 0; PlayerOne.player = 1; //PLAYER TWO PlayerTwo.x = 60; PlayerTwo.y = 100; PlayerTwo.w = 60; PlayerTwo.h = 100; PlayerTwo.clip = &clip[1]; PlayerTwo.yVel = 0; PlayerTwo.player = 2; //BALL Game_Ball.Ball_Bound.x = 190; Game_Ball.Ball_Bound.y = 190; Game_Ball.Ball_Bound.w = 60; Game_Ball.Ball_Bound.h = 60; Game_Ball.clip = &clip[0]; } 明确设置在两个对象中,player类的事件部分明确区分它们,因此,为什么键会混淆?我可以很容易地为第二个玩家写另一个类,但这样效率很低,我宁愿使用同一个类的两个副本,它们除了关键状态之外都做同样的事情。所以,我不确定是什么。

2 个答案:

答案 0 :(得分:0)

问题似乎是你有

if (player == 1) {
  check keys O K pressed
}
else {
  check keys O K released
} 

这没有任何意义,因为这会修改与播放器2上发布的键O K相关的值。

在任何情况下,您都不应让任何游戏对象轮询事件。主游戏循环应该将它们汇集一次并将它们分配给相应的游戏元素,例如:

void gameLoop() {
    while (true) {
    draw();
    updatePositions();
    handleEvents();
  }
}

void handleEvents() {
  while (SDL_PollEvent(&event)) {
    Uint8 *keystates = SDL_GetKeyState(NULL);

    if (keystates[SDLK_o]) paddle1->startMovingUp();
    else if (keystates[SDLK_k]) paddle1->startMovingDown();
    else paddle1->stopMoving();

    // same thing for player2
  }
}

Paddle::startMovingUp() {
  uvelY = -100;
}

Paddle::startMovingDown() {
  uvelY = 100;
}

Paddle::stopMoving() {
  uvelY = 0;
}

Paddle::updatePosition() {  // called on each frame
  positionY += uvelY;
}

您可以简化设计并只更新handleEvents()函数中的位置,但将位置更新与事件管理分开是一件好事。

答案 1 :(得分:0)

乍一看似乎你可以使用:

    if (player == 1)
    {
        if (keystates[SDLK_o] == 1)
        {
            yVel -= 100;
        }
        if (keystates[SDLK_k] == 1)
        {
            yVel += 100;
        }
    }
    else // player == 2
    {
        if (keystates[SDLK_w] == 1)
        {
            yVel -= 100;
        }
        if (keystates[SDLK_s] == 1)
        {
            yVel += 100;
        }
    }

我猜错了当错误的光标移动时,O会向下移动错误的光标吗?