OneLoneCoder / olcPixelGameEngine

The official distribution of olcPixelGameEngine, a tool used in javidx9's YouTube videos and projects
Other
3.86k stars 913 forks source link

problem with my platformer project #255

Open planetixin opened 3 years ago

planetixin commented 3 years ago

I've followed the tutorial how to make tile-based platformed and for some reason sprites doesn't want to appear and also when I've changed the engine to olcPixelGameEngine.h the rectangle player changes its size based on its position. also there are some bugs with displaying on the left side of the screen. I don't know how to solve it

`#include

define OLC_PGE_APPLICATION

using namespace std;

include "olcPixelGameEngine.h"

class Platformer : public olc::PixelGameEngine { public: Platformer() { sAppName = "platformer"; }

private: wstring sLevel; int nLevelWidth; int nLevelHeight;

float fPlayerPosX = 0.0f;
float fPlayerPosY = 0.0f;

float fPlayerVelX = 0.0f;
float fPlayerVelY = 0.0f;

float fCameraPosX = 0.0f;
float fCameraPosY = 0.0f;

olc::Sprite* spriteTiles = nullptr;
olc::Sprite* spriteGuy = nullptr;

protected:

virtual bool OnUserCreate() override
{
    nLevelWidth = 32;
    nLevelHeight = 20;
    sLevel += L"................................";
    sLevel += L"................................";
    sLevel += L"................................";
    sLevel += L"................................";
    sLevel += L"................................";
    sLevel += L"................................";
    sLevel += L"#########.....##................";
    sLevel += L"........##..####........#.......";
    sLevel += L"...........##..........##.......";
    sLevel += L"#.######..########..########..##";
    sLevel += L"#.#................##........##.";
    sLevel += L"#.#.....######.#####.......####.";
    sLevel += L"#.#....##....#.#..........##....";
    sLevel += L"#.....##.....#.#.........##.....";
    sLevel += L"##########...#.#........##......";
    sLevel += L".............#.#.......##.......";
    sLevel += L".............#........##........";
    sLevel += L".............###########........";
    sLevel += L"................................";
    sLevel += L"................................";

    spriteTiles = new olc::Sprite("./block.png");
    spriteGuy = new olc::Sprite("block.png");

    return true;
}
virtual bool OnUserUpdate(float fElapsedTime)
{
    //Utility Lambdas
    auto GetTile = [&](int x, int y)
    {
        if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
        {
            return sLevel[y* nLevelWidth + x];
        }
        else
        {
            return L' ';
        }
    };

    auto SetTile = [&](int x, int y, wchar_t c)
    {
        if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
        {
            sLevel[y * nLevelWidth + x] = c;
        }
    };

    fPlayerVelX;
    fPlayerVelY;

    bool moving = false;
    //handle Input
    if (IsFocused())
    {
        if (GetKey(olc::W).bHeld)
        {
            fPlayerVelY += -6.0f * fElapsedTime;
            moving = true;
        }
        if (GetKey(olc::S).bHeld)
        {
            fPlayerVelY += 6.0f * fElapsedTime;
            moving = true;

        }
        if (GetKey(olc::D).bHeld)
        {
            fPlayerVelX += 6.0f * fElapsedTime;
            moving = true;

        }
        if (GetKey(olc::A).bHeld)
        {
            fPlayerVelX += -6.0f * fElapsedTime;
            moving = true;

        }

    }
    if (moving == false)
    {
        fPlayerVelX += - 3.0 * fPlayerVelX * fElapsedTime;
        fPlayerVelY += -3.0 * fPlayerVelY * fElapsedTime;
        if(fabs(fPlayerPosX) < 0.10f)
        {
            fPlayerPosX = 0.0f;
        }
        if (fabs(fPlayerPosY) < 0.10f)
        {
            fPlayerPosY = 0.0f;
        }
    }

    float fNewPlayerPosX = fPlayerPosX + fPlayerVelX * fElapsedTime;
    float fNewPlayerPosY = fPlayerPosY + fPlayerVelY * fElapsedTime;

    // collision
    if (fPlayerVelX <= 0)//horizontal
    {
        if (GetTile(fNewPlayerPosX + 0.0f, fPlayerPosY + 0.0f) != L'.' || GetTile(fNewPlayerPosX + 0.0f, fPlayerPosY + 0.9f) != L'.')
        {
            fNewPlayerPosX = (int)fNewPlayerPosX +  1;
            fPlayerVelX = 0;
        }
    }else
    {
        if (GetTile(fNewPlayerPosX + 1.0f, fPlayerPosY + 0.0f) != L'.' || GetTile(fNewPlayerPosX + 1.0f, fPlayerPosY + 0.9f) != L'.')
        {
            fNewPlayerPosX = (int)fNewPlayerPosX;
            fPlayerVelX = 0;
        }
    }

    if (fPlayerVelY <= 0)//vertical
    {
        if (GetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY + 0.0f) != L'.' || GetTile(fNewPlayerPosX + 0.9f, fNewPlayerPosY + 0.0f) != L'.')
        {
            fNewPlayerPosY = (int)fNewPlayerPosY + 1;
            fPlayerVelY = 0;
        }
    }
    else
    {
        if (GetTile(fNewPlayerPosX + 0.0f, fNewPlayerPosY + 1.0f) != L'.' || GetTile(fNewPlayerPosX + 0.9f, fNewPlayerPosY + 1.0f) != L'.')
        {
            fNewPlayerPosY = (int)fNewPlayerPosY;
            fPlayerVelY = 0;
        }
    }

    fPlayerPosX = fNewPlayerPosX;
    fPlayerPosY = fNewPlayerPosY;

    fCameraPosX = fPlayerPosX;
    fCameraPosY = fPlayerPosY;

    //draw level
    int nTileWidth = 16;
    int nTileHeight = 16;
    int nVisibleTilesX = ScreenWidth() / nTileWidth;
    int nVisibleTilesY = ScreenHeight() / nTileHeight;

    float fOffsetX = fCameraPosX - (float)nVisibleTilesX / 2.0f;
    float fOffsetY = fCameraPosY - (float)nVisibleTilesY / 2.0f;

    if (fOffsetX < 0) fOffsetX = 0;
    if (fOffsetY < 0) fOffsetY = 0;
    if (fOffsetX > nLevelWidth - nVisibleTilesX) fOffsetX = nLevelWidth - nVisibleTilesX;
    if (fOffsetY > nLevelHeight - nVisibleTilesY) fOffsetY = nLevelHeight - nVisibleTilesY;

    float fTileOffsetX = (fOffsetX - (int)fOffsetX) * nTileWidth;
    float fTileOffsetY = (fOffsetY - (int)fOffsetY) * nTileHeight;

    for (int x = -1; x < nVisibleTilesX + 2; x++)
    {
        for (int y = -1; y < nVisibleTilesY + 2; y++)
        {
            wchar_t sTileID = GetTile(x + fOffsetX, y + fOffsetY);
            switch (sTileID)
            {
                case L'.':
                    FillRect(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, (x + 1) * nTileWidth - fTileOffsetX, (y + 1) * nTileHeight - fTileOffsetY, olc::BLUE);
                    break;
                case L'#':
                    //FillRect(x * nTileWidth - fTileOffsetX, y * nTileHeight - fTileOffsetY, (x + 1) * nTileWidth - fTileOffsetX, (y + 1) * nTileHeight - fTileOffsetY, olc::WHITE);

                    //DrawPartialSprite(x* nTileWidth - fTileOffsetX, y* nTileHeight - fTileOffsetY, spriteTiles, 2 * nTileWidth, 0 * nTileHeight, nTileWidth, nTileHeight);
                    break;
                default:
                    break;
            }
        }
    }

    FillRect((fPlayerPosX - fOffsetX) * nTileWidth, (fPlayerPosY - fOffsetY) * nTileHeight, (fPlayerPosX - fOffsetX + 1.0f) * nTileWidth, (fPlayerPosY - fOffsetY + 1.0f) * nTileHeight, olc::GREEN);
    //DrawPartialSprite((fPlayerPosX - fOffsetX)* nTileWidth, (fPlayerPosY - fOffsetY)* nTileWidth, spriteGuy, nTileWidth, nTileHeight, nTileWidth, nTileHeight);
    //DrawLine((fPlayerPosX - fOffsetX) * nTileWidth, (fPlayerPosY - fOffsetY) * nTileWidth, (fPlayerPosX - fOffsetX + 1.0f) * nTileWidth, (fPlayerPosY - fOffsetY + 1.0f) * nTileHeight, PIXEL_SOLID, FG_GREEN);
    //FillCircle((fPlayerPosX + 0.5 - fOffsetX) * nTileWidth, (fPlayerPosY - 0.5 - fOffsetY) * nTileWidth, nTileWidth/2, PIXEL_SOLID, FG_GREEN);

    return true;
}

};

int main() { Platformer game; if (game.Construct(256, 200, 4, 4)) game.Start();

return 0;

} `

