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

Author Topic: beginning of snake  (Read 6063 times)

0 Members and 2 Guests are viewing this topic.

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
beginning of snake
« on: August 15, 2013, 03:00:35 pm »
Hello, I'm building the beginning of a snake, basically it continue to move in the direction you pressed. It compiles but I really can't figure out where is the problem O_o the code is fairly small

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <vector>


sf::Event event;
enum Direction { Up, Down, Left, Right};
int dir = Up;

class SnakeBlock
{
    public:

    SnakeBlock * next;
    sf::Texture texture;
    sf::Sprite snakeblock;
};

int main()
{
    sf::Sprite background;
    sf::Texture backgroundtex;
    backgroundtex.loadFromFile("background.png", sf::IntRect(0, 0, 1987, 1315));
    background.setTexture(backgroundtex);

    SnakeBlock snakeHead;
    snakeHead.texture.loadFromFile("spritesheetsnake.png", sf::IntRect(0,0,52,44));
    snakeHead.snakeblock.setTexture(snakeHead.texture);

    std::vector<SnakeBlock> Snake;
    Snake.push_back(snakeHead);
    Snake[0].snakeblock.setPosition(100,100);

    sf::RenderWindow window(sf::VideoMode(800,600), "SFML Test");
    window.setFramerateLimit(30);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:

                window.close();
                break;

                default:
                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                dir = Left;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                dir = Right;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                dir = Down;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                dir = Up;
        }
        while(dir == Up)
        {
            Snake[0].snakeblock.move(0,-5);
        }
        while(dir == Down)
        {
            Snake[0].snakeblock.move(0,5);
        }
        while(dir == Left)
        {
            Snake[0].snakeblock.move(-5,0);
        }
        while(dir == Right)
        {
            Snake[0].snakeblock.move(5,0);
        }
        window.clear(sf::Color::Green);

        window.draw(background);
        window.draw(Snake[0].snakeblock);

        window.display();
    }
    return 0;

}
 
Only making one block of the snake for the moment
The sheet+background:
http://imgur.com/5ZW17Na
http://imgur.com/9RnS3Bh

mike38

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Michael's Software Site
Re: beginning of snake
« Reply #1 on: August 15, 2013, 03:16:32 pm »
I think that you're new to loop-based game programming.

You need to think about this:
  while(dir == Up) { ... }

Remember that if you don't change 'dir' in that while loop, then you will be stuck in that while loop forever, not giving SFML a chance to redraw your window- so nothing will happen.
I think that changing the 'while's to 'if's might help  ;)

Remember that for anything to happen on the screen you need to get to the bottom of your game loop.
« Last Edit: August 15, 2013, 03:18:03 pm by mike38 »

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: beginning of snake
« Reply #2 on: August 15, 2013, 03:53:38 pm »
Thanks you a lot. I'm new to EVERYTHING in game programming in general.
So now my code works, and I wondered, how could I make the snake looks like in the other snake games where one part of the snake just follow the movement of the snake head ?
I think I would have needed to use *next but now I wonder if I should not make two int variables that contains the position of the head (first element of the snake) and that the next element of the snake is set at the last position of the head, and the next one set up at the last position of the last one etc etc?
Thanks a lot in advance

mike38

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Michael's Software Site
Re: beginning of snake
« Reply #3 on: August 15, 2013, 04:18:10 pm »
Good. Don't worry programming in a game-loop based way is different but you'll get used to it.

I recently read a tutorial on snake:
http://www.gamedev.net/page/resources/_/technical/game-programming/game-programming-snake-r3133


I think that the best approach is an std::vector, deque, queue of snake bits, (so you are on the right track ;) ), and then every frame:
Code: [Select]
- Calculate where the new snake's head will now be (using the 'dir' variable), and 'push' (look up vector.push_front) a new part of the snake onto the front of the vector to be it's new head.

- 'pop' from the back the tail of the snake.

You get me?

When the snake eats some food, don't do the 'pop', and obviously the snake will grow by one bit!

I think that you should work on your programming style a bit more. It's good at the moment, (and I'm glad to see no sign of 'using namespace std'), but I think it's a good idea in this case to use the fully-qualified name for enums, e.g. Direction::Up instead of Up, just for clarity. That said it's personal preference.

