C++程序的图片问题
C++ program problems with picture
每当我运行此程序时,按下按钮时,"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();
}
}
}
在渲染每个帧之前,需要使用RenderWindow::Clear
清除渲染曲面。
相关文章:
- 基于boost的程序的静态链接——zlib问题
- 使用mongocxx驱动程序时包含头文件问题
- 运行程序时出现问题
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 将应用程序从32位移植到64位时出现问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- C++ 每次运行程序时我都会"nan"输出的问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 如何解决在负数的情况下程序以相同方式工作的问题?
- 在树莓派上用libtorch构建程序时的链接问题
- 覆盖应用程序的低级别键盘挂钩问题
- 运行长度编码程序 c++ 的问题
- 编译 Cilk Plus 程序时遇到问题
- 我的堆栈和库存清单程序的结构有什么问题?
- 系统错误:程序无法启动,因为您的计算机中缺少MSVCP140D.DLL。尝试重新安装该程序以解决此问题
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- 桌面计算器出现问题.C++程序设计语言
- 内存泄漏问题c++程序
- 写函数的基本问题.程序随机跳到最后