【发布时间】:2017-05-17 13:00:56
【问题描述】:
我想创建一个由随机放置的正方形组成的向量并将它们绘制到屏幕上,尝试传递对向量的引用,但我无法让它工作:(
消耗品.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;
}
我想循环
【问题讨论】:
-
当您打算包含 consumable.h 时,您已经包含了 main.cpp 的副本。请edit你的问题来解决这个问题。
-
我会在循环中将
consumableX和consumableY声明为const int。同样,我会将_consumable设为fCreateConsumable内的本地(可能再次在循环内)。在fDrawTarget中,我会将循环写为for(const auto& item: vConsumable)){ thatWindow.draw(item);} -
最后,以什么方式“你不能让它工作”?编译器错误信息(请准确显示)?出乎意料的输出? (什么?)崩溃? (在哪里?)
-
添加了consumable.h,至于错误什么都不会被绘制到屏幕上,我尝试找到一种方法来使用调试工具查看向量,但我找不到任何有用的东西
-
我目前正在尝试将我的原始项目转换为面向对象的格式,因为我不擅长类等。可以在我原来的 main.cpp 中发布文本墙,但即使看起来真的很难看对我来说