C++ SFML 创建形状向量并在函数中绘制它们

C++ SFML creating a vector of shapes and drawing them in a function

我想创建一个由随机放置的正方形组成的矢量并将它们绘制到屏幕上,尝试传递对矢量的引用但我无法让它工作:(

consumable.h

#ifndef CONSUMABLE_H
#define CONSUMABLE_H

#include <SFML/Graphics.hpp>

using namespace std;
using namespace sf;

class consumable
{
    public:
     consumable();
    virtual ~consumable();

    vector<RectangleShape> fCreateConsumable(vector<RectangleShape>& vConsumable);
    void fDrawTarget(float x, float y, RenderWindow &thatWindow);

protected:

private:
    vector<RectangleShape> vConsumable;
    RectangleShape _consumable;
};

consumable.cpp

#include "consumable.h"

consumable::consumable()
{
    //ctor
}

consumable::~consumable()
{
    //dtor
}
void consumable::fCreateConsumable(){
    int consumableX{0}, consumableY{0};

    for(int i=0;i<4;i++){
        consumableX = (rand() % 31) + 1;
        consumableY = (rand() % 22) + 1;
        _consumable.setPosition((consumableX * 25), (consumableY * 25));
        _consumable.setSize(sf::Vector2f(25.0f,25.0f));
        _consumable.setFillColor(sf::Color::Magenta);
        vConsumable.push_back(_consumable);
    }
}
void consumable::fDrawTarget(float x, float y, RenderWindow &thatWindow){
    void fCreateConsumable();

    for(int i{0};i< vConsumable.size();i++){
        thatWindow.draw(vConsumable[i]);
    }
}

main.cpp

#include <iostream>
#include <SFML/Graphics.hpp>

#include "consumable.h"

using namespace std;
using namespace sf;

int main()
{
    consumable Consumable;
    RenderWindow window(VideoMode(800,750), "C++ Snake"); 

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
          switch(event.type)
          {
            case Event::Closed:
              window.close();
              break;
            default:
              break;
          }
        }
        window.clear();
        Consumable.fDrawTarget(25,25,window);
        window.display();
    }
    std::cout << "Finished" << std::endl;
    return 0;
}

我想循环播放

看看你的 class consumable,我会先将 _consumable 设为本地并重构 fCreateConsumable 添加另一个函数。

class consumable
{
    public:
    // ...    
    vector<RectangleShape> fCreateConsumable(vector<RectangleShape>& vConsumable);
    void fDrawTarget(float x, float y, RenderWindow &thatWindow);
    void UpdatePositions();
private:
    vector<RectangleShape> vConsumable;
};

通过从 fDrawTarget 中删除 fCreateConsumable,您可以避免创建新的 RectangleShape,重复使用旧的三个,并用新的位置更新它们。

void consumable::UpdatePositions(){
    srand(time(NULL));
    for(int i{0};i< vConsumable.size();i++){
        vConsumable[i].setPosition(((rand() % 31) + 1) * 25, ((rand() % 22) + 1) * 25);
    }
}
void consumable::fCreateConsumable(){
    int consumableX{0}, consumableY{0};
    srand(time(NULL));
    for(int i=0;i<4;i++){
        consumableX = (rand() % 31) + 1;
        consumableY = (rand() % 22) + 1;
        _consumable.setPosition((consumableX * 25), (consumableY * 25));
        _consumable.setSize(sf::Vector2f(25.0f,25.0f));
        _consumable.setFillColor(sf::Color::Magenta);
        vConsumable.push_back(_consumable);
    }
}
void consumable::fDrawTarget(float x, float y, RenderWindow &thatWindow){
    UpdatePositions();
    for(int i{0};i< vConsumable.size();i++){
        thatWindow.draw(vConsumable[i]);
    }
}

就我个人而言,我也会从 fDrawTarget 中移出 UpdatePositions 并将 drawcall 设为 const 函数。这样你就可以将更新和渲染分开。如果不这样做,请将 UpdatePositions 移至私有范围。