Hertog is now in the game dear god

main
Johannes Hendrik Gerard van der Weide 2023-10-12 13:16:18 +02:00
parent 59560ced66
commit fce1de6303
2 changed files with 19 additions and 25 deletions

View File

@ -12,7 +12,8 @@ class Game
private: private:
sf::RenderWindow mWindow; sf::RenderWindow mWindow;
sf::CircleShape mPlayer; sf::Texture mTexture;
sf::Sprite mPlayer;
bool mIsMovingUp{false}; bool mIsMovingUp{false};
bool mIsMovingDown{false}; bool mIsMovingDown{false};

View File

@ -6,22 +6,22 @@ const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game() Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application") : mWindow(sf::VideoMode(640, 480), "SFML Application")
, mPlayer() , mTexture()
{ , mPlayer() {
mPlayer.setRadius(40.f); if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
mPlayer.setPosition(100.f, 100.f); // Handle loading errors
mPlayer.setFillColor(sf::Color::Cyan);
} }
void Game::run() mPlayer.setTexture(mTexture);
{ mPlayer.setScale(0.25f, 0.25f);
mPlayer.setPosition(100.f, 100.f);
}
void Game::run() {
sf::Clock clock; sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero; sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen()) while (mWindow.isOpen()) {
{
processEvents(); processEvents();
timeSinceLastUpdate += clock.restart(); timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame) while (timeSinceLastUpdate > TimePerFrame) {
{
timeSinceLastUpdate -= TimePerFrame; timeSinceLastUpdate -= TimePerFrame;
processEvents(); processEvents();
update(TimePerFrame); update(TimePerFrame);
@ -30,13 +30,10 @@ void Game::run()
} }
} }
void Game::processEvents() void Game::processEvents() {
{
sf::Event event; sf::Event event;
while (mWindow.pollEvent(event)) while (mWindow.pollEvent(event)) {
{ switch (event.type) {
switch (event.type)
{
//For each time the while loop iterates, it means a new event that was registered by the window is being handled. While there can be many different events, we will only check for some types of events, which are of our interest right now. //For each time the while loop iterates, it means a new event that was registered by the window is being handled. While there can be many different events, we will only check for some types of events, which are of our interest right now.
case sf::Event::KeyPressed: case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true); handlePlayerInput(event.key.code, true);
@ -51,8 +48,7 @@ void Game::processEvents()
} }
} }
void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
{
if (key == sf::Keyboard::W) if (key == sf::Keyboard::W)
mIsMovingUp = isPressed; mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S) else if (key == sf::Keyboard::S)
@ -63,8 +59,7 @@ void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed)
mIsMovingRight = isPressed; mIsMovingRight = isPressed;
} }
void Game::update(sf::Time deltaTime) void Game::update(sf::Time deltaTime) {
{
sf::Vector2f movement(0.f, 0.f); sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp) if (mIsMovingUp)
movement.y -= 100.f; movement.y -= 100.f;
@ -78,15 +73,13 @@ void Game::update(sf::Time deltaTime)
mPlayer.move(movement * deltaTime.asSeconds()); mPlayer.move(movement * deltaTime.asSeconds());
} }
void Game::render() void Game::render() {
{
mWindow.clear(); mWindow.clear();
mWindow.draw(mPlayer); mWindow.draw(mPlayer);
mWindow.display(); mWindow.display();
} }
int main() int main() {
{
Game game; Game game;
game.run(); game.run();
} }