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

Author Topic: Snake Clone apples  (Read 768 times)

0 Members and 1 Guest are viewing this topic.

Gantzias

  • Newbie
  • *
  • Posts: 1
    • View Profile
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();
    }
}
 

lapinozz

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Snake Clone apples
« Reply #1 on: September 04, 2016, 05:10:09 pm »
You're nearly there! Here's a couple of error that I spotted.

  • You are drawing the apple near the end of the main loop anyways, there is no need to draw it elsewhere

    so you can remove all the window.draw(apple); apart from the one at the end
  • You declare apple in the loop which mean it will be deleted and recreated each time the loop is executed.

    this code should go before the while loop

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;
[/list]
  • You don't need to calculate a new position for the apple every frame, only when you collide with the apple

    so I would move it in the collision check condition just like that:
if(rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
{
    int apple_x = (rand() +32)%752;
    int apple_y = (rand() +32)%552;

    apple.setPosition(apple_x, apple_y);
    window.draw(apple);
}
    it is also to note that srand and rand are old and there is multiple reasons to try to avoid them, I would advise to make research in C++11 random.
    [/li]

Here's the final code:

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

    srand(time(0));

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

    bool eatenApple = 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

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

        if(rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
        {
            int apple_x = (rand() +32)%752;
            int apple_y = (rand() +32)%552;

            apple.setPosition(apple_x, apple_y);
        }

        //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();
    }
}
 

 

anything