Need help with snake game

I've never program an actual game so I while back I made it my goal to do so. I decided on a snake game program and started the actual program itself today. I have it almost perfected but there's one major problem:
I have absolutely no idea how to add to the snake when it eats a fruit or how to make it all move together after it's been added to.

The code for the program is:
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
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <process.h>
#include <fstream>
#include <ctime>

#define snake_max_length 5
#define left 0
#define up 1
#define right 2
#define down 3

using namespace std;

void movement();
void food();
void highscore();
void keypress(void*);
void drawscreen();
int gameover();
void snakeplus();

int Hscore = 0;
char board[79][20];
int direction = 0; //0-left 1-up 2-right 3-down
int fruit = 0;
int score = 0;
int snakeparts = 2;

int foodx;
int foody;

int headX = 39;
int headY = 10;
int TailX = 40;
int TailY = 10;

int main()
{
    system("TITLE Snake Game - by Niven");
    system("COLOR 0F");

    _beginthread( keypress, 0, (void*)0 );

    highscore();

    int x,y;

    cout<<"Score: "<<score<<"                                   Highscore: "<<Hscore; //Don't forget this in the drawscreen function

    for (x = 0;x<79;x++)
    { //sets values to the board array
        for (y = 0;y<20;y++)
        {
            if (x == 0 || x == 78 || y == 0 || y == 19)
            { //creates the boarder
                board[x][y] = 254;
            }
            else
            { //empties the space in the boarder
            board[x][y] = ' ';
            }
        }
    }

    //adds the snake to the board
    board[headX][headY] = 'X';
    board[TailX][TailY] = '+';

    food();

    //draws the initial screen
    for (y = 0;y<20;y++)
    {
        cout<<"\n";
        for (x = 0;x<79;x++)
        {
            cout<<board[x][y];
        }
    }

    movement();

    return 0;
}

void food()
{

    if (fruit == 0)
    {
        srand(time(NULL));
        foodx = rand()%77;
        while (foodx == 0 )
        {
            foodx = rand()%77;
        }
        foody = rand()%18;
        while (foody == 0 )
        {
            foody = rand()%18;
        }

        board[foodx][foody] = 'o';

    }
}

void keypress(void* args)
{
    while (1)
    {
    if (GetAsyncKeyState( 0x1B ) == true) //escape
    {
    exit(0);
    }
        if (GetAsyncKeyState( 0x25 ) == true) //left
        {
            direction = left;
        }
        if (GetAsyncKeyState( 0x26 ) == true) //up
        {
            direction = up;
        }
        if (GetAsyncKeyState( 0x27 ) == true) //right
        {
            direction = right;
        }
        if (GetAsyncKeyState( 0x28 ) == true) //down
        {
            direction = down;
        }
    Sleep(1000);
    }
}

void highscore()
{
    ifstream hscore ("High-score.txt");

    hscore>>Hscore;
}

void drawscreen()
{
    int snake = 0;

    if (headX == foodx and headY == foody)
    {
        score += 5;
        if (score > Hscore)
    {
        ofstream hs ("High-score.txt");
        hs<<score;
        Hscore = score;
    }
        fruit = 0;

        snake = 1;
    }

    system("CLS");

    board[headX][headY] = 'X';
    board[TailX][TailY] = '+';

    cout<<"Score: "<<score<<"                                   Highscore: "<<Hscore;
    for (int y = 0;y<20;y++)
    {
        cout<<"\n";
        for (int x = 0;x<79;x++)
        {
            cout<<board[x][y];
        }
    }
    if (snake == 1)
    {
        snakeplus();
    }
}

void movement()
{ //may need to redo the way it moves a little
    while (1)
    {
    switch(direction)
    {
        case left: //left
        if (headX - 1 == 0 or board[headX - 1][headY] == '+')
            { //if it's gonna hit the boarder or part of the snake
                gameover();
            }
            else
            {

                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;

                board[headX][headY] = ' ';
                headX -= 1;
                drawscreen();

            }
        break;

        case up: //up
        if (headY - 1 == 0 or board[headX][headY - 1] == '+')
            {
                gameover();
            }
            else
            {
                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;

                board[headX][headY] = ' ';
                headY -= 1;
                drawscreen();
            }
        break;

        case right: //right
        if (headX + 1 == 78 or board[headX + 1][headY] == '+')
            {
                gameover();
            }
            else
            {
                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;

                board[headX][headY] = ' ';
                headX += 1;
                drawscreen();
            }
        break;

        case down: //down
        if (headY + 1 == 19 or board[headX][headY + 1] == '+')
            {
                gameover();
            }
            else
            {
                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;

                board[headX][headY] = ' ';
                headY += 1;
                drawscreen();
            }
        break;

        default:
        // direction = left; //don't use until the direction = 5 problem is worked out
        break;


    }
    Sleep(75);
    }
}

