-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake game doesn't figured.cpp
214 lines (177 loc) · 3.92 KB
/
Snake game doesn't figured.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
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
struct position {
int x, y;
};
class field_cls {
static const int height;
static const int width;
char ** field;
field_cls(const field_cls &);
field_cls operator =(const field_cls &);
public:
field_cls() {
field = new char*[field_cls::height];
for (int c = 0; c < field_cls::height; ++c) {
field[c] = new char[field_cls::width];
}
}
~field_cls() {
for (int c = 0; c < field_cls::height; ++c) {
delete[] field[c];
}
delete[] field;
}
void print() {
for (int c = 0; c < height; ++c) {
for (int r = 0; r < width; ++r) {
cout << field[c][r];
}
cout << endl;
}
}
void clear() {
for (int c = 0; c < height; ++c) {
for (int r = 0; r < width; ++r) {
field[c][r] = ' ';
}
}
}
int get_width() const { return width; }
int get_height() const { return height; }
void draw(int y, int x, char what) {
//y = (y < 0) ? 0 : (y >= height ? height - 1 : y);
//x = (x < 0) ? 0 : (x >= width ? width - 1 : x);
field[y][x] = what;
}
} field;
class food_cls {
position pos;
char symbol;
public:
food_cls() : symbol('X'), pos() {
pos.x = pos.y = -1;
}
void set_pos(int x, int y) {
pos.x = x;
pos.y = y;
}
void reposition(const field_cls & field) {
pos.x = rand() % field.get_width();
pos.y = rand() % field.get_height();
}
int get_x() const { return pos.x; }
int get_y() const { return pos.y; }
char get_symbol() const { return symbol; }
} food;
class snake_cls {
enum { UP, DOWN, LEFT, RIGHT } dir;
char symbol, head_symbol;
position pos[100];
position & head;
int speed;
int size;
bool can_turn;
public:
snake_cls(int x, int y) :
symbol('o'), head_symbol('O'), pos(),
speed(1), size(1), dir(RIGHT),
head(pos[0]), can_turn(true)
{
pos[0].x = x;
pos[0].y = y;
}
bool check_food(const food_cls & food) {
if (food.get_x() == head.x && food.get_y() == head.y) {
size += 1;
return true;
}
return false;
}
void get_input(const field_cls & field) {
if (GetAsyncKeyState(VK_UP) && dir != DOWN) {
dir = UP;
}
if (GetAsyncKeyState(VK_DOWN) && dir != UP) {
dir = DOWN;
}
if (GetAsyncKeyState(VK_LEFT) && dir != RIGHT) {
dir = LEFT;
}
if (GetAsyncKeyState(VK_RIGHT) && dir != LEFT) {
dir = RIGHT;
}
}
void move(const field_cls & field) {
position next = { 0, 0 };
switch (dir) {
case UP:
next.y = -speed;
break;
case DOWN:
next.y = speed;
break;
case LEFT:
next.x = -speed;
break;
case RIGHT:
next.x = speed;
}
for (int c = size - 1; c > 0; --c) {
pos[c] = pos[c - 1];
}
head.x += next.x;
head.y += next.y;
if (head.x < 0 || head.y < 0 || head.x >= field.get_width() || head.y >= field.get_height()) {
throw "DEADD!!!!";
}
}
void draw(field_cls & field) {
for (int c = 0; c < size; ++c) {
if (c == 0) {
field.draw(pos[c].y, pos[c].x, head_symbol);
}
else {
field.draw(pos[c].y, pos[c].x, symbol);
}
}
}
int get_x() const { return head.x; }
int get_y() const { return head.y; }
char get_symbol() const { return symbol; }
} snake(1, 1);
const int field_cls::height = 24;
const int field_cls::width = 79;
int main() {
system("Color 3f");
field.clear();
food.set_pos(5, 5);
while (1) {
field.clear();
snake.get_input(field);
try {
snake.move(field);
}
catch (const char * er) {
field.clear();
cerr << er << endl;
system("pause");
return -1;
}
snake.draw(field);
field.draw(food.get_y(), food.get_x(), food.get_symbol());
if (snake.check_food(food)) {
food.reposition(field);
}
field.print();
Sleep(10 / 3); // snake speed
system("cls");
}
return 0;
}
field_cls field_cls::operator=(const field_cls &)
{
return field_cls();
}