-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
172 lines (152 loc) · 4.64 KB
/
Copy pathmain.cpp
File metadata and controls
172 lines (152 loc) · 4.64 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
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define CANVAS_WIDTH 10
#define CANVAS_HEIGHT 10
char canvas[CANVAS_WIDTH][CANVAS_HEIGHT]; //x,y
char pressed_key;
int snake_length = 0;
int snake_cords[2] = { 0,0 }; //x,y
int old_snake_cords[2] = { 0,0 }; //x,y
int food_cords[2];
clock_t old_time, now;
enum directions {up, down, left, right};
int direction = right;
int old_direction;
int tail_length = 0;
int length_counter = 0;
int check_cords[2];
int tail_cords[CANVAS_HEIGHT * CANVAS_WIDTH - 1][2] = {};
unsigned int tail_length_increment_flag = 0;
void change_food_cords(int* cords);
void setCursorPosition(int x, int y);
void game_over(const char message[]);
int main() {
system("cls");
old_time = clock();
srand(time(NULL));
for (size_t i = 0; i < CANVAS_WIDTH; i++) {
for (size_t x = 0; x < CANVAS_HEIGHT; x++) {
canvas[i][x] = '*';
}
}
change_food_cords(food_cords);
canvas[food_cords[0]][food_cords[1]] = '#';
while (1) {
while (!_kbhit()) {
setCursorPosition(0, 0);
now = clock();
if (now - old_time >= 700) {
if (tail_length_increment_flag == 1) {
tail_length++;
tail_length_increment_flag = 0;
}
old_time = now;
old_snake_cords[0] = snake_cords[0];
old_snake_cords[1] = snake_cords[1];
//check snakes direction and change snakes cordinates
if (direction == up) {
if (--snake_cords[1] < 0)
game_over("Don't try to eat walls.");
}
else if (direction == down) {
if (++snake_cords[1] >= CANVAS_HEIGHT)
game_over("Don't try to eat walls.");
}
else if (direction == left) {
if (--snake_cords[0] < 0)
game_over("Don't try to eat walls.");
}
else if (direction == right) {
if (++snake_cords[0] >= CANVAS_WIDTH)
game_over("Don't try to eat walls.");
}
for (size_t i = 0; i < CANVAS_WIDTH; i++) {
for (size_t x = 0; x < CANVAS_HEIGHT; x++) {
canvas[i][x] = '*';
}
}
canvas[food_cords[0]][food_cords[1]] = '#';
if (tail_length != 0) {
tail_cords[tail_length - 1][0] = -1;
tail_cords[tail_length - 1][1] = -1;
for (int i = tail_length - 1; i > 0; i--) {
tail_cords[i][0] = tail_cords[i - 1][0];
tail_cords[i][1] = tail_cords[i - 1][1];
}
tail_cords[0][0] = old_snake_cords[0];
tail_cords[0][1] = old_snake_cords[1];
}
printf("\n%d %d\n", snake_cords[0], snake_cords[1]);
//check if the char is food
if (canvas[snake_cords[0]][snake_cords[1]] == '#') {
tail_length_increment_flag = 1;
change_food_cords(food_cords);
while (canvas[food_cords[0]][food_cords[1]] == '@' || canvas[food_cords[0]][food_cords[1]] == 'o') {
change_food_cords(food_cords);
}
canvas[food_cords[0]][food_cords[1]] = '#';
}
for (size_t i = 0; i < tail_length; i++) {
if (tail_cords[i][0] >= 0 && tail_cords[i][0] < CANVAS_WIDTH && tail_cords[i][1] >= 0 && tail_cords[i][1] < CANVAS_HEIGHT) {
canvas[tail_cords[i][0]][tail_cords[i][1]] = 'o';
}
}
if (canvas[snake_cords[0]][snake_cords[1]] == 'o')
game_over("Stepped on tail.");
if (snake_cords[0] >= 0 && snake_cords[0] < CANVAS_WIDTH && snake_cords[1] >= 0 && snake_cords[1] < CANVAS_HEIGHT) {
canvas[snake_cords[0]][snake_cords[1]] = '@';
}
else {
game_over("Snake out of bounds.");
}
for (size_t i = 0; i < CANVAS_WIDTH; i++) {
printf("\n");
for (size_t x = 0; x < CANVAS_HEIGHT; x++) {
printf("%c ", canvas[x][i]);
}
}
printf("Score: %d", tail_length);
}
}
pressed_key = _getch();
if (pressed_key == 'w') {
if (direction == down && tail_length > 0)
game_over("Don't try to eat yourself.");
direction = up;
}
else if (pressed_key == 's') {
if (direction == up && tail_length > 0)
game_over("Don't try to eat yourself.");
direction = down;
}
else if (pressed_key == 'a') {
if (direction == right && tail_length > 0)
game_over("Don't try to eat yourself.");
direction = left;
}
else if (pressed_key == 'd') {
if (direction == left && tail_length > 0)
game_over("Don't try to eat yourself.");
direction = right;
}
else if (pressed_key == 'q')
game_over("You surrendered.");
}
}
void change_food_cords(int* cords) {
cords[0] = rand() % 10;
cords[1] = rand() % 10;
}
void setCursorPosition(int x, int y) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = { (SHORT)x, (SHORT)y };
SetConsoleCursorPosition(hOut, coord);
}
void game_over(const char message[]) {
setCursorPosition(0, CANVAS_HEIGHT + 5);
printf("Game Over! - %s", message);
exit(1);
}