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

Author Topic: Shape is disappearing when i try to move it  (Read 2477 times)

0 Members and 1 Guest are viewing this topic.

FelixDilan

  • Newbie
  • *
  • Posts: 1
    • View Profile
Shape is disappearing when i try to move it
« on: November 12, 2017, 08:27:12 pm »
Hello i just started learning sfml and i decided it would be fun to create the snake game. It went until i got stuck with the tail. I know, the code is messy, but i commented the important parts. So i think the problem is in the the part with switches, but according to the console log it should work fine at least for x axis.



#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
void setupShape(sf::RectangleShape& shape, sf::Vector2f const& pos, sf::Color const& color)
{
        shape.setFillColor(color);
        shape.setPosition(pos);
        shape.setOrigin(shape.getSize() * 0.5f); // The center of the rectangle
}

int main()

{
    int px=300, py=300;
    int xv=0, yv=0;
        int tail=5;
        int tx;
        int ty;
        sf::RenderWindow window(sf::VideoMode(600, 600), "Snek");
        //Set target Frames per second
        window.setFramerateLimit(6);

        sf::Vector2f startPos = sf::Vector2f(px,py);
        sf::RectangleShape playerRect(sf::Vector2f(20, 20));
        setupShape(playerRect, startPos, sf::Color::Green);

       
       
       
    /The shape itself
        sf::RectangleShape tailRect(sf::Vector2f(20, 20));
        setupShape(tailRect,sf::Vector2f(100, 100), sf::Color::Green);




        while (window.isOpen())
        {
       sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::EventType::Closed)
                                window.close();
                }


        // input
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
                        {yv= 20;
                        xv= 0;}
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
                        {yv= -20;
                        xv= 0;}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
                        {xv= 20;
                        yv= 0;}
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
                        {xv= -20;
                        yv= 0;}



        px = px+xv;
        py = py+yv;
        playerRect.move(xv, yv);
   
   
   
   
    // trying to move the shape here..
        switch (xv)
        {
            case 20:
                tailRect.move(px-20*1, py);
                cout << "tailx=" << px-20 << " " << "playerx=" << px << endl;
            case -20:
                tailRect.move(px+20*1, py);


        }
        switch (yv)
        {
            case 20:
                tailRect.move(px, py-20*1);

            case -20:
                tailRect.move(px, py+20*1);


        }


        //cout << "px=" << px << "py" << py << endl;
        if (px <= 0){
            playerRect.move(580, 0);
            px = px + 580;
        }
        if (px >= 600){
            playerRect.move(-580, 0);
            px = px -580;
        }
        if (py >= 600){
            playerRect.move(0, -580);
            py = py -580;
        }
        if (py <= 0){
            playerRect.move(0, 580);
            py = py +580;
        }




                window.clear(sf::Color::Black);
               
                //drawing the shape.
                window.draw(tailRect);
                window.draw(playerRect);


                window.display();

        }

        return 0;
}

 

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Shape is disappearing when i try to move it
« Reply #1 on: November 12, 2017, 09:04:30 pm »
you use move wrong way, so use setPosition instead.
move(arg) = setPosition(getPosition() + arg)

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Shape is disappearing when i try to move it
« Reply #2 on: November 13, 2017, 06:55:57 am »
Take a look at this if you're stuck

Let's make 16 games in C++: Snake

lukaius

  • Guest
Re: Shape is disappearing when i try to move it
« Reply #3 on: November 14, 2017, 03:18:49 pm »
Happens to me to