Published by
Aug 23, 2013 (last update: Jan 21, 2014)

Snake Cpp [Console]

Score: 3.4/5 (210 votes)
*****
Hey,

I've decided to share with you my snake game ,I hope you will like it.
I've added some comments to the game as well,if you want to understand how it works.

Foxefde


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

#include <vector>
#include <iostream>
#include <conio.h> //By Foxefde
#include <windows.h>
#include <ctime>

class Snake
{
private:
    int x,z,Dollars,y,Bonus,time,tail;
    // x - snake head position
    // z = for random number(pineapple generation)
    // y = tail coordinates time = time tail = snakelenght - 1
    char *map; //Map of game
    bool L,R,D,U,A; //L = LEFT R = RIGHT D = DOWN U = UP A = JUST A BOOL
    std::vector <int> past; //Special vector for deleting old parts

public:
    Snake()
    {
        map = new char [2000];
        L = 0;
        R = 1; //bool 1 = true bool 0 = false
        D = 0;
        U = 0;
        A = 0;
        tail = 1; //Actually it will not have a tail until it eats first apple
        x = 1000; //Position of snake at the beggining
        Dollars = 0; //Money
        Bonus = 0;
        time = 40; //For bonus apples
        z = 1; //So first apple generation will not be bugged --
	}

    ~Snake()
    {
        delete [] map;
    }

    void Graphics (); //Drawing game
    void GameLogic(); //Does it need comment?
    void KEYBOARD (); //Checking for input
    void Pineapple(); //Generating new apple
    int GameOver (); //Game over ?.
    void Start(); //Start -_-
    friend void clearscreen(); //It's actually not like system("cls"),but works almost same
    friend void sp();//Choose color function
    friend void s(); //Forget choose color choice
};
////////////////////////////////////////////////////////////////////////////////// Windows
void clearscreen()
{
    HANDLE hOut;
    COORD Position;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    Position.X = 0;
    Position.Y = 0;
    SetConsoleCursorPosition(hOut, Position);
}

void sp(int choosecolor)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), choosecolor); //FUNCTION OF COLOR
}
void s()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    //FUNCTION TO ..EHM FORGET COLOR,not sure how to say I know: Stop using color
}
////////////////////////////////////////////////////////////////////////////////// ^ Windows
void Snake::Start()
{
    for(int p = 0;p < 2000;p++)//////////////////////////////////////// Making array empty
    {
        map[p] = ' ';
    }

    map[ x ] = char(219); //Let's make head - block
    Pineapple(); //Let's generate a pineapple
    Graphics(); //GOOOOOOO
}
//////////////////////////////////////////////////////////////////////////////////////////
int Snake::GameOver()
{
    Sleep(2500);
    system("cls");
    std::cout <<     std::endl << "Oops...You earned " << Dollars + Bonus << " Dollars...";
    //Better luck next time.
    Sleep(1800);
    return 0;
}
void Snake::Graphics()
{
    sp(697); //CHoosing color
    std::cout << Dollars + Bonus << " Dollars By Foxefde 2013 ";
    std::cout << "\n";
    s();
    for(int u = 0;u < 50;u++) //Top border
    {
        sp(750);
        std::cout << char(219);
        s();
    }

	std::cout <<     std::endl;
/***********************************************************************/
    for(int x1 = 0;x1 < 2000 ; x1++) //DRAWING BOARD!~
    {
        if(x1 % 50 == 0 && x1 != 0)
        {
            std::cout <<     std::endl;
        }
        if(x1 % 50 == 0 || (x1-(x1 / 50)) % 49 == 0)
		{
            map[x1] = char(219);
            sp(750);
            std::cout << map[x1];
            s();
		}
	else if(map[x1]!=char(219) && map[x1]!=map[z])
    {
        std::cout << map[x1];
    }
    else if(x1 == z)
    {
        sp(10);
        std::cout << map[x1];
        s();
    }
    else
    {
        sp(750);
        std::cout << map[x1];
        s();
	}

    }

	std::cout <<     std::endl; //New line
/***********************************************************************/
    for(int u = 0;u < 50;u++) //Bottom border
    {
        sp(750);
        std::cout << char(219);
        s();
    }
    if(U == 1 || D == 1)
	{ //WITHOUT IT ,GOING UP/DOWN IS MUCH FASTER THAN < >
        Sleep(19);
    }
    if(map[z] == char(5)) //If apple is bonus ,then start decreasing time to get it!
    {
        time--;
        if(time == 0) //If you were too slow ,you lose a dollar and a new apple is generated
		{
            Dollars++;
            time = 40;
            Pineapple();
        }
    }

    clearscreen();
    GameLogic();
}

void Snake::Pineapple()
{
    map[z] = ' ';
    if(Dollars % 8 != 0 || Dollars == 0)
    //Bonus apple - every 8 normal apples eaten ,so we need to check - generate an apple or a bonus apple
    {
        while(map[z] != ' ' && z % 50 != 0 && (z-(z/50)) % 49!= 0);
    //Keep generating new coordinates of pineapple until that place is empty
        {
            z = rand()%2000 + 1;
		}

	    map[z] = char(229);
        //(z(z/50)) % 49 != 0 that means ,if z isn't 49+50n (49,99,149,199...)
    } //Logic ftw ,yeh?:D

    else //BONUS APPLE
    {
        while(map[z] != ' ' && z % 50 != 0 && (z-(z/50)) % 49 != 0);
        //Keep generating new coordinates of pineapple until that place is empty
        {
            z = rand()%2000 + 1;
        }
        map[z] = char(5);
    }

}

void Snake::KEYBOARD()
{
/***********************************************************************************************/
if(_kbhit()) //If player clicks something

{
    char key;
    key = _getch(); //Now this click is key
    switch( key )
    {
        case 'd':
            //cases below,nothing special ,you should understand that easily
            //,but let me give a simple explanation of first case:
        {
            if (L == 0) //So if a player has clicked 'd' ,then.1:We check if snake is not
            //going left,because how can she turn right,if she's going left?Teleporting?..
            {
                L = 0, U = 0, D = 0, //Left = false UP = false Down = false
                R = 1; //Right = true !
			}
            break; //We break it,end of the case.Identic with other cases..
        }

        case 'w':
        {
            if (D == 0)
            {
                L = 0, D = 0, R = 0,
                U = 1;
            }
        break;
        }

        case 'a':
        {
            if (R == 0)
            {
                D = 0, U = 0, R = 0,
                L = 1;
			}
        break;
        }

        case 's':
        {
            if (U == 0)
            {
                L = 0, U = 0, R = 0,
                D = 1;
            }
        break;
        }

    }

}
}

/**************************************************************************/
void Snake::GameLogic()
{
    past.insert(past.begin(),x); //Inserting past x position to vector
    KEYBOARD();
    if(R == 1) //If snake is going right
    {
        x++;
    }
    else if(L == 1) //If snake is going left
    {
        x--;
    }
    else if(U == 1) //If snake is going up
    {
        x-= 50;
    }
    else //If snake is going down(only case left)
    {
        x+= 50;
    }
    if(map[ x ] == char(219) || x % 50 == 0 || x > 2000 || x < 0 || (x-(x / 50)) % 49 == 0)
        //If it hits herself or border...
    {
        GameOver();
        return;
    }
    if(map[x] == char(229))
    //If it eats an pineapple also A becomes true,that means the
    //very end of the snake(tail) will not be deleted ( line 276 ) for 1 frame
    {
        A = true;
        tail++; //It eats a big apple ,so snake becomes heavier
        Pineapple(); //Let's generate new apple!
        Dollars++; //Apple had some dollars in it,congrulations!
	}
    else if (map[x] == char(5)) //Same,but maybe it has just eaten bonus apple?
    {
        A = true;
        tail++;
        Pineapple();
        Bonus+=time;
    }

    map[x] = char(219);
    //When it touches apple - head becomes an apple,so we need to change it.
/**********/
    if(A == false) //If snake has just eaten an apple
    {
        y = past[past.size() - tail]; //D E L E T I N G past tail from the map!
        map[y] = ' ';
    }

    A = false; //So the next time line 270 will work again,if apple is no eaten ///
    if(tail!=1)
    {
        for(int u = past.size() - 2;u > 0;u--)
		{
            past[u+1] = past[u];
        } //Try your best to understand what's happening here,
        //let's say this is an exercise for you from Foxefde
    past.erase(past.end()-tail);
    }
    else
    {
        past.erase(past.begin()); //I could do it without erases,
    } //
    Graphics();
}
int main()
{
    srand((unsigned)time(0)); //So random numbers will be always random.
    Snake SNAKE; //creating class m
    SNAKE.Start(); //Starting main function/
}
//By Foxefde 2013 www.youtube.com/watch?v=DBOzkCbdoqQ
//Also check this www.youtube.com/watch?v=JkEHImooH74
//Skype Mantasxxl3