int gameover()
{
    if (score > Hscore)
    {
        ofstream hs ("High-score.txt");
        hs<<score;
    }

    system("CLS");

    cout<<"Good job, you got "<<score<<" points";
    cin.get();
    exit(0);
}

void snakeplus()
{
    if (snakeparts >= snake_max_length)
    {
        movement();
    }
    snakeparts++;
        switch (direction)
        {
            case left:
            board[TailX + 1][TailY] = '+';
            direction = 5; //keep until you find a way to add to the snake and make it move
            break;

            case up:
            board[TailX][TailY + 1] = '+';
            direction = 5;
            break;

            case right:
            board[TailX - 1][TailY] = '+';
            direction = 5;
            break;

            case down:
            board[TailX][TailY - 1] = '+';
            direction = 5;
            break;
        }
    drawscreen(); //don't use after the direction = 5 problem is solved
    food();
}


I know that the coding is messy and could be better but I'm still a beginner and don't understand any of the more complicated functions. I was hoping to be able to do this using just basics without having to learn anything too advance by using some creative programming, but I'm seeing that that may not be the case. Also, with help, I'd prefer it if you didn't just give me a modified code and say this will work. I don't want a working version of the program but someone to tell me what I could do to make it work and me, myself, have to figure out how to make that into code. Remember though, I'm still a beginner.

*Note
The code is still incomplete in more ways than just the problem mentioned, so some things may seem useless, unneeded, strange,etc.
Last edited on
I went ahead and redid the way it moves a little and it works perfectly except for one major bugs:
when the tail is added two one of the +'s isn't deleted when turning since it only stores location of the last + when deleting them

the new code is:
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
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <process.h>
#include <fstream>
#include <ctime>

#define snake_max_length 10
#define left 0
#define up 1
#define right 2
#define down 3

using namespace std;

void movement();
void food();
void highscore();
void keypress(void*);
void drawscreen();
int gameover();
void snakeplus();

int Hscore = 0;
char board[79][20];
int direction = 0; //0-left 1-up 2-right 3-down
int fruit = 0;
int score = 0;
int snakeparts = 2;

int taileraseX = 40,taileraseY = 10; //these are used to determine where the tailed ended last move so it can be changed to a ' '
                                    //defined up here so they aren't always 1.

int foodx;
int foody;

int headX = 39;
int headY = 10;
int TailX = 40;
int TailY = 10;

int main()
{
    system("TITLE Snake Game - by Niven");
    system("COLOR 0F");

    _beginthread( keypress, 0, (void*)0 );

    highscore();

    int x,y;

    cout<<"Score: "<<score<<"                                   Highscore: "<<Hscore; //Don't forget this in the drawscreen function

    for (x = 0;x<79;x++)
    { //sets values to the board array
        for (y = 0;y<20;y++)
        {
            if (x == 0 || x == 78 || y == 0 || y == 19)
            { //creates the boarder
                board[x][y] = 254;
            }
            else
            { //empties the space in the boarder
            board[x][y] = ' ';
            }
        }
    }

    //adds the snake to the board
    board[headX][headY] = 'X';
    board[TailX][TailY] = '+';

    food();

    //draws the initial screen
    for (y = 0;y<20;y++)
    {
        cout<<"\n";
        for (x = 0;x<79;x++)
        {
            cout<<board[x][y];
        }
    }

    movement();

    return 0;
}

void food()
{

    if (fruit == 0)
    {
        srand(time(NULL));
        foodx = rand()%77;
        while (foodx == 0 )
        {
            foodx = rand()%77;
        }
        foody = rand()%18;
        while (foody == 0 )
        {
            foody = rand()%18;
        }

        board[foodx][foody] = 'o';

    }
}

void keypress(void* args)
{
    while (1)
    {
    if (GetAsyncKeyState( 0x1B ) == true) //escape
    {
    exit(0);
    }
        if (GetAsyncKeyState( 0x25 ) == true) //left
        {
            direction = left;
        }
        if (GetAsyncKeyState( 0x26 ) == true) //up
        {
            direction = up;
        }
        if (GetAsyncKeyState( 0x27 ) == true) //right
        {
            direction = right;
        }
        if (GetAsyncKeyState( 0x28 ) == true) //down
        {
            direction = down;
        }
    Sleep(1000);
    }
}

void highscore()
{
    ifstream hscore ("High-score.txt");

    hscore>>Hscore;
}

void drawscreen()
{
    int snake = 0;
    // int x,y;

    if (headX == foodx and headY == foody)
    {
        score += 5;
        if (score > Hscore)
    {
        ofstream hs ("High-score.txt");
        hs<<score;
        Hscore = score;
    }
        fruit = 0;

        snake = 1;
    }

    system("CLS");

    //plot snake location
    board[headX][headY] = 'X';
    // board[TailX][TailY] = '+';
    board[taileraseX][taileraseY] = ' ';
    for (int x = 1;x<snakeparts;x++)
    {
        switch(direction)
        {
            case left:
            board[headX + x][headY] = '+';

            taileraseX = headX + x;
            taileraseY = headY;
            break;

            case up:
            board[headX][headY + x] = '+';

            taileraseX = headX;
            taileraseY = headY + x;
            break;

            case right:
            board[headX - x][headY] = '+';

            taileraseX = headX - x;
            taileraseY = headY;
            break;

            case down:
            board[headX][headY - x] = '+';

            taileraseX = headX;
            taileraseY = headY - x;
            break;

        }
    }

    /* for (x = 0;x<79;x++)
    { //checks for tail hitting boarder
        for (y = 0;y<20;y++)
        {
            if ( (x == 0 || x == 78 || y == 0 || y == 19) && (board[x][y] == ' ' || board[x][y] == '+'))
            {
                gameover();
            }
        }
    } */ //not sure if it should be implemented or not

    cout<<"Score: "<<score<<"                                   Highscore: "<<Hscore;
    for (int y = 0;y<20;y++)
    {
        cout<<"\n";
        for (int x = 0;x<79;x++)
        {
            cout<<board[x][y];
        }
    }
    if (snake == 1)
    {
        snakeplus();
    }
}

void movement()
{
    while (1)
    {
    switch(direction)
    {
        case left: //left
        if (headX - 1 == 0 or board[headX - 1][headY] == '+')
            { //if it's gonna hit the boarder or part of the snake
                gameover();
            }
            else
            {
                /*
                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;
                */ //used in the original way for moving before it was improved

                board[headX][headY] = ' ';
                headX -= 1;
                drawscreen();

            }
        break;

        case up: //up
        if (headY - 1 == 0 or board[headX][headY - 1] == '+')
            {
                gameover();
            }
            else
            {
                /*
                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;
                */

                board[headX][headY] = ' ';
                headY -= 1;
                drawscreen();
            }
        break;

        case right: //right
        if (headX + 1 == 78 or board[headX + 1][headY] == '+')
            {
                gameover();
            }
            else
            {
                /*
                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;
                */

                board[headX][headY] = ' ';
                headX += 1;
                drawscreen();
            }
        break;

        case down: //down
        if (headY + 1 == 19 or board[headX][headY + 1] == '+')
            {
                gameover();
            }
            else
            {
                /*
                board[TailX][TailY] = ' ';
                TailX = headX;
                TailY = headY;
                */

                board[headX][headY] = ' ';
                headY += 1;
                drawscreen();
            }
        break;

        default:
        // direction = left; //don't use until the direction = 5 problem is worked out
        break;


    }
    Sleep(125);
    }
}

int gameover()
{
    if (score > Hscore)
    {
        ofstream hs ("High-score.txt");
        hs<<score;
    }

    system("CLS");

    cout<<"Good job, you got "<<score<<" points";
    cin.get();
    exit(0);
}

void snakeplus()
{
    if (snakeparts >= snake_max_length)
    {
        movement();
    }
    snakeparts++;
        /*
        switch (direction)
        {
            case left:
            board[TailX + 1][TailY] = '+';
            // direction = 5; //keep until you find a way to add to the snake and make it move
            break;

            case up:
            board[TailX][TailY + 1] = '+';
            // direction = 5;
            break;

            case right:
            board[TailX - 1][TailY] = '+';
            // direction = 5;
            break;

            case down:
            board[TailX][TailY - 1] = '+';
            // direction = 5;
            break;
        }
        */ //used in the old system of moving the snake. buggy if used in the new one
    // drawscreen(); //don't use after the direction = 5 problem is solved
    food();
}
Last edited on
For now, I'll just say:
DON'T USE SYSTEM. At the beggining of your progam, you can:
1
2
SetConsoleTitle("Snake Game - by Niven");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);

To do the exact same thing, in a better way.
Last edited on
Topic archived. No new replies allowed.