如何用钥匙移动我的圆圈

时间:2015-01-08 13:47:13

标签: c++ sfml

我正在做我关于SFML C ++的第一个项目之一,我现在尝试结合使用。 我想要做的就是拥有我制作的Circle:

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);

但我不确定我做错了什么,有人能帮帮我吗? 提前谢谢!

这是我的代码。

    #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();

    }
}

1 个答案:

答案 0 :(得分:1)

首先,您必须在事件轮询开关内添加KeyPressed事件处理,并在内部添加用于移动精灵的代码

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();
}