-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
207 lines (169 loc) · 5.06 KB
/
Copy pathmain.cpp
File metadata and controls
207 lines (169 loc) · 5.06 KB
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
//
// main.cpp
// Snake_Game
//
//
#include <iostream>
#include <conio.h> // Windows library only.
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x,y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail; //length of tail
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir; // This holds the direction of the snake.
bool hasPrinted = false; // used later in printing the tail.
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2; // This centres the snake at the start
y = height / 2;
fruitX = rand() % width; // random placement of fruit
fruitY = rand() % height;
score = 0; // initalising score to 0 at start
}
void Draw()
{
system("cls"); // to clear terminal at start of game
for (int i = 0; i<width+1; i++){ // Printing top line of map.
std::cout << "#";
}
cout << endl; // move to next line.
// printing perimeter
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j==0) // first character
cout << "#";
if (j == x && i == y)
cout << "O"; // printing head of snake
else if (i == fruitY && j == fruitX)
cout << "F"; // printing fruit.
else
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o"; // if current x and current y co-ordinate equal the tail cordinates. Then we print the tail.
hasPrinted = true; // stating we have printed the tail
}
}
if (!hasPrinted) // if we didn't print the tail, then print a blank space.
{
cout << " ";
}
if (j == width-1)
cout << "#";
}
cout << endl; // move to next line
}
for (int i = 0; i<width+1; i++){ // printing bottom line / wall.
cout << "#";
}
cout << "Score: " << score << endl;
}
void Input() // getting player input "WASD" keys. Note: only works on windows.
{
if (_kbhit()) // checks whether the keyboard button is pressed.
{
switch (_getch()) // gets the code of the key which is pressed.
{
case "a":
dir = LEFT;
break;
case "d":
dir = RIGHT;
break;
case "s":
dir = DOWN;
break;
case "w":
dir = UP;
break;
case "x":
gameOver = true;
break;
}
}
}
void Logic()
{
int prevX = tailX[0]; // previous co-ordinates of tail.
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x; // setting the first element to follow the head of the snake.
tailY[0] = y;
for (int i = 1; i < nTail; i++) // creating movement of the tail.
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir)
{
case LEFT: // If press left then take 1 off the x co-ordinate.
x--;
break;
case RIGHT: // if right, add 1 to x co-ordinate.
x++;
break;
case UP:// add 1 to y co-ordinate
y++;
break;
case DOWN: //subtract 1 from y co-ordinate
y--;
break;
default:
break;
}
// if (x > width || x < 0 || y > height || y < 0){
// gameOver= true; // if exit the map, then game over.
// }
// Alternate view: snake can move from right to left going through walls. Uncomment above to change game structure to end game when hit a wall.
if (x >= width) // if x exceeeds the width, then reposition x coordinate to 0.
{
x = 0;
} else if (x < 0) // if x moves less than 0, then reposition x to the width - 1 (as the width itself is the barrier).
{
x = width - 1;
}
if (y >= height)
{
y = 0;
}else if (y < 0)
{
y = height - 1;
}
// End game if snake hits itself.
for (int i = 0; i<nTail; i++)
{
if (tailX[i] == x && tailY[i] == y) // x coordinate and y coordinate = tail coordinates.
{
gameOver = true;
}
}
if (x == fruitX && y == fruitY) // if you collide with the fruit, increase score and reposition the fruit. Increase the tail length.
{
score += 10;
fruitX = rand() % width; // random placement of fruit again after eaten
fruitY = rand() % height;
nTail ++; // increating tail length by 1
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
}
}