-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake Game.cpp
160 lines (142 loc) · 3.68 KB
/
Snake Game.cpp
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
// Snake Game.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <conio.h>
#include <Windows.h>
int g_snakeX, g_snakeY, g_fruitX, g_fruitY, g_score;
int iTailX[100], iTailY[100];
int g_Width = 20;
int g_Height = 17;
bool g_GameOver = false;
int nTail;
enum eDirection {STOP =0, RIGHT, LEFT, UP, DOWN } eDir;
void Setup() {
eDir = STOP;
g_score = 0;
g_snakeX = g_Width / 2;
g_snakeY = g_Height / 2;
g_fruitX = rand() % g_Width;
g_fruitY = rand() % g_Height;
}
void Draw()
{
system("cls");
for (int index = 0; index < g_Width + 2; index++) {
std::cout << "#";
}
std::cout << std::endl;
for (int row = 0; row < g_Height; row++) {
for (int column = 0; column < g_Width; column++) {
if (column == 0)
std::cout << "#";
if (column == g_snakeX && row == g_snakeY)
std::cout << "*";
else if (column == g_fruitX && row == g_fruitY)
std::cout << "%";
else
{
bool bPrintTail = false;
for (int index = 0; index < nTail; index++) {
if (iTailX[index] == column && iTailY[index] == row) {
std::cout << "*";
bPrintTail = true;
}
}
if (!bPrintTail)
std::cout << " ";
}
if (column == g_Width - 1)
std::cout << "#";
}
std::cout << std::endl;
}
for (int bIndex = 0; bIndex < g_Width + 2; bIndex++) {
std::cout << "#";
}
std::cout << std::endl;
std::cout << "Score :" << g_score << std::endl;
}
void Input() {
if (_kbhit()) {
switch (_getch())
{
case 'a':
eDir = LEFT;
break;
case 'w':
eDir = UP;
break;
case 's':
eDir = DOWN;
break;
case 'd':
eDir = RIGHT;
break;
case 'x':
g_GameOver = true;
break;
default:
break;
}
}
}
void algorithm() {
Sleep(150);
int prevX, prevY, prev2X, prev2Y;
prevX = iTailX[0];
prevY = iTailY[0];
iTailX[0] = g_snakeX;
iTailY[0] = g_snakeY;
for (int index = 1; index < nTail; index++) {
prev2X = iTailX[index];
prev2Y = iTailY[index];
iTailX[index] = prevX;
iTailY[index] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (eDir) {
case LEFT:
g_snakeX--;
break;
case RIGHT:
g_snakeX++;
break;
case UP:
g_snakeY--;
break;
case DOWN:
g_snakeY++;
break;
}
//Handle out of boundary cases
if (g_snakeX > g_Width)
g_snakeX = 0;
else if (g_snakeX < 0)
g_snakeX = g_Width - 1;
if (g_snakeY > g_Height)
g_snakeY = 0;
else if (g_snakeY < 0)
g_snakeY = g_Height - 1;
for (int index = 0; index < nTail; index++) {
if (iTailX[index] == g_snakeX && iTailY[index] == g_snakeY)
g_GameOver = true;
}
if (g_snakeX == g_fruitX && g_snakeY == g_fruitY) {
g_score += 10;
g_fruitX = rand() % g_Width;
g_fruitY = rand() % g_Height;
nTail++;
}
}
int main()
{
std::cout << "Welcome to Snake Game\n";
Setup();
while (!g_GameOver) {
Draw();
Input();
algorithm();
}
return 0;
}