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

Author Topic: Snake Game[In progress]  (Read 2797 times)

0 Members and 1 Guest are viewing this topic.

Serilda

  • Guest
Snake Game[In progress]
« on: March 19, 2016, 06:00:28 pm »

sf::RectangleShape generateFruit(){
       
       
        sf::RectangleShape fruit;
        fruit.setFillColor(sf::Color::Yellow);
        int fruitx = rand() % 400;
        int fruity = rand() % 400;
        fruit.setPosition(fruitx, fruity);
        fruit.setSize(sf::Vector2f(5, 5));
       
        return fruit;
       
       
}
int main()
{
        srand(time(NULL));
        int width = 400;
        int height = 400;
        sf::VideoMode videomode(width, height);
        sf::RenderWindow window(videomode, "Snake");
        sf::RectangleShape snake;
       
        snake.setFillColor(sf::Color::Red);
        snake.setSize(sf::Vector2f(20, 20));
        snake.setPosition(100,100);
        while (window.isOpen()){
                window.clear(sf::Color::Black);
                window.draw(snake);
                sf::Clock clock;
               
                sf::Time t1 = sf::seconds(20);
                sf::Time elapsed1 = clock.getElapsedTime();
                if (elapsed1 == t1){



                        window.draw(generateFruit());
                        clock.restart();
                       
                }
               
                window.display();
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if ((event.type == sf::Event::Closed) ||
                                ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
                                window.close();
                }


                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))

                        snake.move(0, -0.1 );
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        snake.move(0, 0.1);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        snake.move(-0.1, 0);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        snake.move(0.1, 0);
        }
       
}
 

Problem is that the fruit is never generated, and when I don't write the time stuff it is displayed but it has no delay and keeps on appearing randomly with 0 delay.
         

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Snake Game[In progress]
« Reply #1 on: March 19, 2016, 06:06:03 pm »
put the clock outside the while loop, you're redifining it on every iteration

Serilda

  • Guest
Re: Snake Game[In progress]
« Reply #2 on: March 19, 2016, 06:09:59 pm »
Thanks, but I did that and the problem persists :/

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Snake Game[In progress]
« Reply #3 on: March 19, 2016, 06:13:03 pm »
hm, put clock declaration before while loop. Do the same with defininition of your time object. Also, instead of "elapsed1 == t1" do >=, there's a possibility that the frame lags and the time is never spot on 20seconds.

Serilda

  • Guest
Re: Snake Game[In progress]
« Reply #4 on: March 19, 2016, 06:18:15 pm »
still :/

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Snake Game[In progress]
« Reply #5 on: March 19, 2016, 06:28:04 pm »
okay so, the thing is that you only draw the fruit on the frame that it is spawned. so it will only be drawn once and then isntantly deleted by window.clear().

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Snake Game[In progress]
« Reply #6 on: March 19, 2016, 06:32:31 pm »
at the top of the file, declare std::Vector<sf::RectangleShape> spawnedFruit; and then when you 'generate fruit' do, spawnedFruit.push_back(generateFruit()); then just before window.display(). put a for loop :

for(int i = 0; i < spawnedFruit.size(); i++){
    window.draw(spawnedFruit[i]);
}
 

basically you have to draw things every frame if you want them to be visible while using window.clear(); You may also want to define a limit to howmany fruit are allowed, otherwise the game will just keep making them and the lag will be very real. so just for example again at the top of the file:

int maxFruit = 20;
int fruitCount = 0;
 

then in the while loop:


if (elapsed1 >= t1){


            if(fruitCount  < maxFruit){
            spawnedFruit.push_back(generateFruit());
            fruitCount++;
             }
            clock.restart();
           
        }
 

there's a possibilty with this that you'll need to use smart pointers or something, but try it and see first. I know vectors can be annoying.
« Last Edit: March 19, 2016, 06:49:56 pm by ka0s420 »

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Snake Game[In progress]
« Reply #7 on: March 19, 2016, 07:29:52 pm »
okay, as i can see that my replies are all a bit confusing, i tested out the app with the changes, so here it is, and it works, remember to wait 20sec to see the fruit spawn ;)

