Game Dev

I decided a few days ago that i wanted to create a console-based version of that old game Snakes. The one where you're a little snake and you go around and eat orbs and get bigger and bigger, and the object of the game is to avoid yourself, as when you run into your body its game over.

I've already ran into an issue. I can't figure a robust way to limit the console to say.. 30 frames per seconds.
suggestions, or hints?

Thanks a bunch
-Thumper
The console sucks for this kind of thing. Why not just use a graphics lib? It's much easier and looks better.
I've got zero experience with any graphics library. I know the console sucks for it, but that's what I'm comfortable with.

If you could give me the name of, or link me to a user friendly graphics lib, I'll give it a shot.
I've got zero experience with any graphics library. I know the console sucks for it, but that's what I'm comfortable with.


This is the bane of so many people on these forums. Every time I see another post about a console game I die a little inside.

And I've been seeing so many of them lately.

If you could give me the name of, or link me to a user friendly graphics lib, I'll give it a shot.


SFML is very simple. Installing it on your machine is a little work, but you only have to do that once, and it's well worth it.

http://www.sfml-dev.org/
Here is something you can work on. I've written this to be an installer for a keylogger, so don't expect much.
SFML is a good library for games, google it.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <iostream>
#include <string>
#include <list>
#include <cstdlib>
#include <ctime>
#include <process.h>
#include <windows.h>
using namespace std;

class GameConsole
{
private:
    HANDLE console;

public:
    GameConsole()
    {
        console=GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_CURSOR_INFO cursor_status;

        GetConsoleCursorInfo(console,&cursor_status);

        cursor_status.bVisible=false;

        SetConsoleCursorInfo(console,&cursor_status);
    }

    void CharOutXY(int x, int y, char c)
    {
        COORD pos; pos.X=x; pos.Y=y;

        SetConsoleCursorPosition(console,pos);

        std::cout << c << std::flush;
    }

    void CharOutXYC(int x, int y, WORD color, char c)
    {
        COORD pos; pos.X=x; pos.Y=y;

        SetConsoleCursorPosition(console,pos);
        SetConsoleTextAttribute(console,color);

        std::cout << c << std::flush;
    }

    void TextOutXY(int x, int y, const std::string & str)
    {
        COORD pos; pos.X=x; pos.Y=y;

        WORD color=FOREGROUND_BLUE|FOREGROUND_GREEN;
        color|=FOREGROUND_RED|FOREGROUND_INTENSITY;

        SetConsoleCursorPosition(console,pos);
        SetConsoleTextAttribute(console,color);

        std::cout << str << std::flush;
    }
};

class GameInput
{
public:
    enum MoveDirection
    {
        MD_NONE,
        MD_UP,
        MD_DOWN,
        MD_RIGHT,
        MD_LEFT
    };

private:
    volatile MoveDirection move_dir;
    volatile bool polling;
    volatile bool stopped_polling;

    static void poll(void * p)
    {
        GameInput * gi = (GameInput*)p;

        while (true)
        {
            if (!gi->polling) break;

            if (GetAsyncKeyState(VK_UP)>>15)
                {gi->move_dir=MD_UP;}

            if (GetAsyncKeyState(VK_DOWN)>>15)
                {gi->move_dir=MD_DOWN;}

            if (GetAsyncKeyState(VK_LEFT)>>15)
                {gi->move_dir=MD_LEFT;}

            if (GetAsyncKeyState(VK_RIGHT)>>15)
                {gi->move_dir=MD_RIGHT;}

            Sleep(25);
        }

        gi->stopped_polling=true;
    }

public:
    GameInput():move_dir(MD_NONE),polling(false){}

    void StartPolling()
    {
        if (polling) return;

        polling=true;
        stopped_polling=false;
        _beginthread(poll,0,this);
    }

    void StopPolling()
    {
        if (!polling) return;

        polling=false;
        while (!stopped_polling) Sleep(100);
    }

    MoveDirection GetInput()
    {
        return move_dir;
    }
};

class GameMap
{
public:
    enum TileType
    {
        TT_EMPTY,
        TT_SNAKE,
        TT_BERRY
    };

private:
    TileType map[80][25];

public:
    GameMap()
    {
        for (int x=0; x<80; x++)
            for (int y=0; y<25; y++)
                map[x][y]=TT_EMPTY;
    }

    TileType & At(int x, int y)
    {
        return map[x][y];
    }
};

class Snake
{
private:
    std::list<COORD> snake_pos;

public:
    Snake(){}

    void init(int x, int y)
    {
        COORD head;
        head.X=x;
        head.Y=y;

        snake_pos.push_front(head);
    }

    COORD GetHead()
    {
        return snake_pos.front();
    }

    COORD GetTail()
    {
        return snake_pos.back();
    }

    void Grow(COORD new_head)
    {
        snake_pos.push_front(new_head);
    }

    void Move(COORD new_head)
    {
        Grow(new_head);
        snake_pos.pop_back();
    }
};

class Game
{
private:
    GameConsole game_console;
    GameInput game_input;
    GameMap game_map;
    Snake snake;

    static const char snake_char='*';
    static const char berry_char='@';

    std::list<COORD> need_redraw;
    GameInput::MoveDirection move_dir;
    COORD new_head;
    COORD old_tail;
    int berry_count;

    void find_good_berry_pos(int & x, int & y)
    {
        while (true)
        {
            x=rand()%80;
            y=rand()%25;

            if (game_map.At(x,y)==GameMap::TT_EMPTY) break;
        }
    }

    void need_redraw_xy(int x, int y)
    {
        COORD pos;
        pos.X=x;
        pos.Y=y;

        need_redraw.push_back(pos);
    }

    void spawn_berries()
    {
        if (berry_count>=3) return;

        int spawn_prob=berry_count+1;
        spawn_prob*=berry_count+1;
        spawn_prob*=2;
        spawn_prob=20-spawn_prob;

        bool spawn=rand()%100+1<=spawn_prob;

        int x, y;

        if (spawn)
        {
            find_good_berry_pos(x,y);

            game_map.At(x,y)=GameMap::TT_BERRY;
            need_redraw_xy(x,y);
            berry_count++;
        }
    }

    void get_input()
    {
        new_head=snake.GetHead();
        move_dir=game_input.GetInput();

        switch(move_dir)
        {
            case GameInput::MD_UP:
                new_head.Y--;

            break;
            case GameInput::MD_DOWN:
                new_head.Y++;

            break;
            case GameInput::MD_RIGHT:
                new_head.X++;

            break;
            case GameInput::MD_LEFT:
                new_head.X--;

            break;
            default:;
        }

        new_head.X+=80;
        new_head.X%=80;
        new_head.Y+=25;
        new_head.Y%=25;
    }

    bool defeat()
    {
        if (game_map.At(new_head.X,new_head.Y)==GameMap::TT_SNAKE) return true;

        return false;
    }

    void move_snake()
    {
        old_tail=snake.GetTail();

        switch(game_map.At(new_head.X,new_head.Y))
        {
            case GameMap::TT_BERRY:
                game_map.At(new_head.X,new_head.Y)=GameMap::TT_SNAKE;
                snake.Grow(new_head);
                need_redraw_xy(new_head.X,new_head.Y);

                berry_count--;

            break;
            default:
                game_map.At(old_tail.X,old_tail.Y)=GameMap::TT_EMPTY;
                need_redraw_xy(old_tail.X,old_tail.Y);
                snake.Move(new_head);
                game_map.At(new_head.X,new_head.Y)=GameMap::TT_SNAKE;
                need_redraw_xy(new_head.X,new_head.Y);
        }
    }

    void draw()
    {
        COORD pos;
        std::list<COORD>::iterator it;
        for (it=need_redraw.begin(); it!=need_redraw.end(); it++)
        {
            pos=*it;

            switch(game_map.At(pos.X,pos.Y))
            {
                case GameMap::TT_EMPTY:
                game_console.CharOutXY(pos.X,pos.Y,' ');

                break;
                case GameMap::TT_SNAKE:
                game_console.CharOutXYC(pos.X,pos.Y,
                    FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY,snake_char);

                break;
                case GameMap::TT_BERRY:
                game_console.CharOutXYC(pos.X,pos.Y,
                    FOREGROUND_RED|FOREGROUND_INTENSITY,berry_char);
            }
        }
    }

    void main_game_loop()
    {
        while (move_dir==GameInput::MD_NONE)
        {
            get_input();

            Sleep(100);
        }

        game_console.TextOutXY(0,0,"                                        ");
        game_console.TextOutXY(0,1,"                                        ");

        while (true)
        {
            if (GetAsyncKeyState(VK_ESCAPE)>>15) break;

            need_redraw.clear();
            spawn_berries();
            get_input();
            if (defeat()) return;
            move_snake();
            draw();

            Sleep(200);
        }
    }

    void init()
    {
        srand(time(0));

        move_dir=GameInput::MD_NONE;
        berry_count=0;

        snake.init(40,12);
        game_console.CharOutXYC(40,12,
            FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY,snake_char);
    }

public:
    Game()
    {
        init();
    }

    void Play()
    {
        game_console.TextOutXY(0,0,"(hit an arrow key to start...)");
        game_console.TextOutXY(0,1,"(hold esc to quit...)");

        game_input.StartPolling();
        main_game_loop();
        game_input.StopPolling();
    }
};

int main(int argc, char * argv[])
{
    Game().Play();

    return 0;
}

Last edited on
Disch wrote:
Every time I see another post about a console game I die a little inside.

Oh... Sorry... :/ (I couldn't edit the previous post... max chars reached...)
@Disch
My apologies. After all, i joined this forum to improve my code. Your input is greatly appreciated.

@m4sterr0shi
Well damn, you just totally obliterated what i was trying to do. I think I'll have to pass on reading your code before i give it a go myself. Thanks, though.
Topic archived. No new replies allowed.