Help Snake

I am using SFML. I can't code the apples I need help. This is what I have but is totally wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

        int apple_x = (rand() +32)%752;
        int apple_y = (rand() +32)%552;

        if(rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
        {
            apple.setPosition(apple_x, apple_y);
            window.draw(apple);
        }

        else
        {
            apple.setPosition(apple_x, apple_y);
            window.draw(apple);
        }
C'mon please help, I can't figure it out alone and I can't find anything online, so I have no ideia what to do... Here is the full code if it helps with anything

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <SFML/Graphics.hpp>
#include <ctime>

using namespace std;
using namespace sf;

int main()
{
    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);

    Texture wTexture;
    Sprite wallImage;

    if(!wTexture.loadFromFile("Wall.png", IntRect(0, 0, 32, 32)))
        return 1;

    wallImage.setTexture(wTexture);

    Font font;

    if(!font.loadFromFile("arial.ttf"))
        return 1;

    Text text("Game Over", font, 100);
    text.setFillColor(Color(255,255,255));
    text.setPosition(150,200);

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

    do
    {
            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);

            //WALL

            for(int x = 0; x<25; x++)
            {
                wallImage.setPosition(x*32,0);
                window.draw(wallImage);
                wallImage.setPosition(x*32,32*17.75);
                window.draw(wallImage);
                wallImage.setPosition(0,x*32);
                window.draw(wallImage);
                wallImage.setPosition(24*32,x*32);
                window.draw(wallImage);
            }

            //FRUIT

            srand(time(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);

            bool eatenApple = false;

            int apple_x = (rand() +32)%752;
            int apple_y = (rand() +32)%552;

            if(rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
            {
                apple.setPosition(apple_x,apple_y);
                window.draw(apple);
            }

            else if(!rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
            {
                if(apple.getPosition().x != 400 && apple.getPosition().y != 400)
                {
                    window.draw(apple);
                }

            }

            //INTERSECTION WITH WALL

            if(pos_x <= 32 || pos_y <= 32 || pos_y + 16 >= 568 || pos_x + 16 >= 768)
            {
                turnRight = false;
                turnLeft = false;
                turnDown = false;
                turnUp = false;
                window.draw(text);
                GameOver = true;
            }

            window.draw(apple);
            window.draw(rectangle);
            window.display();
            window.clear();
        }
    }while(GameOver == false);

}
YES! Thank you so much!! But why putting this
RectangleShape apple;
apple.setSize(Vector2f(16,16));
apple.setFillColor(Color(250,0,0));
apple.setPosition(400,400);

in the beggining change anything?
Again thank you so much for the help
Lol, ok thanks anyways :D
then how did you solve his or her question gentleguy?
gentleguy wrote:
I cannot explain, because (to tell the truth) I know nothing about SFML.

The solution you gave doesn't actually have anything to do with SFML.
You moved the declaration of the variable outside the while loop so that the variable didn't get re-declared during every iteration of the while loop and thus the changes applied to the same instance of RectangleShape instead of a new version in every loop iteration.
Topic archived. No new replies allowed.