#include <SFML/Graphics.hpp>

sf::RectangleShape generateFruit() {


        sf::RectangleShape fruit;
        fruit.setFillColor(sf::Color::Yellow);
        int fruitx = rand() % 400;
        int fruity = rand() % 400;
        fruit.setPosition(fruitx, fruity);
        fruit.setSize(sf::Vector2f(5, 5));

        return fruit;


}
int main()
{
        srand(time(NULL));
        int width = 400;
        int height = 400;
        sf::VideoMode videomode(width, height);
        sf::RenderWindow window(videomode, "Snake");
        sf::RectangleShape snake;

        snake.setFillColor(sf::Color::Red);
        snake.setSize(sf::Vector2f(20, 20));
        snake.setPosition(100, 100);
        sf::Clock clock;
        sf::Time t1 = sf::seconds(20);
        std::vector<sf::RectangleShape> spawnedFruit;
        int maxFruit = 20;
        int fruitCount = 0;
        while (window.isOpen()) {
                window.clear();
                window.draw(snake);
               

               
                sf::Time elapsed1 = clock.getElapsedTime();
                if (elapsed1 >= t1) {


                        if (fruitCount < maxFruit)
                        {
                                spawnedFruit.push_back(generateFruit());
                                fruitCount++;
                        }
                        clock.restart();

                }
                for (int i = 0; i < spawnedFruit.size(); i++)
                {
                        window.draw(spawnedFruit[i]);
                }
                window.display();
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if ((event.type == sf::Event::Closed) ||
                                ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
                                window.close();
                }


                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))

                        snake.move(0, -0.1);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        snake.move(0, 0.1);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        snake.move(-0.1, 0);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        snake.move(0.1, 0);
        }

}
 

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Snake Game[In progress]
« Reply #8 on: March 19, 2016, 08:10:17 pm »
Since it's a snake game, i'd think it unnecessary to use a vector for Fruit as 1 fruit is spawned at a time and the next is only spawned when the current is reached.
At least, that's the snake game i know :D.

Ignore this if the change in gameplay is intended.

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Snake Game[In progress]
« Reply #9 on: March 19, 2016, 08:32:51 pm »
good point bitano.

Here it is with just one piece of fruit at a time.

#include <SFML/Graphics.hpp>

sf::RectangleShape generateFruit() {


        sf::RectangleShape fruit;
        fruit.setFillColor(sf::Color::Yellow);
        int fruitx = rand() % 400;
        int fruity = rand() % 400;
        fruit.setPosition(fruitx, fruity);
        fruit.setSize(sf::Vector2f(5, 5));

        return fruit;


}
int main()
{
        srand(time(NULL));
        int width = 400;
        int height = 400;
        sf::VideoMode videomode(width, height);
        sf::RenderWindow window(videomode, "Snake");
        sf::RectangleShape snake;

        snake.setFillColor(sf::Color::Red);
        snake.setSize(sf::Vector2f(20, 20));
        snake.setPosition(100, 100);
        sf::Clock clock;
        sf::Time t1 = sf::seconds(20);
        sf::RectangleShape spawnedFruit;
        while (window.isOpen()) {
                window.clear();
                window.draw(snake);



                sf::Time elapsed1 = clock.getElapsedTime();
                if (elapsed1 >= t1) {


                        spawnedFruit = generateFruit();
                       
                        clock.restart();

                }
               
                window.draw(spawnedFruit);
               
                window.display();
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if ((event.type == sf::Event::Closed) ||
                                ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
                                window.close();
                }


                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))

                        snake.move(0, -0.1);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        snake.move(0, 0.1);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        snake.move(-0.1, 0);
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        snake.move(0.1, 0);
        }

}
 

Serilda

  • Guest
Re: Snake Game[In progress]
« Reply #10 on: March 19, 2016, 11:19:27 pm »
It worked! Thanks so much guys  :)

 

anything