1

I am trying to make a program so that a red block falls from the top of the screen and the player has to avoid it. When I run the program I can see and move the player(paddle) but I can't see the enemy or "thing" sprite. Please help me because I have tried everything with the code and it still doesn't work :/

things.h

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
#pragma once

class Thing
{
public:
void thingspawn(RenderWindow &gameDisplay, int &enemystartx, int &enemystarty, int &enemywidth, int &enemyheight, int &blockcolor)
{
    RectangleShape thing(Vector2f(enemywidth, enemyheight));
    thing.setFillColor(Color(blockcolor));
    thing.setPosition(enemystartx, enemystarty);
    gameDisplay.clear();
    gameDisplay.draw(thing);



}
};

main.cpp

#include <iostream>
#include <SFML/Graphics.hpp>
#include "things.h"
using namespace std;
using namespace sf;

int main()
{
RenderWindow gameDisplay(VideoMode(1366, 768), "Game", Style::Fullscreen);
gameDisplay.setMouseCursorVisible(false);
gameDisplay.clear();
int enemystarty = -200;
int enemystartx = 300;
int enemyheight = 100;
int enemywidth = 100;
int enemyspeed = 0.3f;
int enemycount = 1;
int dodged = 0;
int blockcolor = (255, 0, 0);

RectangleShape player(Vector2f(300, 30));
player.setFillColor(Color(0, 0, 255));
player.setPosition(400, 728);


while (gameDisplay.isOpen())
{
    Event evnt;
    while (gameDisplay.pollEvent(evnt))
    {
        switch (evnt.type)
        {
        case Event::Closed:
            gameDisplay.close();
        case Event::KeyPressed:
            if (Keyboard::isKeyPressed(Keyboard::Q))
                gameDisplay.close();

        }
    }


    if (Keyboard::isKeyPressed(Keyboard::Right))
        if (player.getPosition().x < 1000)
            player.move(0.2f, 0.0f);
    if (Keyboard::isKeyPressed(Keyboard::Left))
        if (player.getPosition().x > 50)
            player.move(-0.2f, 0.0f);

    Thing thingobject;
    thingobject.thingspawn(gameDisplay, enemystartx, enemystarty, enemywidth, enemyheight, blockcolor);

    enemystarty += enemyspeed;


    gameDisplay.draw(player);
    gameDisplay.display();


}

}

Joseph H
  • 15
  • 3

2 Answers2

0

The problem is that you create RectangleShape thing in function Thing::thingspawn(...) and it is destroyed after it ends. Try to put declaration of it as member of the Thing class

capi1500
  • 115
  • 1
  • 6
0

The thing is rendering correctly but in just a bad habit way. like what capi1500 said,

But the real problem is that int blockcolor = (255, 0, 0); results a black color when you pass it to the sf::Color. you wont see a black drawing in a black canvas right?

You can see here on how to construct a color from an integer: How to pack ARGB to one integer uniquely?

for now change the color of the thing using the predefined colors of sf::Color like sf::Color::Green or pass each component using the another constructor Color (Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha=255)

SFML's sf::Color's documentation: http://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Color.php

Community
  • 1
  • 1
Lorence Hernandez
  • 1,189
  • 12
  • 23