播放器未显示在 [C++/SFML] 的 window 中

The player is not showing in the window in [C++/SFML]

我正在使用 SFML 用 C++ 编写一个游戏。我为玩家、最终目标和网格编写了代码,但是当游戏开始时,控制台显示玩家的坐标正在变化,但玩家本身没有显示或显示在 window 中。任何人都可以帮忙吗?游戏也是关于在网格内移动的玩家。当玩家与 End Goal 发生碰撞时(这里这意味着玩家在同一个网格中)。

main.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/System/Clock.hpp>
#include "Player.h"

float tileSize = 25.f;
sf::RectangleShape endGoal(sf::Vector2f(tileSize - tileSize / 10, tileSize - tileSize / 10));

Player player(tileSize);

int main()
{
    sf::Clock clock;

    
    sf::RenderWindow window(sf::VideoMode(1080, 720), "Player & End Goal -  Grid Game");
    window.setVerticalSyncEnabled(true);


    
    endGoal.setFillColor(sf::Color(0, 185, 0, 255));
    endGoal.setOutlineColor(sf::Color(0, 185, 0, 255));
    endGoal.setOutlineThickness(tileSize / 10);
    endGoal.setPosition(tileSize * 20, tileSize * 15);

    /////// Game Loop //////
    while (window.isOpen())
    {
        float dt = clock.restart().asSeconds();
        sf::Event evnt;
        while (window.pollEvent(evnt))
        {
            switch (evnt.type)
            {
            case sf::Event::Closed:
                std::cout << "Window has been closed." << std::endl;
                window.close(); // closes the window when the 'X' button gets pressed
            }
        }
        
        //Player Movement

        player.handleMovement();

        if (player.isPlayerColliding(endGoal)) {
            std::cout << "Player is colliding with End Goal.." << std::endl;
        }
        else {
            std::cout << "X" << std::endl;
        }

        window.clear(sf::Color::White);
        //Drawing The Grid
        for (int i = 0; i <= window.getSize().x / tileSize; i++) {
            
            for (int j = 0; j <= window.getSize().y / tileSize; j++) {
                sf::RectangleShape gridTile(sf::Vector2f(20.f, 20.f));
                gridTile.setSize(sf::Vector2f((float)tileSize, (float)tileSize));
                gridTile.setFillColor(sf::Color(210, 210, 210, 255));
                gridTile.setOutlineColor(sf::Color(155, 155, 155, 255));
                gridTile.setOutlineThickness(2.5f);
                gridTile.setPosition(sf::Vector2f(i * gridTile.getSize().x, j * gridTile.getSize().y));
                
                window.draw(gridTile);
            }
            
        }
        

        
        std::cout << " Player X: " << player.getPosition().x / tileSize<< "  Y: " << player.getPosition().y / tileSize << " EndGoal X: " << endGoal.getPosition().x / tileSize << "  Y: " << endGoal.getPosition().y  / tileSize << std::endl;


        window.draw(endGoal);
        player.draw(window);
        window.display();
        
    }

    return 0;
}



Player.h

#pragma once
#include <SFML/Graphics.hpp>

class Player {
public:
    Player(float size) {
        gridSize = size;
        player.setFillColor(sf::Color::Black);
        player.setOutlineThickness(gridSize / 10);
        player.setOutlineColor(sf::Color::Black);
        player.setPosition(sf::Vector2f(gridSize, gridSize));
    }

    void draw(sf::RenderWindow &renderWindow) {

        renderWindow.draw(player);
    }

    void handleMovement() {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { player.move(0, -gridSize); }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) { player.move(0, gridSize); }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { player.move(-gridSize, 0); }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { player.move(gridSize, 0); }
    }
    bool isPlayerColliding(sf::RectangleShape &collider) {
        if (player.getPosition() == collider.getPosition())
            return true;
        else
            return false;
    }

    sf::Vector2f getPosition() { return player.getPosition(); }
    sf::Vector2f getSize() { return player.getSize(); }

private:
    float gridSize;
    sf::RectangleShape player;
};

播放器正在渲染,但它的大小是 (0, 0)。

在其构造函数中设置播放器大小。

这可能看起来像:

player.setSize(sf::Vector2f(size, size));