Also, most of your snake blocks will have the same texture so perhaps you should make the SnakeBlock's texture member a pointer. This means you're not saving so many copies of the same texture, and if you change the texture for one of the blocks they will all change, but this is up to you.
« Last Edit: August 15, 2013, 04:21:15 pm by mike38 »

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: beginning of snake
« Reply #4 on: August 15, 2013, 11:21:38 pm »
BEFORE YOU ANSWER, LOOK AT MY EDITS ^^

I don't exactly get you. I was already planning to use .push_front() and pop() to place in new snakeblocks in std::vector<snakeblock> snake, but I don't know how I could draw these on the screen, dir only serves me to know in which direction to move, not to know where I draw my snakeblocks (the exact coordinates x and y) on the screen. :/ aka "Calculate where the new snake's head will now be (using the 'dir' variable)".

I was planning to store the last coordinates of each snakeblock before those move, and draw each snakeblock at the position of those coordinates (by draw I mean set position by the way !). So in each if key pressed, make a :

for(int i = 1; i < snake.size(); i++)
{
snake.snakeblock.setPosition(snake[i+1].snakeblock.getPosition())
}

Or something like that. Only thing I need is how could I get the coordinates for one sprite. I need to use globalbounds :/ If you know how I could easily get the coordinates of one sprite...

BTW, the Direction:: didn't work, because Direction is not a class/namespace but an enumeration.
EDIT1: just figured I could use vector 2f.x/y
EDIT2: Here is the code of my implementation, WHAT CAN I USE TO push_front() elements? vectors do not have this function. I let it in this code but I know I will have to change my vector to another list system.
Here is the code of how I would do it, if somebody can tell me if there is one error or if I should not do it like that..

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <deque>

void selfIncrement();

sf::Event event;
sf::Clock clockSnake;
sf::Time elapse;

enum Direction { Up, Down, Left, Right};

int dir = Up;

int n = 1;

class SnakeBlock
{
    public:

    SnakeBlock * next;
    sf::Texture texture;
    sf::Sprite snakeblock;
    int lastX, lastY;
};

std::deque<SnakeBlock> Snake;

int main()
{
    elapse = clockSnake.getElapsedTime();

    sf::Music epicMusic;
    epicMusic.openFromFile("epicmusic.wav");
    epicMusic.play();

    SnakeBlock snakeHead;
    snakeHead.texture.loadFromFile("spritesheetsnake.png", sf::IntRect(0,0,25,22));
    snakeHead.snakeblock.setTexture(snakeHead.texture);
    SnakeBlock snakeBody1;
    snakeBody1.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));
    SnakeBlock snakeBody2;
    snakeBody2.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));

    Snake.push_front(snakeHead);
    Snake.push_front(snakeBody1);
    Snake.push_front(snakeBody2);
    Snake[2].snakeblock.setPosition(500,350);
    Snake[1].snakeblock.setPosition(475, 338);
    Snake[0].snakeblock.setPosition(450, 316);

    sf::RenderWindow window(sf::VideoMode(1028,768), "SFML Snake");
    window.setFramerateLimit(30);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                epicMusic.stop();
                window.close();
                break;

                default:
                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                dir = Left;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                dir = Right;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                dir = Down;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                dir = Up;
        }
        if(dir == Up)
        {

            Snake[0].snakeblock.move(0,-5);
            Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x, Snake[n-1].snakeblock.getPosition().y-22);
        }
        if(dir == Down)
        {
            Snake[0].snakeblock.move(0,5);
            Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x, Snake[n-1].snakeblock.getPosition().y+22);
        }
        if(dir == Left)
        {
            Snake[0].snakeblock.move(-5,0);
            Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x+25, Snake[n-1].snakeblock.getPosition().y);
        }
        if(dir == Right)
        {
            Snake[0].snakeblock.move(5,0);
            Snake[n].snakeblock.setPosition(Snake[n-1].snakeblock.getPosition().x-25, Snake[n-1].snakeblock.getPosition().y);
        }
        window.clear(sf::Color::Green);
        selfIncrement();
        for(unsigned int m = 0; m < Snake.size(); m++)
        {
            window.draw(Snake[m].snakeblock);
        }

        window.display();
    }
    return 0;

}

void selfIncrement()
{
    if(elapse.asSeconds() > 3)
    {
        n++;
        clockSnake.restart();
    }
    if(n > Snake.size())
    {
        n = 1;
    }
}
 

