c++ - 如何让我的圈子用键移动

标签 c++ sfml

我正在做我的第一个关于 SFML C++ 的项目,我正在尝试将它们结合起来。 我想做的是拥有我制作的圆圈:

sf::CircleShape shape(50);
shape.setPosition(800, 450);

shape.setFillColor(sf::Color(100, 250, 50));

现在我尝试使用 W、A、S、D 或箭头键移动它。

但我不确定该怎么做,我尝试了几种方法,例如:

        if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up))
            Sprite.Move(spriteSpeed * App.GetFrameTime(), 0);

但是我不确定我做错了什么,有人可以帮助我吗? 提前致谢!

这是我在 atm 上的密码。

    #include "stdafx.h"
#include<SFML/Graphics.hpp>
#include<string>
#include<iostream>

int main()
{
    //Here we declare the render window so we can talk to it.
    sf::RenderWindow window;

    //sf::VideoMode is to set the size of the window
    //The seconds parameter (the string) is for setting the title
    //The style is to show/hide the close button and the title bar, or to set full screen
    window.create(sf::VideoMode(1600, 900), " My First SFML Game", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);

    //----------------------------------wait for a key to be pressed-------------------------------------
    //This shows a message that you should press a key
    /*std::cout << "Press a key to continue." << std::endl;*/
    //---------------------------------------------------------------------------------------------------

    //----------------------------------Showing a message------------------------------------------------
    //Define the messages that will be showed, and the display text
    std::string message = "Hello my name is Jean-Paul van Houten";
    std::string display = "";

    int index = 0;

    window.setKeyRepeatEnabled(false);
    //----------------------------------------------------------------------------------------------------

    sf::CircleShape shape(50);
    shape.setPosition(800, 450);

    //this while loop will only be called if the window is open.
    while(window.isOpen())
    {
        //Define the event variable
        sf::Event eventSF;

        //Check if there is an event
        while(window.pollEvent(eventSF))
        {


            shape.setFillColor(sf::Color(100, 250, 50));
            //shape.setPosition(eventSF.mouseMove.x 0 sha,eventSF.mouseMove.y);
            window.clear();
            switch(eventSF.type)
            {
            case sf::Event::Closed:
                window.close();
                break;
            case sf::Event::MouseEntered:
                std::cout << "Mouse within screen bounds"  << std::endl;
                break;
            case sf::Event::MouseLeft:
                std::cout << "Mouse outisde the screen bounds" << std::endl;
                break;
            case sf::Event::MouseMoved:
                std::cout << "X: " << eventSF.mouseMove.y << " Y: " << eventSF.mouseMove.y <<  std::endl;

                break;
            case sf::Event::MouseButtonPressed:
                if(eventSF.mouseButton.button == sf::Mouse::Left)
                    std::cout << "Left Button Pressed At: X: " << eventSF.mouseButton.x << " Y: " << eventSF.mouseButton.y << std::endl;
                break;
            case sf::Event::MouseWheelMoved:
                std::cout << "Scrolled: " << eventSF.mouseWheel.delta << std::endl;
                break;

            case sf::Event::GainedFocus:
                std::cout << "Window Active" << std::endl;
                break;
            case sf::Event::LostFocus:
                std::cout << "Window Not Active" << std::endl;
                break;
            case sf::Event::Resized:
                std::cout << "Width: " << eventSF.size.width << " Height: "  << eventSF.size.height << std::endl;
                break;

            case sf::Event::TextEntered:
                if(eventSF.text.unicode != 8)//(eventSF.text.unicode >= 33 && eventSF.text.unicode <= 126) //This is to only include the characters between the number, now we use punctuation and letters.
                    std::cout << (char)eventSF.text.unicode;
                else if(eventSF.text.unicode == 8)
                    display = display.substr(0, display.length() - 1);

                system("cls");
                std::cout << display;
                break;
            }



            window.draw(shape);

            //If you release a key
            if(eventSF.type == sf::Event::KeyReleased)
            {
                //and this key is the enter key
                if(eventSF.key.code == sf::Keyboard::Return)
                {
                    display += message[index];
                    index ++;
                    system("cls"); //CLS on windows, clear on mac/linux
                    std::cout << display;
                }
            }
        }

        window.display();

    }
}

最佳答案

首先,您必须在事件轮询开关中添加 KeyPressed 事件处理,并在其中添加移动 Sprite 的代码

switch(eventSF.type)
{
    [...]
    case sf::Event::KeyPressed:
        if(eventSF.key.code == sf::Keyboard::Up)
        {
            shape.move(0, 1)
        }
        break;

}

此外,

shape.setFillColor(sf::Color(100, 250, 50));

不应该在游戏循环中。

还有这个

window.clear();
window.draw(shape);
window.display();

应该在事件轮询循环之外:

while(window.isOpen())
{
    //Define the event variable
    sf::Event eventSF;

    //Check if there is an event
    while(window.pollEvent(eventSF))
    {
        [...]
    }


    window.clear();
    window.draw(shape);
    window.display();
}

关于c++ - 如何让我的圈子用键移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841637/

相关文章:

c++ - int (%d) 和 %.0lf 有什么区别?

c++ - SFML: Vector2<double> 无法编译

c++ - 错误 : `MEMBER` in `class CLASS` does not name a type; C++

c++ - 存在线程错误的 SFML 程序

c++ - 获取 sf::View 的 X 和 Y 偏移量

c++ - iOS预编译头文件包含<boost/shared_ptr.hpp>时出错

c++ - 纤维可以在线程之间迁移吗?

c++ - 模板化模拟类中的 MOCK_METHODx() 定义缺少“typename”

c++ - 构造数组

events - 重复关键事件阻塞