SFML intersection

I want there to be an intersection between rectangle and apple but it doesn't work, why?
You don't need to read all the code just where i said that i think the error is. I just put everything in case you want to run it.
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    RenderWindow window;
    window.create(VideoMode(800,600), "Snake Game");
    window.setFramerateLimit(300);

    RectangleShape rectangle;
    rectangle.setSize(Vector2f(16,16));
    rectangle.setFillColor(Color(250,250,250));
    rectangle.setPosition(100,100);

    bool turnRight = false;
    bool turnLeft = false;
    bool turnDown = false;
    bool turnUp = false;

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

            else if(event.type == Event::KeyPressed)
                if(event.key.code == Keyboard::Escape)
                    window.close();
        }

        //MOVEMENT

        if(Keyboard::isKeyPressed(Keyboard::Up))
        {
            turnRight = false;
            turnLeft = false;
            turnDown = false;
            turnUp = true;
        }
        if(Keyboard::isKeyPressed(Keyboard::Down))
        {
            turnRight = false;
            turnLeft = false;
            turnDown = true;
            turnUp = false;
        }
        if(Keyboard::isKeyPressed(Keyboard::Left))
        {
            turnRight = false;
            turnLeft = true;
            turnDown = false;
            turnUp = false;
        }
        if(Keyboard::isKeyPressed(Keyboard::Right))
        {
            turnRight = true;
            turnLeft = false;
            turnDown = false;
            turnUp = false;
        }

        if(turnUp == true)
            rectangle.move(0,-1);
        else if(turnDown == true)
            rectangle.move(0,1);
        else if(turnLeft == true)
            rectangle.move(-1,0);
        else if(turnRight == true)
            rectangle.move(1,0);

        int pos_x = rectangle.getPosition().x;
        int pos_y = rectangle.getPosition().y;

        RectangleShape apple;
        apple.setSize(Vector2f(16,16));
        apple.setFillColor(Color(250,0,0));
        apple.setPosition(400,400);

        int bottom, left, right, up;
        bottom = apple.getPosition().y + 16;
        left = apple.getPosition().x;
        right = apple.getPosition().x + 16;
        up = apple.getPosition().y;

        bool eatenApple = false;
//HERE IS WHERE I THINK IS THE ERROR
        if((pos_x >= left && pos_x <= right && pos_y <= bottom) || (pos_x >= left && pos_x <= right && pos_y + 16 >= up)
           || (pos_x + 16 >= left && pos_x + 16 <= right && pos_y <= bottom) || (pos_x + 16 >= left && pos_x + 16 <= right && pos_y + 16 >= up))
        {
            eatenApple = true;
        }

        if(eatenApple == true)
        {
            cout<<"You did it"<<endl;
            cout<<bottom<<" "<<left<<" "<<right<<" "<<up<<" "<<pos_x<<" "<<pos_y<<endl;
        }

        window.draw(apple);
        window.draw(rectangle);
        window.display();
        window.clear();
Last edited on
1
2
3
4
5
6
7
sf::RectangleShape shape1;
...
sf::RectangleShape shape2;
...
if (shape1.getGlobalBounds().intersects(shape2.getGlobalBounds())) {
...
}


http://www.sfml-dev.org/documentation/2.4.0/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011
No way I cant believe its just that lol
Thanks!
Also if i want to turn the whole screen black how would I do that?
I want when they intersect to turn black and say game over (i know how to display fonts, i just don't know how to turn the screen black)
Something like system("CLS")
I tried drawing a black rectangle on top but it didnt work
If you want the screen to go black, you can just not draw those things.
1
2
3
4
if (!gameOver) {
  window.draw(apple);
  window.draw(rectangle);
}


Just set gameOver to true when the game is finished.
Thanks for the help!
Topic archived. No new replies allowed.