Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can't display certain sprites  (Read 2427 times)

0 Members and 1 Guest are viewing this topic.

Skarpetkowa

  • Newbie
  • *
  • Posts: 1
    • View Profile
Can't display certain sprites
« on: June 16, 2020, 03:13:07 pm »
Good afternoon, I'm completely new to sfml and probably my problem is due to some silly mistake, actually.

I tried making a game where the player sprite moves left and right and tries to catch items falling down from the 'ceiling'. I've written much code and lots of files and currently I'm stuck at the point where the player sprite moves as supposed but the falling items wouldn't display. I store the items in a list. Apparently the list's size is always 0 despite me pushing items into the list. I need to complete the project asap. Thank you in advance for your help.

My code is:

(file flowers.h)
#pragma once
#include "stdafx.h" // most includes are here
#include "flower.h"

using namespace std;
class flowers
{
    public:
    vector<flower> floowers; // flower is a class defined and implemented in other files but I'm perfectly sure this class works just fine
    int points;
    void clearUnused(); // I don't use this function yet so it's implementation is empty
    sf::Font foont;
public:
    flowers();
    ~flowers();
    void createNew();
    void moveDown();
    int gainPoints(int x, int y);
    void printPoints(sf::RenderWindow &window);
    void render(sf::RenderWindow &window);
};

file (flowers.cpp)
#include "stdafx.h"
#include "flowers.h"

using namespace std;

void flowers::clearUnused()
{

}

flowers::flowers()
{
    floowers = vector<flower>();
    points = 0;
    foont.loadFromFile("ariendezze.ttf");
}

flowers::~flowers()
{

}

void flowers::createNew()
{
    flower flow;

    floowers.push_back(flow);
}

void flowers::moveDown()
{

    for(auto it = floowers.begin(); it != floowers.end(); it++)
        it->moving(0, 1);

    bool multi = true;

    while (multi)
    {
        multi = false;

        for(auto it = floowers.begin(); it != floowers.end(); it++)
            if (it->GetSprite().getPosition().y>400|| it->getIsDead())
            {
                floowers.erase(it);
                multi = true;
                break;
            }
    }
}

int flowers::gainPoints(int x, int y)
{
    bool multi = true;

    while (multi)
    {
        multi = false;

        for (auto it = floowers.begin(); it != floowers.end(); it++)
            if (it->checkForHit(x, y))
            {
                floowers.erase(it);
                multi = true;
                points += 5;
                break;
            }
    }
}

void flowers::printPoints(sf::RenderWindow &window)
{
    stringstream ss;

    ss << points;
    sf::Text text(ss.str().c_str(), foont, 40);
    text.setPosition(800, 100);

    window.draw(text);
}

void flowers::render(sf::RenderWindow &window)
{

    for (auto it = floowers.begin(); it != floowers.end(); it++)
        window.draw(it->GetSprite());

    printPoints(window);
}

(file Game.h)
#pragma once
#include "stdafx.h"
#include "PlayerPaddle.h"
#include "flowers.h"
#include "MainMenu.h"
#include "SplashScreen.h"

class Game
{

public:
    static void Start();

    static bool IsExiting();
    static void GameLoop();

    static void ShowSplashScreen();
    static void ShowMenu();
    static void moove();

    enum GameState { Uninitialized, ShowingSplash, Paused,
                     ShowingMenu, Playing, Exiting
                   };
    static GameState gameState;
    static sf::RenderWindow mainWindow;
    static PlayerPaddle player1;
    static flowers flowers1;
};

(file Game.cpp)
#include "stdafx.h"
#include "Game.h"

bool flag = true;

using namespace std;

void Game::moove()
{
    if (gameState == Game::Playing)
    {
        mainWindow.clear(sf::Color(sf::Color(80,70,200)));
        flowers1.moveDown();
        flowers1.render(mainWindow);
        player1.Draw(mainWindow);
        mainWindow.display();
    }
}

void Game::Start(void)
{
    srand(time(0));
    if(gameState != Uninitialized)
        return;

    mainWindow.create(sf::VideoMode(1024,768,32),"Gra");

    player1.Load("Images/kiwi.png");
    player1.SetPosition((1024/2)-45,400);

    flowers1 = flowers();

    gameState= Game::ShowingSplash;

    while(!IsExiting())
    {
        GameLoop();
        moove();
    }

    mainWindow.close();
}

bool Game::IsExiting()
{
    if(gameState == Game::Exiting)
        return true;
    else
        return false;
}



void Game::GameLoop()
{
    sf::Event currentEvent;
    int lastTime = clock();
    int posX, posY;
    posX = posY = 0;

    if(mainWindow.pollEvent(currentEvent))
    {
        if (currentEvent.type == sf::Event::Closed)
            mainWindow.close();

        switch(gameState)
        {
        case Game::ShowingMenu:
        {
            ShowMenu();
            break;
        }
        case Game::ShowingSplash:
        {
            ShowSplashScreen();
            break;
        }
        case Game::Playing:
        {

            if (((clock() - lastTime) / (float)CLOCKS_PER_SEC) > 1.0)
            {
                if ((rand() % 101) < 70)
                {
                    flowers1.createNew();
                    lastTime = clock();
                }

                else
                    lastTime += 0.5 * CLOCKS_PER_SEC;
            }

            flowers1.gainPoints(posX, posY);

            if(gameState==Playing)
            {
                if (currentEvent.type == sf::Event::Closed)
                    mainWindow.close();
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                {
                    ShowMenu();
                    gameState = ShowingMenu;
                }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                    player1.mirror(false);
                    mainWindow.clear(sf::Color(80,70,200));
                    player1.moving(-10,0);
                    player1.Draw(mainWindow);
                    flowers1.render(mainWindow);
                    mainWindow.display();
                    posX = player1.GetPosition().x;
                    posY = player1.GetPosition().y;
                }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                    player1.mirror(true);
                    mainWindow.clear(sf::Color(80,70,200));
                    player1.moving(10,0);
                    player1.Draw(mainWindow);
                    flowers1.render(mainWindow);
                    mainWindow.display();
                    posX = player1.GetPosition().x;
                    posY = player1.GetPosition().y;
                }
                else if (currentEvent.type == sf::Event::MouseMoved)
                {
                    player1.moveTo(currentEvent.mouseMove.x);
                    posX = currentEvent.mouseMove.x;
                    posY = 400;
                }
            }
        }
        }
    }
}

void Game::ShowSplashScreen()
{
    SplashScreen splashScreen;
    splashScreen.Show(mainWindow);
    gameState = Game::ShowingMenu;
}

void Game::ShowMenu()
{
    MainMenu mainMenu;
    MainMenu::MenuResult result = mainMenu.Show(mainWindow);
    switch(result)
    {
    case MainMenu::Exit:
        gameState = Game::Exiting;
        break;
    case MainMenu::Play:
        gameState = Game::Playing;
        break;
    }
}


Game::GameState Game::gameState = Uninitialized;
sf::RenderWindow Game::mainWindow;
PlayerPaddle Game::player1;
flowers Game::flowers1;

« Last Edit: June 16, 2020, 04:35:18 pm by eXpl0it3r »