c++ - 移动后未绘制 SFML 形状

标签 c++ sfml

遵循 SFML 教程。这段代码应该允许我在屏幕上移动一个青色圆圈。在不使用 mPlayer.move(movement) 的情况下绘制圆圈效果很好,但是当这条线在 game.update 中运行时,形状从屏幕上消失,永远不会重新绘制。我错过了什么?

Game.cpp 

   Game::Game()
    :mWindow(sf::VideoMode(1440, 900, 32),"yeeboi1!!!!!") , mPlayer()
    {
    mPlayer.setRadius(20.f);
    mPlayer.setPosition(100.f, 110.f);
    mPlayer.setFillColor(sf::Color::Cyan);
    }


    Game::~Game()
{
}

    void Game::run() {
    while (mWindow.isOpen()) {
        processEvents();
        update();
        render();
    }
}

    void Game::processEvents()
    {
        sf::Event event;
        while (mWindow.pollEvent(event))
    {
            switch (event.type)
        {
        case sf::Event::KeyPressed:
            HandleInput(event.key.code, true);
            break;
        case sf::Event::KeyReleased:
            HandleInput(event.key.code, false);
            break;
        case sf::Event::Closed:
            mWindow.close();
            break;
        }

    }
}

    void Game::update() {
    sf::Vector2f movement(0.f, 0.f);
    if (mMoveUp)
        movement.y -= 1.f;
    if (mMoveDown)
        movement.y += 1.f;
    if (mMoveLeft)
        movement.x-= 1.f;
    if (mMoveRight)
        movement.x += 1.f;

    mPlayer.move(movement);




}

    void Game::render() {
    mWindow.clear();
    mWindow.draw(mPlayer);
    mWindow.display();
}

    void Game::HandleInput(sf::Keyboard::Key key, bool isPressed) {
    if (key == sf::Keyboard::W)
        mMoveUp = isPressed;
    if (key == sf::Keyboard::S)
        mMoveDown = isPressed;
    if (key == sf::Keyboard::A)
        mMoveLeft = isPressed;
    if (key == sf::Keyboard::D)
        mMoveRight = isPressed;
}



Game.h 

    #pragma once
    #include <SFML/Graphics.hpp>
    class Game
    {
    public:
    Game();
    ~Game();


    void run();

    private:
    void processEvents();
    void update();
    void render();
    void HandleInput(sf::Keyboard::Key key, bool isPressed);
    bool mMoveUp, mMoveDown, mMoveLeft, mMoveRight = false;

    private:
    sf::RenderWindow mWindow;
    sf::CircleShape mPlayer;
};




Main.cpp

#include "Game.h"


    int main()
    {
    Game game;
    game.run();

    return 0;
    }

最佳答案

为什么不转向基于时间的处理? 请务必阅读 http://gameprogrammingpatterns.com/game-loop.html以及。

我现在正坐在火车上,所以请为这个简短的回答道歉,但我想你已经明白了。查看 Game::run 和 update(dtAsSeconds) 的变化

Game.cpp
#include "Game.h"

Game::Game()
 :mWindow(sf::VideoMode(1440, 900, 32),"yeeboi1!!!!!") , mPlayer()
 {
 mPlayer.setRadius(20.f);
 mPlayer.setPosition(100.f, 110.f);
 mPlayer.setFillColor(sf::Color::Cyan);
 }


 Game::~Game()
{
}

 void Game::run() {
   sf::Clock m_Clock = sf::Clock();

 while (mWindow.isOpen()) {
    sf::Time dt = m_Clock.restart();

    // Count seconds since last process
    float dtAsSeconds = dt.asSeconds();

     processEvents();
     update(dtAsSeconds);
     render();
 }
}

 void Game::processEvents()
 {
     sf::Event event;
     while (mWindow.pollEvent(event))
 {
         switch (event.type)
     {
     case sf::Event::KeyPressed:
         HandleInput(event.key.code, true);
         break;
     case sf::Event::KeyReleased:
         HandleInput(event.key.code, false);
         break;
     case sf::Event::Closed:
         mWindow.close();
         break;
     }

 }
}

 void Game::update(float dtAsSeconds) {
 sf::Vector2f movement(0.f, 0.f);
 if (mMoveUp)
     movement.y -= 1.f * dtAsSeconds * 100;
 if (mMoveDown)
     movement.y += 1.f * dtAsSeconds * 100;
 if (mMoveLeft)
     movement.x-= 1.f * dtAsSeconds * 100;
 if (mMoveRight)
     movement.x += 1.f * dtAsSeconds * 100;

 mPlayer.move(movement);




}

 void Game::render() {
 mWindow.clear();
 mWindow.draw(mPlayer);
 mWindow.display();
}

 void Game::HandleInput(sf::Keyboard::Key key, bool isPressed) {
 if (key == sf::Keyboard::W)
     mMoveUp = isPressed;
 if (key == sf::Keyboard::S)
     mMoveDown = isPressed;
 if (key == sf::Keyboard::A)
     mMoveLeft = isPressed;
 if (key == sf::Keyboard::D)
     mMoveRight = isPressed;
}

Game.h

#pragma once
#include <SFML/Graphics.hpp>
class Game
{
public:
Game();
~Game();


void run();

private:
void processEvents();
void update(float dtAsSeconds);
void render();
void HandleInput(sf::Keyboard::Key key, bool isPressed);
bool mMoveUp, mMoveDown, mMoveLeft, mMoveRight = false;

private:
sf::RenderWindow mWindow;
sf::CircleShape mPlayer;
};

main.cpp

#include "Game.h"

int main()
{
Game game;
game.run();

return 0;
}

关于c++ - 移动后未绘制 SFML 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45087211/

相关文章:

c++ - 如何仅将宏扩展为C++文件的预处理步骤

c++ - 向其绘制时屏幕变黑 SFML C++

c++ - 使用套接字通信阻止服务器-客户端之间的连接

c++ - SFML 不会在 CodeBlocks 13.12 中运行

c++ - 堆腐败,怎么办?

c++ - `char16_t` 和 `char32_t` 是用词不当吗?

c++ - 是否可以让调试器显示 boost 函数对象指向的函数的名称?

c++ - 未找到成员声明

c++ - 如何在 C++ 中将多个参数传递给 BackgroundWorker

c++通过串联迭代push_back?