EDIT3: Figured that would not do  the behaviour I wanted, aka have a snake that could eat himself. I guess to do what I want I would need to make a variable that would maybe increment itself after a tiny period of time... Don't really know how I could do it. Please help me :)
EDIT4: Just figured out what you said about deque. Here it is.
So I don't know how to do what I said, aka "make the snake movement to make it possible to bite himself". What I got now : each snakeblock will take the last snakeblock position, resulting in a perfectly straight snake. :/

Maybe I need to use one variable that auto increments over time, resulting to update the position of each snakeblock one by one, or maybe I need to use the enumeration Direction, or maybe I need to do both of that, but I don't really know how to practically do so.

Some ideas would greatly help me ! Thanks.

EDIT: new code, figured I would need to use direction to pop and then push back one new element with new position here is the new code but the sprite don't even move :(
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <deque>

void advanceStep();

sf::Event event;
sf::Clock clockSnake;
sf::Time elapse;
sf::Vector2f headpos;

enum Direction { Up, Down, Left, Right};

sf::Vector2i direction(0, 0);
int dir = Up;

int n = 1;

class SnakeBlock
{
    public:

    sf::Texture texture;
    sf::Sprite snakeblock;
    int dir;
    int lastX, lastY;
};

std::deque<SnakeBlock> Snake;

int main()
{
    elapse = clockSnake.getElapsedTime();

    sf::Music epicMusic;
    epicMusic.openFromFile("epicmusic.wav");
    epicMusic.play();

    SnakeBlock snakeHead;
    snakeHead.texture.loadFromFile("spritesheetsnake.png", sf::IntRect(0,0,20,22));
    snakeHead.snakeblock.setTexture(snakeHead.texture);
    /*SnakeBlock snakeBody1;
    snakeBody1.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));
    SnakeBlock snakeBody2;
    snakeBody2.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));*/


    Snake.push_front(snakeHead);
    // Snake.push_front(snakeBody1);
    // Snake.push_front(snakeBody2);

    /*Snake[2].snakeblock.setPosition(500,350);
    Snake[1].snakeblock.setPosition(475, 338);*/

    Snake[0].snakeblock.setPosition(450, 316);

    sf::RenderWindow window(sf::VideoMode(1028,768), "SFML Snake");
    window.setFramerateLimit(30);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                epicMusic.stop();
                window.close();
                break;

                default:
                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                dir = Left;
                direction.x = -1;
                direction.y = 0;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                dir = Right;
                direction.x = 1;
                direction.y = 0;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                dir = Down;
                direction.x = 0;
                direction.y = -1;
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                dir = Up;
                direction.x = 0;
                direction.y = 1;
        }
        if(dir == Up)
        {

            Snake[0].snakeblock.move(0,-2);
        }
        if(dir == Down)
        {
            Snake[0].snakeblock.move(0,2);
        }
        if(dir == Left)
        {
            Snake[0].snakeblock.move(-2,0);
        }
        if(dir == Right)
        {
            Snake[0].snakeblock.move(2,0);
        }
        window.clear(sf::Color::Red);
        advanceStep();
        for(unsigned int m = 0; m < Snake.size(); m++)
        {
            window.draw(Snake[m].snakeblock);
        }
        window.display();
    }
    return 0;

}

void advanceStep()
{
    headpos.x = Snake[0].snakeblock.getPosition().x;
    headpos.y = Snake[0].snakeblock.getPosition().y;

    headpos.x += 22 * direction.x;
    headpos.y += 22 * direction.y;

    SnakeBlock element;
    element.snakeblock.setPosition(headpos);

    //Snake.pop_back();
    Snake.push_front(element);
}
 
« Last Edit: August 16, 2013, 03:07:37 am by Engineer »

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: beginning of snake
« Reply #5 on: August 16, 2013, 09:12:54 am »
Are you trying to make your snake get bigger and bigger every time you go through the loop? Seems like you'll be adding and element to your deque every time through the loop which would make it huge very quickly.

mike38

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Michael's Software Site
Re: beginning of snake
« Reply #6 on: August 16, 2013, 11:41:37 am »
Quote
if(dir == Down)
        {
            Snake[0].snakeblock.move(0,2);
        }
        if(dir == Left)
        {
            Snake[0].snakeblock.move(-2,0);
        }
        if(dir == Right)
        {
            Snake[0].snakeblock.move(2,0);
        }

