使用sf :: sprite创建Sprite对象

时间:2015-02-21 05:47:28

标签: c++ sfml

代码:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <string.h>

static void threadSleep(int newMilli) {
    std::this_thread::sleep_for(std::chrono::milliseconds(newMilli));
}


int main() {
    // Sprite =======

    bool gate = true;

    sf::Texture texture;
    if (!texture.loadFromFile("index.png")) {
        std::cout << "Error loading index.png:" << std::endl;
    } else { std::cout << "Loaded 'index.png'" << std::endl; }

    // sprite0
    sf::Sprite sprite;
    sprite.setTexture(texture);
    sprite.setTextureRect(sf::IntRect(0, 0, 10, 10));
    float x = 315, y = 235;
    sprite.setPosition(x, y);

    // sprite1
    sf::Sprite sprite1;
    sprite1.setTexture(texture);
    sprite1.setTextureRect(sf::IntRect(0, 0, 10, 10));
    float x1 = 305, y1 = 235;
    sprite1.setPosition(x1, y1);

    // sprite2
    sf::Sprite sprite2;
    sprite2.setTexture(texture);
    sprite2.setTextureRect(sf::IntRect(0, 0, 10, 10));
    float x2 = 295, y2 = 235;
    sprite2.setPosition(x2, y2);

    // sprite3
    sf::Sprite sprite3;
    sprite3.setTexture(texture);
    sprite3.setTextureRect(sf::IntRect(0, 0, 10, 10));
    float x3 = 285, y3 = 235;
    sprite3.setPosition(x3, y3);

    // sprite4
    sf::Sprite sprite4;
    sprite4.setTexture(texture);
    sprite4.setTextureRect(sf::IntRect(0, 0, 10, 10));
    float x4 = 275, y4 = 235;
    sprite4.setPosition(x4, y4);

    // Starting Window ==========================
    sf::RenderWindow window(sf::VideoMode(640, 480), "-Pandora's Box-");

    //   Window----------------------------------------------------------------------
    while (window.isOpen()) {
        sf::Event event;

        //==============================
        while (window.pollEvent(event)) {

            // The Message Loop (Brain of Program)
            switch (event.type) {

                // Close Window
            case sf::Event::Closed:
                window.close();
                break;

            case sf::Event::KeyPressed:
                switch (event.key.code) {

                case sf::Keyboard::A:
                    std::cout << "[a] -pressed-\n" << std::endl;

                    x4 = x3; y4 = y3;  // This just makes x4 copy x3 
                    x3 = x2; y3 = y2;  // and x3, x2, x1, x;...
                    x2 = x1; y2 = y1;  
                    x1 = x; y1 = y;

                    sprite.setPosition(x -= 10, y);
                    sprite1.setPosition(x1, y1);
                    sprite2.setPosition(x2, y2);
                    sprite3.setPosition(x3, y3);
                    sprite4.setPosition(x4, y4);

                         break;
                 case sf::Keyboard::W:
                     std::cout << "[w] -pressed-\n" << std::endl;

                     x4 = x3; y4 = y3;
                     x3 = x2; y3 = y2;
                     x2 = x1; y2 = y1;
                     x1 = x; y1 = y;

                     sprite.setPosition(x, y -= 10);
                     sprite1.setPosition(x1, y1);
                     sprite2.setPosition(x2, y2);
                     sprite3.setPosition(x3, y3);
                     sprite4.setPosition(x4, y4);

                     break;
                case sf::Keyboard::D:
                     std::cout << "[d] -pressed-\n" << std::endl;

                     x4 = x3; y4 = y3;
                     x3 = x2; y3 = y2;
                     x2 = x1; y2 = y1;
                     x1 = x; y1 = y;

                     sprite.setPosition(x += 10, y);
                     sprite1.setPosition(x1, y1);
                     sprite2.setPosition(x2, y2);
                     sprite3.setPosition(x3, y3);
                     sprite4.setPosition(x4, y4);

                     break;
                case sf::Keyboard::S:
                     std::cout << "[s] -pressed-\n" << std::endl;

                     x4 = x3; y4 = y3;
                     x3 = x2; y3 = y2;
                     x2 = x1; y2 = y1;
                     x1 = x; y1 = y;

                     sprite.setPosition(x, y += 10);
                     sprite1.setPosition(x1, y1);
                     sprite2.setPosition(x2, y2);
                     sprite3.setPosition(x3, y3);
                     sprite4.setPosition(x4, y4);

                    break;
                case sf::Keyboard::Space:
                    std::cout << "[space] -pressed-\n" << std::endl;

                    break;
                }
                break;

            case sf::Event::Resized:
                std::cout << event.size.width << " : " << event.size.height << std::endl;
                break;
            case sf::Event::LostFocus:
                std::cout << "[Pandora Box] -Lost Focus-\n" << std::endl;
                break;
            case sf::Event::GainedFocus:
                std::cout << "[Pandora Box] -Gained Focus-\n" << std::endl;
                break;

            }

        }


        window.clear();
        //===========================
        if (gate == true) {
            window.draw(sprite);
            window.draw(sprite1);
            window.draw(sprite2);
            window.draw(sprite3);
            window.draw(sprite4);
            } 
        //===========================
        window.display();
    }

    return 0;
}

我想要做的就是当我按下&#34; Space&#34; at&#34; case sf :: Event :: KeyPressed:&#34;然后创建一个新的精灵。比如一开始的代码:&#34; sf :: Sprite sprite&#34;,但类似于&#34; sprite [#]&#34;这样的数组。但Idk怎么样,我一直在寻找一个&#34; sf :: Sprite&#34;可以有一个数组,但无法在其上找到任何内容。

例如:

case sf::Keyboard::Space:
    counter += 1  // counter ='s 6 now
    sf::Sprite sprite[counter];

    sprite[counter].setTexture(texture);
    sprite[counter].setTextureRect(sf::IntRect(0, 0, 10, 10));
    float x = #, y = #; // don't worry about x or y
    sprite[counter].setPosition(x, y);

    break;

当然我会将sprite0-sprite4更改为相同的格式(使用数组),但有谁知道我怎么做到这一点?我无法在网上找到这样的东西,我怀疑stackoverflow上的任何人已经问过这个问题(已经搜索过)

如果它简单易行,请评论/回复,我是C ++和SFML的新手

- 提前谢谢。

此程序使用SFML,类似游戏的图形库

这也就像一场蛇游戏,目前所有它都可以像原来的蛇游戏一样以5块(10x10像素)的速度移动。

1 个答案:

答案 0 :(得分:1)

std :: vector就是你要找的东西:

std::vector<sf::Sprite> sprites;

按空格键时:

sf::Sprite sprite;
// Initialization here
sprites.push_back(sprite);

然后当你使用它时:

sprites[x].setTexture(SomeTexture);