【发布时间】:2015-06-24 12:45:01
【问题描述】:
我决定用 c++ 和 sfml 进行碰撞测试。但是当玩家击中方块时,你就不能再移动了。我对如何进行碰撞没有任何问题,但是当我实际发生碰撞时该怎么办。
这是我的代码:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <thread>
using namespace std;
using namespace sf;
RenderWindow window(VideoMode(500, 500), "SFML");
RectangleShape r1;
RectangleShape r2;
void collision(){
r1.setSize(Vector2f(50.0, 50.0));
r2.setSize(Vector2f(50.0, 50.0));
r1.setPosition(20, 200);
r2.setPosition(420, 200);
r1.setFillColor(Color::Red);
r2.setFillColor(Color::Blue);
}
int main(){
collision();
while (window.isOpen()){
Event event;
while (window.pollEvent(event)){
if (event.type == Event::Closed){
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::W))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, -0.05);
if (Keyboard::isKeyPressed(Keyboard::A))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(-0.05, 0.0);
if (Keyboard::isKeyPressed(Keyboard::S))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, 0.05);
if (Keyboard::isKeyPressed(Keyboard::D))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.05, 0.0);
window.draw(r2);
window.draw(r1);
window.display();
window.clear();
}
}
再次,我想知道如何正确移动您的播放器并使其在您无法进入物体时做到这一点。
提前致谢!
PS。请不要告诉我“呃,你的代码太可怕了。你的括号太烂了……”我知道。有点乱好吗?
谢谢。
【问题讨论】: