如何使用 c++ 在 SFML 中配置播放器
How ove the player in SFML with c++
我正在用SFML用c++做一个游戏。我已经编写了一个移动玩家的代码,但是当游戏开始时,玩家会移动,但是当我离开按钮时,玩家会返回其原始位置。你能帮帮我吗?
主要:
#include <iostream>
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(1320, 840), "Window");
window.setPosition(sf::Vector2i(150, 50));
window.setSize(sf::Vector2u(1320, 840));
window.getPosition();
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(5);
window.getSize();
while (window.isOpen()) {
window.clear(sf::Color::White);
//texture
sf::Texture texture;
if (!texture.loadFromFile("/users/gaetanodonnarumma/desktop/background1.png", sf::IntRect(0, 0, 1320, 840))) {
return -1;
}
sf::Texture playerTexture;
if (!playerTexture.loadFromFile("/users/gaetanodonnarumma/desktop/quadrato-giallo.jpg", sf::IntRect(0, 0, 70, 70))) {
return -1;
}
//sprite
sf::Sprite backgroud;
backgroud.setTexture(texture);
sf::Sprite player;
double pX = 0;
double pY = 770;
player.setTexture(playerTexture);
player.setPosition(sf::Vector2f(pX, pY));
player.getPosition();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::D) {
player.move(10.f, 0.f);
}
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::D) {
player.setPosition(pX + 10, 770);
}
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::A) {
player.move(-5.f, 0.f);
}
}
}
//draw
window.draw(backgroud);
window.draw(player);
window.display();
}
return 0;
}
所有初始化代码都在每一帧上运行。这意味着在每一帧上,您都在创建(和销毁)一个新播放器,设置其纹理,并将其位置设置为 [0, 770]
.
将所有初始化移动到主绘制循环开始之前(while (window.isOpen())
之前)。
相关文章:
- SFML纹理像播放器
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 旋转播放器模型以指向一个点
- 播放器未在程序中的"X"和"O"之间切换
- DwmGetWindowAttribute 在全屏模式下无法获得媒体播放器的正确矩形大小
- 可以访问一个类中的播放器结构,但不能访问另一个类中的播放器结构
- C++ 播放器墙与 SFML 的冲突
- 如何在不显示媒体播放器的情况下在libVLC中拍摄快照
- OpenGL沿方向矢量移动播放器
- 如何将 cdparanoia 读取的 CD 音频数据移交给 ALSA 播放器?
- 我应该如何在文本RPG中调用播放器咒语
- 类模板"没有用于调用播放器的匹配函数::P layer()"
- 使用 Struct 进行 mp3 播放器模拟C++(不需要播放 mp3 的唯一显示列表)
- 如何修复结构播放器对象中的矢量位置,以便它在 Main 中与 me.position 一起使用?
- 将超能力高级音频播放器同步到绝对位置
- QT 5.8 WebEngine HTML 5视频播放器支持
- 克里昂未初始化记录类型:播放器
- 当磁盘几乎完整时,使用C 流式播放器复制Linux中的文件也不例外
- 如何使用 c++ 在 SFML 中配置播放器