-
Notifications
You must be signed in to change notification settings - Fork 1
/
SERPENTINO.cpp
283 lines (235 loc) · 6.02 KB
/
SERPENTINO.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
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
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
// Map dimensions
const int MAP_WIDTH = 20;
const int MAP_LENGTH = 40;
const int MAP_SIZE = MAP_WIDTH * MAP_LENGTH;
// Map array
int Map[MAP_SIZE];
// Snake head details
int headxpos;
int headypos;
int direction;
// The snake length
int bodyLength = 3;
// Score
int score = 0;
// Initial 0.3 second for sleep
int sec = 300;
// Determine if game is running
bool running = true;
// Returns graphical character for display from map value
char getMapValue(int value)
{
// Returns a part of snake body
if (value > 0) return '0';
switch (value) {
// Return vertical wall
case -1: return '|';
// Return honrizontal wall
case -2: return '-';
// Return food
case -3: return 'X';
}
}
// Print the map
void printMap()
{
for (int x = 0; x < MAP_WIDTH; ++x) {
for (int y = 0; y < MAP_LENGTH; ++y) {
// Prints the value at current x,y location
std::cout << getMapValue(Map[x + y * MAP_WIDTH]);
}
// Newline
std::cout << std::endl;
}
}
// Make food
void generateFood()
{
int xFood = 0;
int yFood = 0;
srand (time(NULL));//Truly random
do {
// Generate random x and y values within the map
xFood = rand() % (MAP_WIDTH - 2) + 1;
yFood = rand() % (MAP_LENGTH - 2) + 1;
// If location is not free try again
} while (Map[xFood + yFood * MAP_WIDTH] != 0);
// Place new food
Map[xFood + yFood * MAP_WIDTH] = -3;
}
// Initialize the map and snake position
void initialMap()
{
// Places the initial head location in middle of map
headxpos = MAP_WIDTH / 2;
headypos = MAP_LENGTH / 2;
Map[headxpos + headypos * MAP_WIDTH] = 1;
// Places left and right walls
for (int x = 0; x < MAP_WIDTH; ++x) {
Map[x] = -1;
Map[x + (MAP_LENGTH - 1) * MAP_WIDTH] = -1;
}
// Places top and bottom walls
for (int y = 0; y < MAP_LENGTH; y++) {
Map[0 + y * MAP_WIDTH] = -2;
Map[(MAP_WIDTH - 1) + y * MAP_WIDTH] = -2;
}
// Generates first food
generateFood();
}
// Move the head to new position
void move(int dx, int dy, int n)
{
// determine new head position
int xNewPo = headxpos + dx;
int yNewPo = headypos + dy;
// Check if there is food at location
if (Map[xNewPo + yNewPo * MAP_WIDTH] == -3) {
// Increase body length and score
bodyLength++;
score++;
// Increase speed
if (sec > 50 && n == 1) {
sec -= 10;
}
// Generate new food on map
generateFood();
}
// Check location is free
else if (Map[xNewPo + yNewPo * MAP_WIDTH] != 0) {
running = false;
}
// Move head to new location, increase by 1
headxpos = xNewPo;
headypos = yNewPo;
Map[headxpos + headypos * MAP_WIDTH] = bodyLength + 1;
}
// Change direction from user input
void changeDirection(char key)
{
switch (key) {
case 'w': case 'W':
if (direction != 1) direction = 0;
break;
case 's': case 'S':
if (direction != 0) direction = 1;
break;
case 'd': case 'D':
if (direction != 3) direction = 2;
break;
case 'a': case 'A':
if (direction != 2) direction = 3;
break;
}
}
// Update the map after each move
void update(int n)
{
// Move in direction indicated
switch (direction) {
case 0: move(-1, 0, n);
break;
case 1: move( 1, 0, n);
break;
case 2: move( 0, 1, n);
break;
case 3: move( 0,-1, n);
break;
}
// Reduce snake values on map by 1
for (int i = 0; i < MAP_SIZE; i++) {
if (Map[i] > 0) Map[i]--;
}
}
// Clear screen to continue
void clearScreen()
{
// Clear the screen
system("cls");
}
// Game over screen
void gameOver()
{
Sleep(1000);
clearScreen();
// Print out game over text
std::cout << "\n\n\t\t!GAME OVER!\n" << std::endl << "\t Your score is: " << score;
// Stop console from closing instantly
std::cin.ignore();
}
// Loading screen
void load()
{
int timer = 2000;
std::cout << "\n\n\t\tLoading\n\t ";
for (int i = 1; i <= 11; i++) {
printf("#");
Sleep(timer);
if (timer > 500) timer -= 500;
}
}
void options()
{
std::cout << "Choose your mode" << std::endl;
std::cout << "0. Normal: The snake won't speed up'" << std::endl;
std::cout << "1. Hard : The snake will speed up" << std::endl;
}
void gui()
{
printf ("\tWELCOME TO SERPENTINO \n");
printf ("\n PRESS ENTER TO CONTINUE");
getchar();
clearScreen();
printf ("Playing with snake is easy \n");
printf ("Using w,a,s,d to control your snake \n");
printf ("Control your snake to eat food on map \n");
printf ("Hit the wall will let your snake go to heaven \n");
printf ("WARNING: \n");
printf ("PLEASE DON'T USE THE DIRECTION CONTRARY WITH YOUR SNAKE MOVING DIRECTION \n");
printf ("PLAY SAFE AND DON'T EAT YOUR BODY PLEASE \n'");
printf ("HAVE FUN \n");
printf ("Press Enter to continue");
getchar();
clearScreen();
}
//main function printMap then clearMap loop
void run()
{
int n;
gui();
options();
std::cin >> n;
clearScreen();
// Loading screen
load();
// Initialize the map
initialMap();
// While not hitting
while (running == true) {
// If a key is pressed
if (kbhit()) {
// Change to direction determined by key pressed
changeDirection(getch());
}
// Upate the map
update(n);
// Clear the screen
clearScreen();
//clrscr();
// Print the map
printMap();
// wait
Sleep(sec);
}
gameOver();
}
int main()
{
run();
return 0;
}