Get rid of this.

Think about it logically.
You should do this every time you want the snake to move.

1. Create a new temporary SnakeBlock to be the new head.
  - Calculate the new global position of this new head. Use the snake's old head (snake[0]) and the direction that the snake is currently traveling in to calculate the new head position.

2. Push this SnakeBlock onto the front of the snake.

3. Pop the tail of the SnakeBlock off.

You shouldn't have to touch any other parts of the snake. Try playing it online or on a Nokia, see how the middle part of the snake stays absolutely still, only the head and tail move.

Don't worry about the snake not being able to eat himself. That's pretty trivial and not part of what you're trying to do at the moment. ;)

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: beginning of snake
« Reply #7 on: August 16, 2013, 01:19:37 pm »
Yeah thanks, I should have tried to play one snake and see how it works, it's pretty easy when you think about it...
So now I got one code that is for me the algorithm of a snake but it justs make my snake disappear, probably because of the deque pop(). I really do not see the problem since when I pop(), I push_front() one element... :(
Thanks in advance. (don't mind the clock and time, I do not use them).

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <deque>

void advanceStep();

sf::Event event;
/*sf::Clock clockSnake;
sf::Time elapse;*/


sf::Vector2i direction(0, 0);

int n = 1;

class SnakeBlock
{
    public:

    sf::Texture texture;
    sf::Sprite snakeblock;
    int dir;
    int lastX, lastY;
};

std::deque<SnakeBlock> Snake;

int main()
{
    //elapse = clockSnake.getElapsedTime();

    sf::Music epicMusic;
    epicMusic.openFromFile("epicmusic.wav");
    epicMusic.play();

    SnakeBlock snakeHead;
    snakeHead.texture.loadFromFile("spritesheetsnake.png", sf::IntRect(0,0,20,22));
    snakeHead.snakeblock.setTexture(snakeHead.texture);
    SnakeBlock snakeBody1;
    snakeBody1.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));
    SnakeBlock snakeBody2;
    snakeBody2.snakeblock.setTexture(*(snakeHead.snakeblock.getTexture()));

    Snake.push_back(snakeHead);
    Snake.push_back(snakeBody1);
    Snake.push_back(snakeBody2);

    Snake[2].snakeblock.setPosition(500,350);
    Snake[1].snakeblock.setPosition(475, 338);
    Snake[0].snakeblock.setPosition(450, 316);

    sf::RenderWindow window(sf::VideoMode(1028,768), "SFML Snake");
    window.setFramerateLimit(20);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                epicMusic.stop();
                window.close();
                break;

                default:
                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                direction.x = -1;
                advanceStep();
                for(unsigned int i = 1; i < Snake.size(); i++)
                {
                    Snake[i].snakeblock.setPosition(Snake[i+1].snakeblock.getPosition().x-20, Snake[i+1].snakeblock.getPosition().y);
                }
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                direction.x = 1;
                advanceStep();
                for(unsigned int j = 1; j < Snake.size(); j++)
                {
                    Snake[j].snakeblock.setPosition(Snake[j+1].snakeblock.getPosition().x+20, Snake[j+1].snakeblock.getPosition().y);
                }
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                direction.y = -1;
                advanceStep();
                for(unsigned int l = 1; l < Snake.size(); l++)
                {
                    Snake[l].snakeblock.setPosition(Snake[l+1].snakeblock.getPosition().x, Snake[l+1].snakeblock.getPosition().y-22);
                }
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                direction.y = 1;
                advanceStep();
                for(unsigned int o = 1; o < Snake.size(); o++)
                {
                    Snake[o].snakeblock.setPosition(Snake[o+1].snakeblock.getPosition().x, Snake[o+1].snakeblock.getPosition().y+22);
                }
        }
        window.clear(sf::Color::Red);
        for(unsigned int m = 0; m < Snake.size(); m++)
        {
            window.draw(Snake[m].snakeblock);
        }
        window.display();
    }
    return 0;

}

void advanceStep()
{
    sf::Vector2f headpos;
    headpos.x = Snake[0].snakeblock.getPosition().x;
    headpos.y = Snake[0].snakeblock.getPosition().y;

    headpos.x += 22 * direction.x;
    headpos.y += 22 * direction.y;

    SnakeBlock element;
    element.snakeblock.setPosition(headpos);

    Snake.push_front(element);
    Snake.pop_back();
}

 

