当您按住空格键3秒钟时,我想给我的Sprite额外的速度。之后,您应该等待10秒,然后再次使用。 我尝试使用SFML的时间,但是程序启动时时钟立即开始,所以无论如何都会开始。...
简短的问题:如何在不像Sleep()那样冻结程序的情况下延迟功能。
代码(仅适用于开发进度缓慢的控制台):
#include "SFML/Graphics.hpp"
#include <iostream>
#include <Windows.h>
int main()
{
using namespace std;
sf::Clock ClockSpeedFunc, Cooldown;
ClockSpeedFunc.restart();
bool Timer, CooldownTimer = false;
sf::RenderWindow window(sf::VideoMode(600, 600), "SFML WORK!");
sf::Texture texture;
if (!texture.loadFromFile("PlayerTexture.jpg"))
{
std::cout << "Error loading PlayerTexture.jpg" << std::endl;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(sf::Vector2f(50, 40));
sprite.setPosition(sf::Vector2f(50, 40));
sprite.setPosition(sf::Vector2f(50, 40));
sprite.setPosition(sf::Vector2f(50, 40));
float PlayerSpeed = 0.1;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
//###Controls ::
//If W is pressed, move upwards
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
sprite.move(sf::Vector2f(0, -PlayerSpeed));
}
//If A is pressed, move left
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
sprite.move(sf::Vector2f(-PlayerSpeed, 0));
}
//If S is pressed, move downwards
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
sprite.move(sf::Vector2f(0, PlayerSpeed));
}
//If D is pressed, move right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
sprite.move(sf::Vector2f(PlayerSpeed, 0));
}
//If Space is pressed, set Timer to true and set Speed to 0.5
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && CooldownTimer == false) {
cout << "Turbo started" << endl;
Timer = true;
CooldownTimer == false;
PlayerSpeed = 0.5;
}
//If Timer = true, start a clock
if (Timer = true) {
sf::Clock ClockSpeedFunc;
}
//aafter 3 Seconds set Playerspeed back to 0.1
//Set Cooldowntimer to true and restart timer clock.
sf::Time elapsed1 = ClockSpeedFunc.getElapsedTime();
sf::Time elapsed2 = Cooldown.getElapsedTime();
if (elapsed1.asSeconds() >= 3) {
PlayerSpeed = 0.1;
cout << "Turbo ended." << endl;
ClockSpeedFunc.restart();
CooldownTimer = true;
}if (CooldownTimer == true) {
cout << "Cooldown started" << endl;
sf::Clock Cooldown;
}if (elapsed2.asSeconds() >= 10) {
cout << "Cooldown ended" << endl;
Cooldown.restart();
CooldownTimer = false;
}
//Draw everything
window.clear();
window.draw(sprite);
window.display();
}
}`
答案 0 :(得分:0)
首先,请确保您不使用“使用命名空间”。那是个坏习惯。以及为什么要在其中包含#include。我不需要它。
这不是问题的一部分,但我强烈建议您查看https://gafferongames.com/post/fix_your_timestep/并管理您的增量时间。
我想我会为此使用一个类。但是在这里您可以看到如何将其与状态结合使用。我在代码中添加了一些注释,以便您可以看到我的工作。
#include "SFML/Graphics.hpp"
#include <iostream>
// Define the different states of the player
enum class m_State{
POWERUP, NORMAL, COOLDOWN
};
int main(){
sf::Clock m_Clock;
// Record elapsed time of power up
float m_SecondsSincePowerUp;
// Set initial state
m_State m_State = m_State::NORMAL;
sf::RenderWindow window(sf::VideoMode(600, 600), "SFML WORK!");
sf::Texture texture;
if (!texture.loadFromFile("PlayerTexture.jpg")){
std::cout << "Error loading PlayerTexture.jpg" << std::endl;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(sf::Vector2f(50, 40));
sprite.setPosition(sf::Vector2f(50, 40));
sprite.setPosition(sf::Vector2f(50, 40));
sprite.setPosition(sf::Vector2f(50, 40));
float PlayerSpeed = 0.1;
while (window.isOpen()){
sf::Time elapsedTime = m_Clock.restart();
sf::Event event;
while (window.pollEvent(event)){
switch (event.type){
case sf::Event::Closed:
window.close();
break;
}
}
//###Controls ::
//If W is pressed, move upwards
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
sprite.move(sf::Vector2f(0, -PlayerSpeed));
}
//If A is pressed, move left
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
sprite.move(sf::Vector2f(-PlayerSpeed, 0));
}
//If S is pressed, move downwards
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
sprite.move(sf::Vector2f(0, PlayerSpeed));
}
//If D is pressed, move right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
sprite.move(sf::Vector2f(PlayerSpeed, 0));
}
//If Space is pressed, set Timer to true and set Speed to 0.5
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && (m_State == m_State::NORMAL)){
std::cout << "Turbo started" << std::endl;
// Set state to PowerUp
m_State = m_State::POWERUP;
PlayerSpeed = 0.5;
}
// If state is PowerUp count the seconds in a variable
if (m_State == m_State::POWERUP){
// Add elapsed time in seconds
m_SecondsSincePowerUp += elapsedTime.asSeconds();
// After 3 seconds switch the state to Cooldown
if (m_SecondsSincePowerUp > 3.0f){
std::cout << "Turbo ended." << std::endl;
m_State = m_State::COOLDOWN;
PlayerSpeed = 0.1;
}
}
// After 10 seconds switch to normal
if (m_State == m_State::COOLDOWN){
m_SecondsSincePowerUp += elapsedTime.asSeconds();
if (m_SecondsSincePowerUp > 10.0f){
std::cout << "Cooldown ended." << std::endl;
m_State = m_State::NORMAL;
m_SecondsSincePowerUp = 0;
}
}
//Draw everything
window.clear();
window.draw(sprite);
window.display();
}
}
答案 1 :(得分:-1)
您可以使用线程创建睡眠,而普通代码继续一致地继续执行。对于完整的解释来说,它们非常复杂,但请查看示例,看看是否值得尝试将其实现。