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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - FellowCoder

Pages: [1]
1
Graphics / emoji as output
« on: January 25, 2020, 02:15:46 am »
 ::)I was trying to check the sf::RectangleShape color using std::cout<<map.blocos.getFillColor().r<<std::endl; and the output is just "☺", can someone explain me how this happened and what i have to do to get the shape color?

Edit: i specifically want the color alpha, i was just trying new things and this happened. Also the RGB of the shape is (1, 0, 0)

2
General / Re: Direction of collision
« on: January 20, 2020, 02:14:01 am »
This method would work if the map was just a rectangle with nothing inside, but wouldn´t work in a platform game like Mario

The ideia I have is to get the first y pixels of every rectangle and the variable isAbleToJump will be true once the player collides with this pixels... so when the player collides in the side of a rectangle isAbleToJump will continue beeing false( isAbleToJump will be false after the player jumps)

But I still think that this can be made easier, do you have any other idea?


3
General / Direction of collision
« on: January 17, 2020, 08:02:44 am »
So, in my project i used "Sprite.getGlobalBounds().intersects(sprite2.getGlobalBounds())" to see if the player was collinding with the floor, beeing able to jump again or not, but if it collides with the wall it will be able to jump again, so how can i make it to be able to identify the direction of the collisions?


Also i am not using perfect squares as walls or floors in this so i cant just subtract the position to know the direction...

4
General / Segmentation Fault
« on: January 10, 2020, 06:13:34 am »
So, i am making a 2d "game" on sfml and for the first time it gave me a error called  Segmentation Fault. When i run my code the window just opens and close quickly and the console say this error to me, here is my code:


main.cpp:

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include "Player.h"
#include <string>

int main(){
        sf::RenderWindow janela(sf::VideoMode::getDesktopMode() , "titulo", sf::Style::Default);


        Player jogador;

        float tempo = 0.0f;
        sf::Clock clock;

        while(janela.isOpen()){
                tempo = clock.restart().asSeconds();

                sf::Event e;
                while(janela.pollEvent(e)){
                        if(e.type == sf::Event::Closed){
                                janela.close();}


                }


                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                        jogador.corpo.move(0.1f,0);
                        jogador.walking = true;
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                        jogador.corpo.move(-0.1f,0);
                        jogador.walking = true;
                }

                jogador.Anim(tempo);

                janela.clear(sf::Color(150, 150, 150));
                janela.draw(jogador.corpo);
                janela.display();

        }







        return 0;
}
 


Player.h:

#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>

class Player{
  public:
    Player();
    ~Player();
    sf::Sprite corpo;
    bool walking;
    std::vector<sf::Texture> texturas;


    void Anim(float tempo);


  private:
    int textura;
    float tempoTotal;
    int i;



};


#endif /* end of include guard:  */
 

Player.cpp

#include "Player.h"
#include <iostream>

Player::Player()
{
  textura = 0;
  corpo.setTexture(texturas[0]);
  corpo.setPosition(100.f, 100.f);
  corpo.setOrigin(250,250);
  corpo.setScale(0.2f,0.2f);

  tempoTotal = 0.0f;

  walking = false;
  texturas.reserve(10);

  for (i=0; i < texturas.size(); i++) {
    texturas[i].loadFromFile("Idle (" + std::to_string(i+1) + ").png");
    texturas[i].setSmooth(true);
  }

}

Player::~Player()
{

}

void Player::Anim(float tempo){
  tempoTotal += tempo;
  if (!walking){
    if(tempoTotal >= 0.1f){
      std::cout<<textura<<std::endl;
      tempoTotal = 0;
      textura ++;
      if(textura == texturas.size()){
        textura = 0; }

      corpo.setTexture(texturas[textura]);

    }
  }
  else{
    if(tempoTotal >= 0.3f){
      walking = false;
    }
  }
}
 

i will be really thankful if someone can help me, i am struggling with this error for a long time...



Sorry for my bad english btw :-[

5
Graphics / White rectangle issue
« on: December 27, 2019, 03:16:03 am »
Can someone explain me why is my code is outputitng an white rectangle instead of the properly texture?

Sorry for my bad english btw :-[

main.cpp:

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

int main(){
        sf::RenderWindow janela(sf::VideoMode(512, 512), "titulo", sf::Style::Default);
        sf::Texture texture, tex1, tex2, tex3, tex4;

        texture.loadFromFile("Idle (1).png");
        tex1.loadFromFile("Idle (2).png");
        tex2.loadFromFile("Idle (3).png");
        tex3.loadFromFile("Idle (4).png");
        tex4.loadFromFile("Idle (5).png");

        std::vector<sf::Texture> texturas;
        texturas.push_back(texture);texturas.push_back(tex1);texturas.push_back(tex2);texturas.push_back(tex3);texturas.push_back(tex4);




        Player jogador(texturas);
        //jogador.corpo.setTexture(texture);

        sf::Clock clock;
        sf::Time tempo;


        while(janela.isOpen()){
                sf::Event e;
                while(janela.pollEvent(e)){
                        if(e.type == sf::Event::Closed){
                                janela.close();}


                }


                tempo = clock.getElapsedTime();
                if(tempo.asSeconds() <=20){
                        std::cout<<tempo.asSeconds()<<std::endl;
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                        jogador.corpo.move(0.1f,0);
                        std::cout<<jogador.corpo.getPosition().x;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                        jogador.corpo.move(-0.1f,0);
                        std::cout<<jogador.corpo.getPosition().x;
                }
                if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
                        jogador.corpo.setPosition(sf::Mouse::getPosition(janela).x,sf::Mouse::getPosition(janela).y);
                }
                janela.clear(sf::Color(150, 150, 150));
                janela.draw(jogador.corpo);
                janela.display();

        }




        return 0;
}

Player.h

#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>

class Player{
  public:
    Player(std::vector<sf::Texture> texturas);
    ~Player();
    sf::Sprite corpo;


    void Anim();


  private:



};


#endif /* end of include guard:  */

Player.cpp:

#include "Player.h"

Player::Player(std::vector<sf::Texture> texturas)
{
  corpo.setTexture(texturas[0]);
  corpo.setPosition(20.f, 20.f);
  corpo.setOrigin(250,250);
  corpo.setScale(0.1f,0.1f);
}

Player::~Player()
{

}

void Player::Anim(){

}
 


Also i tried using pointers in the textures but i dont think i used it correctly  :-\



6
Graphics / Re: Array of rectangles
« on: July 11, 2019, 01:17:48 am »
So i fixed it by changing it already in the vector how Hapax said but know i am getting a lot of bugs and idk why

Also
(spawnx%static_cast<int>(tamanho)!=0)
makes it a multiple of 10, i need it because the snake walks 10 pixels each time.It was a very nonsense way to do it so i changed and after the change the snake cant even walk right know(Sometimes yes but the code stop after a few seconds)

Here is the code:

#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
#include <typeinfo>
/*
bool checker(std::vector<sf::RectangleShape> snake, sf::RectangleShape maca){
    std::vector<sf::Vector2f> body;
    for(int i = 0;i<snake.size();i++){
        if (snake[i].getPosition() == maca.getPosition());{
            return true;
        }
    }


    return false;
}
*/

int main(){
    srand(time(0));
    sf::Clock clock;

    char mov, ultmov;
    int spawnx, spawny, spawnx1, spawny1;
    float tamanho;

    sf::RenderWindow janela(sf::VideoMode(510, 510), "Snake Game", sf::Style::Close);
    sf::RectangleShape head(sf::Vector2f(10.0f, 10.0f));
    tamanho = head.getSize().x;


    std::vector<int> spawns;
    for(int i=0;i<450;i+=10){
        spawns.push_back(i);}

    spawnx = spawns[rand()%(spawns.size() + 1)];
    spawny = spawns[rand()%(spawns.size() + 1)];
    /*while(spawnx%static_cast<int>(tamanho)!=0){
        spawnx = (rand()%440)+10;}
    while(spawny%static_cast<int>(tamanho)!=0){
        spawny = (rand()%440)+10;}
     */
                           
       

    head.setPosition(spawnx, spawny);

    sf::RectangleShape maca(sf::Vector2f(10.0f, 10.0f));



    do{
        spawnx1 = spawns[rand()%(spawns.size() + 1)];
        spawny1 = spawns[rand()%(spawns.size() + 1)];
    }while(spawnx == spawnx1 && spawny ==spawny1);

    /*while(spawnx%static_cast<int>(tamanho)!=0){
        spawnx = (rand()%440)+10;}
    while(spawny%static_cast<int>(tamanho)!=0){
        spawny = (rand()%440)+10;}
     */
                           
       

    maca.setPosition(spawnx1, spawny1);
    maca.setFillColor(sf::Color::Red);

    std::vector<sf::RectangleShape> snake;
    snake.push_back(head);

    std::cout<<head.getPosition().x<<"-"<<head.getPosition().y<<std::endl<<maca.getPosition().x<<"-"<<maca.getPosition().y<<std::endl;

    while(janela.isOpen()){
        sf::Event e;
        while(janela.pollEvent(e)){
            if(e.type == sf::Event::Closed){
                janela.close();

            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)&& ultmov!='s'){
            mov = 'w';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)&& ultmov!='d'){
            mov = 'a';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)&& ultmov!='w'){
            mov = 's';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) && ultmov!='a'){
            mov = 'd';}



        sf::Time elapsed1 = clock.getElapsedTime();
        if (elapsed1.asMilliseconds() == 100){
            std::cout<<elapsed1.asMilliseconds() <<std::endl;
            if (snake.size()>1){
                for(int i=snake.size();0!=i;i--){
                    snake[i].setPosition(snake[i-1].getPosition());} //posiciona a cauda
            }

            if(mov =='w'){
                snake[0].move(0.0f, -10.0f);}
            if(mov =='a'){
                snake[0].move(-10.0f, 0.0f);}
            if(mov =='s'){
                snake[0].move(0.0f, 10.0f);}
            if(mov =='d'){
                snake[0].move(10.0f, 0.0f);}


            for(int i=3;i<snake.size();i++){
                if (snake[0].getPosition() == snake[i].getPosition()){ //checa colisoes
                    janela.close();
                    std::cout<<"you died"<<std::endl;}

            }


            ultmov = mov;
            if(snake[0].getPosition() == maca.getPosition()){
                sf::RectangleShape corpo(sf::Vector2f(10.0f, 10.0f)); //spawna a nova cauda
                corpo.setPosition(snake[snake.size()-1].getPosition());
                snake.push_back(corpo);
                std::cout<<snake.size() - 1 <<std::endl;


                spawnx1 = spawns[rand()%(spawns.size() + 1)];
                spawny1 = spawns[rand()%(spawns.size() + 1)];
               
                /*while(spawnx%static_cast<int>(tamanho)!=0){
                    spawnx = (rand()%440)+10;}
                while(spawny%static_cast<int>(tamanho)!=0){
                    spawny = (rand()%440)+10;}
                 */
                                                                                                   
                               

                maca.setPosition(spawnx1, spawny1); //acha uma nova posicao vazia pra maca

                }


            clock.restart();
        }


        janela.clear(sf::Color(150, 150, 150));
        janela.draw(maca);
        for(int i=0;i<snake.size();i++){
            janela.draw(snake[i]);}
        janela.display();
    }


    return 0;
}

7
Graphics / Re: Array of rectangles
« on: July 08, 2019, 07:38:23 pm »
I know its a copy but how can i do to when i change the var that is not in the vector it changes the same var that is in the vector?

Re:Also: I couldn't find the C++ forum, is it General? I used & and % prorpely tho... 

8
Graphics / Array of rectangles
« on: July 08, 2019, 03:02:54 am »
I am trying to create an array of sf::RectangleShape's. I create one but if i change something in the rec after throwing it inside my array(vector) the one inside the array wont change. Can someone help me to get the array to have on live objects

Here is my code(you can see that there is a rectangle that dont move and one that dont move, the one that does not move is the one in the array and the other one is the same Rect but not in the array)

#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>


int main(){
    srand(time(0));
    sf::Clock clock;

    char mov, ultmov;
    int spawnx, spawny, spawnx1, spawny1;
    float tamanho;

    sf::RenderWindow janela(sf::VideoMode(510, 510), "Snake Game", sf::Style::Close);
    sf::RectangleShape head(sf::Vector2f(10.0f, 10.0f));
    tamanho = head.getSize().x;

    while(spawnx%static_cast<int>(tamanho)!=0){
        spawnx = rand()%450;}
    while(spawny%static_cast<int>(tamanho)!=0){
        spawny = rand()%450;}

    head.setPosition(spawnx, spawny);

    sf::RectangleShape maca(sf::Vector2f(10.0f, 10.0f));

    loop:
    while(spawnx1%static_cast<int>(tamanho)!=0){
        spawnx1 = rand()%450;}
    while(spawny1%static_cast<int>(tamanho)!=0){
        spawny1 = rand()%450;}

    if (spawnx == spawnx1 && spawny ==spawny1){
        goto loop;}


    maca.setPosition(spawnx1, spawny1);
    maca.setFillColor(sf::Color::Red);

    std::vector<sf::RectangleShape> snake(200);
    snake.push_back(head);

    while(janela.isOpen()){
        sf::Event e;
        while(janela.pollEvent(e)){
            if(e.type == sf::Event::Closed){
                janela.close();

            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)&& ultmov!='s'){
            mov = 'w';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)&& ultmov!='d'){
            mov = 'a';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)&& ultmov!='w'){
            mov = 's';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) && ultmov!='a'){
            mov = 'd';}



        sf::Time elapsed1 = clock.getElapsedTime();
        if (elapsed1.asMilliseconds() >= 100){
            //std::cout<<"Funcao" <<std::endl;
            if(mov =='w'){
                head.move(0.0f, -10.0f);}
            if(mov =='a'){
                head.move(-10.0f, 0.0f);}
            if(mov =='s'){
                head.move(0.0f, 10.0f);}
            if(mov =='d'){
                head.move(10.0f, 0.0f);}
            ultmov = mov;
            clock.restart();}



        janela.clear(sf::Color(150, 150, 150));
        janela.draw(maca);
        for(int i=0;i<snake.size();i++){
            janela.draw(snake[i]);}
        janela.draw(head);
        janela.display();
    }






    return 0;
}

Sorry for my bad english btw

Pages: [1]
anything