And thanks for your help again. :)
BTW: even if I put the advance step in each if(keypressed = left/right/down/left) nothing happens :)
« Last Edit: August 16, 2013, 02:22:18 pm by Engineer »

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: beginning of snake
« Reply #8 on: August 17, 2013, 01:21:09 am »
Okay so I made a big step in the code, after asking stackoverflow I did another approach which is similar but don't use sprites and better organization etc.
Here is a code of a snake that can wiggle. sadly ! it only move when you press a key. Why so ? putting the move outside of the if(key pressed) change nothing.
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>

#include <deque>

enum Direction { Up, Down, Right, Left};
int direction = Down;

class Block
{
    private:
    int ax, ay;

    public:
    Block(int, int);
    int getX();
    int getY();

};


class Snake
{
    private:
    std::deque<Block> asnake;
    int adirection;

    public:
    void Move(int);
    void grow();
    void shrink();
    std::deque<Block> getBlocks();
    int getX();
    int getY();
    Snake();
    void setdirection();
};

int main()
{
    sf::Music epicMusic;
    epicMusic.openFromFile("epicmusic.wav");
    epicMusic.play();
    sf::Event event;
    Snake snake;
    snake.setdirection();
    snake.grow();
    snake.grow();

    sf::RenderWindow window(sf::VideoMode(500,500), "SFML Snake");
    window.setFramerateLimit(30);
    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:

                epicMusic.stop();
                window.close();
                break;

                default:

                break;
            }

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                direction = Left;
                snake.Move(direction);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                direction = Right;
                snake.Move(direction);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
                direction = Down;
                snake.Move(direction);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
                direction = Up;
                snake.Move(direction);
        }

        window.clear(sf::Color::Red);


        for(unsigned int i = 1; i < (snake.getBlocks()).size(); i++)
        {
            float x = (snake.getBlocks())[i].getX();
            float y = (snake.getBlocks())[i].getY();

            sf::RectangleShape block(sf::Vector2f(20,20));
            block.setFillColor(sf::Color::Green);
            block.setPosition(x, y);

            window.draw(block);
        }
        window.display();
    }
    return 0;
}

Block::Block(int x, int y)
{
    ax = x;
    ay = y;
}

int Block::getX()
{
    return ax;
}

int Block::getY()
{
    return ay;
}

void Snake::Move(int NewDirection)
{
    shrink();
    if(NewDirection == Up)
    {

        if(adirection != Down)
        {
            asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() - 22));
            adirection = Up;
        }
        else
        {
            asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() + 22));
        }
    }
    else if(NewDirection == Right)
    {
        if(adirection != Left)
        {
            asnake.push_front(Block(asnake.front().getX() + 25, asnake.front().getY()));
            adirection = Right;
        }
        else
        {
            asnake.push_front(Block(asnake.front().getX() - 25, asnake.front().getY()));
        }
    }
    else if(NewDirection == Down)
    {
            if(adirection != Up)
            {
                asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() + 22));
                adirection = Down;
            }
            else
            {
                asnake.push_front(Block(asnake.front().getX(), asnake.front().getY() - 22));
            }

    }
    else if(NewDirection == Left)
    {
        if(adirection != Right)
        {
            asnake.push_front(Block(asnake.front().getX() - 25, asnake.front().getY()));
            adirection = Left;
        }
        else
        {
            asnake.push_front(Block(asnake.front().getX() + 25, asnake.front().getY()));
        }
    }

}

void Snake::grow()
{
    asnake.push_back(Block(asnake.back().getX(), asnake.back().getY()));
}
void Snake::shrink()
{
    asnake.pop_back();
}

std::deque<Block> Snake::getBlocks()
{
    return asnake;
}
int Snake::getX()
{
    return asnake.front().getX();
}

int Snake::getY()
{
    return asnake.front().getY();
}

Snake::Snake()
{
    asnake.push_front(Block(25,0));
    asnake.push_front(Block(50,0));
    asnake.push_front(Block(75,0));
}

void Snake::setdirection()
{
    adirection = Up;
}
 
Thanks.

Engineer

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: beginning of snake
« Reply #9 on: August 17, 2013, 03:11:58 am »
Made it work. So. Fucking. Happy. :)

 

anything