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 - Gantzias

Pages: [1]
1
General / Snake Clone apples
« on: September 03, 2016, 10:41:46 pm »
I can't figure out how to spawn an apple and for it to stay there until there is a new intersection. Can anyone help me?
The game is far from done but this is what I have so far if it helps

#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;

    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;
        int x = apple_x;
        int y = apple_y;

        if(rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
        {
            apple.setPosition(x,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);
        }

        window.draw(apple);
        window.draw(rectangle);
        window.display();
        window.clear();
    }
}
 

Pages: [1]
anything