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

Author Topic: Array of rectangles  (Read 579 times)

0 Members and 1 Guest are viewing this topic.

FellowCoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
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
« Last Edit: July 11, 2019, 01:18:58 am by FellowCoder »

FRex

  • Hero Member
  • *****
  • Posts: 1843
    • View Profile
    • My GitHub Page
    • Email
Re: Array of rectangles
« Reply #1 on: July 08, 2019, 03:56:19 am »
std::vector has in it a copy of your original, this is how C++ works and it's different to Java and such.

Also: this isn't C question, you shouldn't use goto in C++, & is binary and - % is modulo.
ZipSavings, script to count rar/7z/zip savings: https://goo.gl/vvBj5M
LuaConsole: https://goo.gl/X4kRUk
FoxRaycaster: https://goo.gl/27nVS8
Small Games - Heart, Routing and Snek: https://goo.gl/15ZGWE https://goo.gl/k5gwWD https://goo.gl/4nKPnT
Botes - a notes app in Pascal: https://goo.gl/bzTqsi

FellowCoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Array of rectangles
« Reply #2 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... 

Hapax

  • Hero Member
  • *****
  • Posts: 3017
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Array of rectangles
« Reply #3 on: July 09, 2019, 12:51:42 am »
This is the SFML forum, not a specifically C++ forum. However, SFML is based in C++ and has multiple language bindings.

I think the point about C and C++ was that you shouldn't be using goto in C++. In your code, it code easily be replaced by a do...while loop.

Your vector has 200 element - all default constructed. When you push to the vector, you're adding another so it'll be 201 after the first one. The first 200 are unused...

What you want to do is slightly ambiguous. If you want to move that piece, move the actual vector element directly, not the original 'template' (head) from which it is copied. If you want to add another piece, push another to the vector. Remember, as FRex said, the vector element is a copy of "head" so changing head does not change the vector.

Your timing is a bit loose. You treat any frame size of 100ms or greater (including 105ms and even 10 second, for example) as the same amount of time passing.

Just for our own nosiness, could you confirm your intention with this condition:
(spawnx%static_cast<int>(tamanho)!=0)
« Last Edit: July 09, 2019, 09:30:37 pm by Hapax »
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

FellowCoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Array of rectangles
« Reply #4 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;
}
« Last Edit: July 11, 2019, 01:21:51 am by FellowCoder »

 

anything