justinrichardsmusic commented 3 years ago

FillRect((fPlayerPosX - fOffsetX) nTileWidth, (fPlayerPosY - fOffsetY) nTileHeight, nTileWidth, nTileHeight, olc::GREEN);

Your rectangle width and height just need to be tileWidth and tileHeight, no need to multiply with position etc

Also when you load your sprite images in OnUserCreate one of them has a relative path and the other one has no path The sprites need to be in the same folder as your cpp file generally

planetixin commented 3 years ago

now it's working path wasn't wrong but ox and oy was wrong. also I have some glitches on upper and left part of the screen

planetixin commented 3 years ago

obraz

planetixin commented 3 years ago

obraz

justinrichardsmusic commented 3 years ago

Send me messages on the discord please 👍🏼

On Tue, 17 Aug 2021 at 9:16 pm, planetixin @.***> wrote:

[image: obraz] https://user-images.githubusercontent.com/47472488/129716860-f8ecbfbe-a225-4d23-af46-e2d9ae0c6f5d.PNG

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/OneLoneCoder/olcPixelGameEngine/issues/255#issuecomment-900208888, or unsubscribe https://github.com/notifications/unsubscribe-auth/APZQCXZNLR5O7W2Q7TOIBHLT5JAKRANCNFSM5CJLO55Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .