所以我已经为pong克隆编写了代码,我正在努力让paddles停止超出窗口界限。继承人我正在使用的代码
if (this->getPosition().y < 0)
{
this->move(0, 1.0f);
}
else if (this->getPosition().y + this->getGlobalBounds().height > 600);
{
this->move(0, -1.0f);
}
不,当我这样做时,桨不会向底部移动,但会向顶部移动并离开屏幕。当我注释掉这个 - >移动(0,-1.0f)时,拨片将停在屏幕顶部,但会从屏幕底部移开。继承头文件:
#pragma once
#include "entity.h"
class paddle_player : public Entity
{
public:
paddle_player(int playerNumber)
{
this->playerNumber = playerNumber;
switch (this->playerNumber)
{
case 0:
this->Load("paddle1.png");
break;
default:
this->Load("paddle2.png");
break;
}
}
void Update()
{
switch (this->playerNumber)
{
case 0:
this->velocity.y = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S) - sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W);
break;
default:
this->velocity.y = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down) - sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up);
break;
}
Entity::Update();
if (this->getPosition().y < 0)
{
this->move(0, 1.0f);
}
else if (this->getPosition().y + this->getGlobalBounds().height > 600);
{
this->move(0, -1.0f);
}
}
private:
int playerNumber;
};
我不知道还有什么要包括但请帮忙!