-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.cpp
More file actions
225 lines (190 loc) · 4.17 KB
/
SnakeGame.cpp
File metadata and controls
225 lines (190 loc) · 4.17 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//CSC114-651
//Student: Christopher Yonek (700642859)
//Assignment: Project - Create snake game.
//Date: November 2018
#include "pch.h"
#include <iostream>
#include <conio.h>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
void run();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void changeDirection(char key);
void clearScreen();
void generateFood();
char getMapValue(int value);
// Map dimensions
const int mapwidth = 150;
const int mapheight = 150;
const int size = mapwidth * mapheight;
// The tile values for the map
int map[size];
// Snake head details
int headxpos;
int headypos;
int direction;
// Amount of food the snake has (How long the body is)
int food = 3, size;
// Determine if game is running
bool running;
int main()
{
run();
return 0;
}
// Main game function
void run()
{
// Initialize the map
initMap();
running = true;
while (running) {
// If a key is pressed
if (kbhit()) {
// Change to direction determined by key pressed
changeDirection(getch());
}
// Upate the map
update();
// Clear the screen
clearScreen();
// Print the map
printMap();
// wait 0.5 seconds
std::Thread::Sleep_for();
}
// Stop the vector function by transforming into an array
// Stop console from closing instantly
std::cin.ignore();
}
// Changes snake direction from input
void changeDirection(char key) {
/*
W
A + D
S
1
4 + 2
3
*/
switch (key) {
case 'w':
if (direction != 2) direction = 0;
break;
case 'd':
if (direction != 3) direction = 1;
break;
case 's':
if (direction != 4) direction = 2;
break;
case 'a':
if (direction != 5) direction = 3;
break;
}
}
// Moves snake head to new location
void move(int dx, int dy) {
// determine new head position
int newx = headxpos + dx;
int newy = headypos + dy;
// Check if there is food at location
if (map[newx + newy * mapwidth] == -2) {
// Increase food value (body length)
food++;
// Generate new food on map
generateFood();
}
// Check location is free
else if (map[newx + newy * mapwidth] != 0) {
running = false;
}
// Move head to new location
headxpos = newx;
headypos = newy;
map[headxpos + headypos * mapwidth] = food + 1;
}
// Clears screen
void clearScreen() {
// Clear the screen
system("cls");
}
// Generates new food on map
void generateFood() {
int x = 0;
int y = 0;
do {
// Generate random x and y values within the map
x = rand() % (mapwidth - 2) + 1;
y = rand() % (mapheight - 2) + 1;
// If location is not free try again
} while (map[x + y * mapwidth] != 0);
// Place new food
map[x + y * mapwidth] = -2;
}
// Updates the map
void update() {
// Move in direction indicated
switch (direction) {
case 0: move(-1, 0);
break;
case 1: move(0, 1);
break;
case 2: move(1, 0);
break;
case 3: move(0, -1);
break;
}
// Reduce snake values on map by 1
for (int i = 0; i < size; i++) {
if (map[i] > 0) map[i]--;
}
}
// Initializes map
void initMap()
{
// Places the initual head location in middle of map
headxpos = mapwidth / 2;
headypos = mapheight / 2;
map[headxpos + headypos * mapwidth] = 1;
// Places top and bottom walls
for (int x = 0; x < mapwidth; ++x) {
map[x] = -1;
map[x + (mapheight - 1) * mapwidth] = -1;
}
// Places left and right walls
for (int y = 0; y < mapheight; y++) {
map[0 + y * mapwidth] = -1;
map[(mapwidth - 1) + y * mapwidth] = -1;
}
// Generates first food
generateFood();
}
// Prints the map to console
void printMap()
{
for (int x = 0; x < mapwidth; ++x) {
for (int y = 0; y < mapheight; ++y) {
// Prints the value at current x,y location
std::cout << getMapValue(map[x + y * mapwidth]);
}
// Ends the line for next x value
std::cout << std::endl;
}
}
// Returns graphical character for display from map value
char getMapValue(int value)
{
// Returns a part of snake body
if (value > 0) return 'o';
switch (value) {
// Return wall
case -1: return 'X';
// Return food
case -2: return 'O';
}
}