gpt4 book ai didi

c++ - SFML 中 getPosition() 的问题

转载 作者:行者123 更新时间:2023-11-28 04:36:50 24 4
gpt4 key购买 nike

我是 C++ 的新手,所以我对这个有点困惑。我试图让玩家重生,然后当用户按下“C”时,玩家会切换汽车。当前发生的事情是玩家最初在正确的位置生成,然后汽车在玩家最初生成的位置生成,而不是当前位置。但是,由于我使用的是 getPosition() 函数,因此我假设它会在玩家的当前位置生成。

播放器.h:

#pragma once
#include <SFML\Graphics.hpp>
#include <string>
class player {

public:
sf::Texture normalTexture;
sf::Sprite normalSprite;
sf::Texture carTexture;
sf::Sprite carSprite;
sf::Texture springTexture;
sf::Sprite springSprite;
sf::Texture rocketTexture;
sf::Sprite rocketSprite;
sf::IntRect normalRect[1];
sf::IntRect carRect[1];
sf::IntRect springRect[1];
sf::IntRect rocketRect[1];


bool rocket=false;
bool car=false;
bool spring=false;
bool normal=false;

player();
player(sf::Vector2f position, std::string normalFileLoc, std::string carFileLoc, std::string springFileLoc, std::string rocketFileLoc);

void update();
};

播放器.cpp:

#include "Player.h"

player::player() : player::player(sf::Vector2f(100, 0), "player.jpg", "car.png", "spring.png", "rocket.png") {
}

player::player(sf::Vector2f position, std::string normalFileLoc, std::string
carFileLoc, std::string springFileLoc, std::string rocketFileLoc)
{
normalTexture.loadFromFile(normalFileLoc);
normalRect[0] = sf::IntRect(0, 0, 64, 128);
normalSprite.setTexture(normalTexture);
normalSprite.setTextureRect(normalRect[0]);
normalSprite.setOrigin(0,0);
normalSprite.setScale(1, 1);
normalSprite.setPosition(position.x, position.y);

carTexture.loadFromFile(carFileLoc);
carRect[0] = sf::IntRect(0, 0, 64, 64);
carSprite.setTexture(carTexture);
carSprite.setTextureRect(carRect[0]);
carSprite.setOrigin(0, -64);
carSprite.setScale(1, 1);
carSprite.setPosition(normalSprite.getPosition().x, normalSprite.getPosition().y);

}

Main.cpp 我知道它看起来很恶心,我计划稍后让它看起来更漂亮:

#include <SFML/Graphics.hpp>
#include <SFML\Graphics\Rect.hpp>
#include "player.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(750, 750), "SFML Works!");

player thePlayer;

const float gravity = .001;
int groundheight = 500;
sf::Vector2f velocity(sf::Vector2f(0, 0));

thePlayer.normal = true;


while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();

if (event.type == sf::Event::KeyPressed)
{
if (thePlayer.car && event.key.code == sf::Keyboard::C)
{
thePlayer.car = false;
thePlayer.spring = false;
thePlayer.rocket = false;
thePlayer.normal = true;
break;
}
if (thePlayer.spring && event.key.code == sf::Keyboard::S)
{
thePlayer.car = false;
thePlayer.spring = false;
thePlayer.rocket = false;
thePlayer.normal = true;
break;
}
if (thePlayer.rocket && event.key.code == sf::Keyboard::R)
{
thePlayer.car = false;
thePlayer.spring = false;
thePlayer.rocket = false;
thePlayer.normal = true;
break;
}
if (event.key.code == sf::Keyboard::C)
{
thePlayer.car = true;
thePlayer.spring = false;
thePlayer.rocket = false;
thePlayer.normal = false;
break;
}
if (event.key.code == sf::Keyboard::R)
{
thePlayer.car = false;
thePlayer.spring = false;
thePlayer.rocket = true;
thePlayer.normal = false;
break;
}
if (event.key.code == sf::Keyboard::S)
{
thePlayer.car = false;
thePlayer.spring = true;
thePlayer.rocket = false;
thePlayer.normal = false;
break;
}

}

}



if (thePlayer.normal)
{
velocity.x = 0;
thePlayer.normalSprite.move(velocity.x, velocity.y);
if (thePlayer.normalSprite.getPosition().y < groundheight)
{
velocity.y += gravity;
}
else
{
thePlayer.normalSprite.setPosition(thePlayer.normalSprite.getPosition().x, groundheight);
velocity.y = 0;
}

}

if (thePlayer.car)
{
thePlayer.carSprite.move(velocity.x, velocity.y);
if (thePlayer.carSprite.getPosition().y < groundheight)
{
velocity.y += gravity;
}
else
{
thePlayer.carSprite.setPosition(thePlayer.carSprite.getPosition().x, groundheight);
velocity.y = 0;
}

}

window.clear();
if (thePlayer.normal)
{
window.draw(thePlayer.normalSprite);
}
if (thePlayer.car)
{
window.draw(thePlayer.carSprite);
}

window.display();
}
}

非常感谢!

最佳答案

在画车之前你应该调整它的位置到你的玩家位置:

if (thePlayer.car)
{
thePlayer.carSprite.setPosition(thePlayer.normalSprite.getPosition());
window.draw(thePlayer.carSprite);
}

但正如 ractiv 所说,您应该以不同的方式组织代码。我建议你在尝试做这种游戏之前学习更多关于 c++ 的“良好实践”。

关于c++ - SFML 中 getPosition() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51177073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com