-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
251 lines (209 loc) · 4.95 KB
/
main.cpp
File metadata and controls
251 lines (209 loc) · 4.95 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
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
// License GPL v3.0
// by Davide Fassio
#include <ncurses.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include "color.hpp"
using namespace std;
#define ROW 24 // Rows
#define COL 50 // Colomns
#define LEN ROW*COL // Max lenght of the snake
void print_map(int);
void opening();
void end(int);
void winend();
int main(int argc, char ** argv){
srand(time(0));
// Initialize the windows
initscr();
noecho();
refresh();
curs_set(0);
halfdelay(1);
// Define all colors for the text
start_color();
init_pair(WALL, COLOR_WHITE , COLOR_BLACK);
init_pair(SB , COLOR_BLUE , COLOR_BLACK);
init_pair(SH , COLOR_GREEN , COLOR_BLACK);
init_pair(FD , COLOR_RED , COLOR_BLACK);
init_pair(SCO , COLOR_YELLOW, COLOR_BLACK);
// Main program
int x, y, i, score = 0, act_len = 4;
char move = 'w', prev_move = 'w';
int food[2] = {rand()%49+1, rand()%23+1};
int snake[LEN][2];
// Initialize the snake
for(i = 0; i < act_len; i++){
snake[i][0] = (int) COL / 2;
snake[i][1] = ROW - 10 + i;
}
// Initialize the position with the head of the snake
x = snake[0][0];
y = snake[0][1];
// Opening screen
opening();
// First print
print_map(score);
attron(COLOR_PAIR(FD));
mvprintw(food[1], food[0], "@");
attroff(COLOR_PAIR(FD));
attron(COLOR_PAIR(SH));
mvprintw(snake[0][1], snake[0][0], "X");
attroff(COLOR_PAIR(SH));
attron(COLOR_PAIR(SB));
for(i = 1; i < act_len; i++){
mvprintw(snake[i][1], snake[i][0], "O");
}
attroff(COLOR_PAIR(SB));
// Game loop
while(1){
// Wait 0.1s for an input
if((move = getch()) == ERR){
move = prev_move;
}
// In case of wrong key press
m:
switch(move){
case 'w':
prev_move = move;
y--;
break;
case 'a':
prev_move = move;
x--;
break;
case 's':
prev_move = move;
y++;
break;
case 'd':
prev_move = move;
x++;
break;
default:
// Wrong key press
// Move like before
move = prev_move;
goto m;
break;
}
// Food eaten
if(food[0] == x && food[1] == y){
score++;
if(score >= LEN){
// You win!
winend();
return 0;
}
while(1){
food[0] = rand()%(COL - 1) + 1;
food[1] = rand()%(ROW - 1) + 1;
for(i = 0; i < act_len; i++){
if(food[0] == snake[i][0] && food[1] == snake[i][1]){
break;
}
}
if(i == act_len){
break;
}
}
act_len++;
}
// Hit the borders
if(x <= 0 || x > COL || y <= 0 || y > ROW){
// You lose
end(score);
return 0;
}
// Update the snake
for(i = act_len-1; i > 0; i--){
// Move the snake forward
snake[i][0] = snake[i-1][0];
snake[i][1] = snake[i-1][1];
// Collision with the snake
if(snake[i][0] == x && snake[i][1] == y){
// You lose
end(score);
return 0;
}
}
snake[0][0] = x;
snake[0][1] = y;
// Clear the screen
clear();
// Print the borders and score
print_map(score);
// Print the food
attron(COLOR_PAIR(FD));
mvprintw(food[1], food[0], "@");
attroff(COLOR_PAIR(FD));
// Print the snake
attron(COLOR_PAIR(SH));
mvprintw(snake[0][1], snake[0][0], "X");
attroff(COLOR_PAIR(SH));
attron(COLOR_PAIR(SB));
for(i = 1; i < act_len; i++){
mvprintw(snake[i][1], snake[i][0], "O");
}
attroff(COLOR_PAIR(SB));
// Update the window
refresh();
}
//Unexpected end
return 0;
}
/* Print the border of the map,
the score and the escape key */
void print_map(int score){
attron(COLOR_PAIR(WALL));
mvprintw(0, 0, "############## Snake by Davide Fassio ##############");
for(int i = 1; i < ROW + 1; i++){
mvprintw(i, 0, "#");
mvprintw(i, COL + 1, "#");
}
mvprintw(ROW + 1, 0, "####################################################", score);
attroff(COLOR_PAIR(WALL));
attron(COLOR_PAIR(SCO));
mvprintw(ROW + 2, ((int) COL / 2 ) - 5, "Score:%4d", score);
attroff(COLOR_PAIR(SCO));
}
// Opening screen
void opening(){
mvprintw( 6, 6, "###### ## ## #### ## ## #######");
mvprintw( 7, 6, "###### #### ## ## ## ## ## #######");
mvprintw( 8, 6, "## ## ## ## ## ## ## ## ## ");
mvprintw( 9, 6, "###### ## ## ## ######## #### #### ");
mvprintw(10, 6, "###### ## ## ## ######## #### #### ");
mvprintw(11, 6, " ## ## #### ## ## ## ## ## ");
mvprintw(12, 6, "###### ## ### ## ## ## ## #######");
mvprintw(13, 6, "###### ## ## ## ## ## ## #######");
mvprintw(16, 15, "Press enter to start ...");
refresh();
while(getch() != 10);
clear();
}
// Ending screen
void end(int score){
clear();
attron(COLOR_PAIR(SH));
mvprintw(10, 21, "Game over!");
mvprintw(12, 17, "Your score is = %d", score);
mvprintw(14, 14, "Press enter to leave ...");
attroff(COLOR_PAIR(SH));
refresh();
while(getch() != 10);
endwin();
}
// Win ending screen
void winend(){
clear();
attron(COLOR_PAIR(SH));
mvprintw(10, 22, "You win!!");
mvprintw(12, 16, "Your score is = %d", LEN);
mvprintw(14, 14, "Press enter to leave ...");
attroff(COLOR_PAIR(SH));
refresh();
while(getch() != 10);
endwin();
}