码:
#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;
}
我想做的是在“ case sf :: Event :: KeyPressed:”处按“ Space”,然后创建一个新的Sprite。类似于开头的代码:“ sf :: Sprite sprite”,但类似于类似于“ sprite [#]”的数组。但是我想知道的是,我想知道“ sf :: Sprite”是否可以有一个数组,但实际上找不到任何数组。
例如:
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像素)移动。
最佳答案
std :: vector是您要寻找的:
std::vector<sf::Sprite> sprites;
当您按空格键时:
sf::Sprite sprite;
// Initialization here
sprites.push_back(sprite);
然后,当您使用它时:
sprites[x].setTexture(SomeTexture);
关于c++ - 使用sf::sprite创建Sprite对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28642609/