每当我运行此程序时,按下按钮时会重复“Car1”图像。我该如何解决这个问题?我找不到任何会导致这种情况的东西。在此先感谢您的帮助。这是代码:
#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();
}
}
}