2013-02-21 50 views
0

每当我运行该程序时,按下按钮时“Car1”图片重复。我该如何解决这个问题?我找不到任何会导致它的东西。预先感谢您的帮助。代码如下:带图片的C++程序问题

#include <iostream> 
#include <SFML/Graphics.hpp> 
using namespace std; 


int main() 
{ 

//variables,etc 

sf::RenderWindow window(sf::VideoMode(800,600), "Maze"); 
window.setFramerateLimit(60); 
window.setKeyRepeatEnabled(false); 
bool play = true; 

bool Up = false; 
bool rButton = false; 
bool lButton = false; 
bool down = false; 

int xVelocity = 0, yVelocity = 0; 
int xVelocity2 = 0; yVelocity2 = 0; 
sf::Event event; 
sf::Texture pic1; 
pic1.loadFromFile("Data/Car1.png"); 

sf::Texture pic2; 
pic2.loadFromFile("Data/Car2.png"); 

sf::RectangleShape road; 
road.setSize(sf::Vector2f(100,200)); 
road.setPosition(100,220); 
road.setFillColor(sf::Color::White); 

sf::RectangleShape car1; 
car1.setSize(sf::Vector2f(70,70)); 
car1.setPosition(0,0); 
car1.setTexture(&pic1); 

sf::RectangleShape car2; 
car2.setSize(sf::Vector2f(70,70)); 
car2.setPosition(20,20); 
car2.setTexture(&pic2); 

//events 

while (play == true) 
{ 
while(window.pollEvent(event)) 
{ 
if(event.type == sf::Event::Closed) 
{ 
    play = false; 
} 
if(event.type == sf::Event::KeyPressed&&event.key.code==sf::Keyboard::Up) 
{ 
Up = true; 
} 

if(event.type == sf::Event::KeyReleased&&event.key.code==sf::Keyboard::Up) 
{ 
Up = false; 
} 

if(event.type == sf::Event::KeyPressed&&event.key.code==sf::Keyboard::Right) 
{ 
rButton = true; 
} 

if(event.type == sf::Event::KeyReleased&&event.key.code==sf::Keyboard::Right) 
{ 
rButton = false; 
} 

if(event.type == sf::Event::KeyPressed&&event.key.code==sf::Keyboard::Left) 
{ 
lButton = true; 
} 

if(event.type == sf::Event::KeyReleased&&event.key.code==sf::Keyboard::Left) 
{ 
lButton = false; 
} 


if(event.type == sf::Event::KeyPressed&&event.key.code==sf::Keyboard::Down) 
{ 
down = true; 
} 

if(event.type == sf::Event::KeyReleased&&event.key.code==sf::Keyboard::Down) 
{ 
down = false; 
} 

//Logic 

if (rButton == true) 
{ 
xVelocity = 5; 
} 
if(lButton == true) 
{ 
xVelocity = 5; 
} 

if(lButton ==false&&rButton==false) 
{ 
xVelocity = 0; 
} 
if(down == true) 
{ 
yVelocity = 5; 
} 

if(Up == true) 
{ 
yVelocity = -5; 
} 

if(Up && down == false) 
{ 
yVelocity = 0; 
} 

car1.move(xVelocity,yVelocity); 



window.draw(car1); 
window.draw(car2); 
window.draw(road); 
window.display(); 


} 
} 
} 
+0

你可能会做一个简短的例子,最好是一个正确缩进?阅读http://sscce.org/寻求帮助。 – 2013-02-21 04:37:52

回答