Snake Problem

Hi!


I was trying to create a snake program in c++ (i'm using devC++ environment). I had already initialized the snake. But I have no idea as to how to move the snake. I'm using the graphics.h header from borland...

I was wondering if you could help me out on the snake movement algorithm??? thanks!
Using graphics.h may not be the best idea. Can you post the code you have so far?

BTW, nice nickname. Reminds me of this -> http://www.yosefk.com/blog/the-nomadic-programmer.html
Thanks! :)

void snake::createSnake()
{
while (cnt==0){
for (i;i<=800;i++){
cnt++;
rectangle(10+i,300,i,310);
rectangle(20+i,300,10+i,310);
rectangle(30+i,300,20+i,310);
rectangle(40+i,300,30+i,310);
rectangle(50+i,300,40+i,310);
rectangle(60+i,300,50+i,310);
rectangle(70+i,300,60+i,310);
Sleep(10);
cleardevice();
}
}
}

i'm trying to manipulate this code snippet...this is just a trial and not the final codes yet. i just want to try it out if i could move the rectangles. thanks... i'm really starting from scratch... and it isn't easy... lol... and if not graphics.h... what would be best to use?
CodeNomad wrote:
i just want to try it out if i could move the rectangles.

This is typically done by clearing the screen and then redrawing the rectangles in their new
positions. Which means that you should store the position of every snake part somewhere.

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
#include <windows.h>
#include <iostream>
#include <list>

struct Position
{
    int x, y;

    Position(int x_, int y_): x(x_), y(y_) {}
};

void goto_xy(int x, int y)
{
    COORD pos = { x, y };

    SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void clear_screen()
{
    DWORD written;
    COORD start = { 0, 0 };

    FillConsoleOutputCharacter(
        GetStdHandle(STD_OUTPUT_HANDLE),
        ' ', 80 * 25, start, &written);

    goto_xy(0, 0);
}

void draw_snake(const std::list<Position> & snake)
{
    std::list<Position>::const_iterator cur_it = snake.begin();
    std::list<Position>::const_iterator end_it = snake.end();

    for (; cur_it != end_it; ++cur_it)
    {
        goto_xy(cur_it->x, cur_it->y);
        std::cout << '@' << std::flush;
    }
}

int main()
{
    std::list<Position> snake_body;

    snake_body.push_front(Position(0, 0));
    snake_body.push_front(Position(0, 1));
    snake_body.push_front(Position(0, 2));
    snake_body.push_front(Position(1, 2));
    snake_body.push_front(Position(1, 3));
    snake_body.push_front(Position(2, 3));

    draw_snake(snake_body);

    Sleep(1000);

    for (int i = 0; i < 5; ++i)
    {
        // add new head
        snake_body.push_front(Position(3 + i, 3));

        // remove old tail
        snake_body.pop_back();

        // redraw
        clear_screen();
        draw_snake(snake_body);

        Sleep(1000);
    }

    clear_screen();
}

CodeNomad wrote:
if not graphics.h... what would be best to use?

If it works for you, use it. It doesn't work here. I don't even have the header.
I would simply use ascii characters to represent the snake parts, and windows
API functions to move the console cursor around and change the console color.

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
#include <windows.h>
#include <iostream>

void goto_xy(int x, int y)
{
    COORD pos = { x, y };

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void set_color(int color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}

int main()
{
    goto_xy(30, 10);

    set_color
    (
        // light yellow foreground
        FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY |

        // dark blue background
        BACKGROUND_BLUE
    );

    std::cout << "Hello, World!" << std::endl;
}

If you want fancier stuff, you could take a look at the (pd)curses library, or even SFML.

Useful link -> http://msdn.microsoft.com/en-us/library/ms682073(v=vs.85).aspx
Last edited on
I like SDL library, it's really made from developers for such... even better: it's available for any platform

ZED
Topic archived. No new replies allowed.