-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake2d.cpp
308 lines (277 loc) · 7.1 KB
/
Snake2d.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Snake2d.cpp
*
* Created on: Jul 25, 2016
* Author: kenny
*/
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include <list>
#include <iostream>
//#include <thread>
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <sstream>
int foodx, foody;
int xdim, ydim;
int windowX, windowY;
float menuy;
int stepTime;
enum Direction { up, right, down, left };
enum Status {alive, dead, complete};
Status s;
Direction d;//, prevdir;
time_t start;
struct Pair{
Pair(int X, int Y){
x = X;
y = Y;
}
int x;
int y;
};
std::list<Pair> snake;
bool isSnake(int x, int y){
for(std::list<Pair>::iterator i = snake.begin(); i != snake.end(); i++){
if((*i).x == x && (*i).y == y){
return true;
}
}
return false;
}
void printToScreen( float x, float y, std::string s)
{
int i;
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f( x, y); // location to start printing text
for( i=0; i < static_cast<int>(s.size()); i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, s[i]); // Print a character on the screen
}
}
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-1.0f, 1.0f); // x, y
glVertex2f(-1.0f, 1.0f - menuy);
glVertex2f(1.0f, 1.0f - menuy);
glVertex2f(1.0f, 1.0f);
glEnd();
float xspace = 2.0f / (windowX >= windowY ? xdim : ydim);
float yspace = (2.0f - menuy) / (windowX >= windowY ? xdim : ydim);
//TODO add gameover and game complete using status s
for(int i = 0; i < xdim; i++){
for(int j = 0; j < ydim; j++){
glBegin(GL_QUADS);
if(i == foodx && j == foody){
glColor3f(1.0f, 0.0f, 0.0f);
}
else if(isSnake(i,j)){
glColor3f(0.0f, 1.0f, 0.0f);
//std::cout << "here" << std::endl;
}
else{
glColor3f(0.0f, 0.0f, 1.0f);
}
/*
std::cout << xdim<< "," << ydim << ",";
std::cout << xspace << "," << yspace << ",";
std::cout << -1.0f + i * xspace;
std::cout << ",";
std::cout << 1.0f - j * yspace <<std::endl;
*/
glVertex2f(-1.0f + i * xspace, 1.0f - menuy - j * yspace); // x, y
glVertex2f(-1.0f + i * xspace, 1.0f - menuy - (j + 1) * yspace);
glVertex2f(-1.0f + (i+1) * xspace, 1.0f - menuy - (j + 1) * yspace);
glVertex2f(-1.0f + (i+1) * xspace, 1.0f - menuy - j * yspace);
glEnd();
glLineWidth(.1f);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
glVertex2f(-1.0f + i * xspace, 1.0f - menuy - j * yspace); // x, y
glVertex2f(-1.0f + i * xspace, 1.0f - menuy - (j + 1) * yspace);
glVertex2f(-1.0f + (i+1) * xspace, 1.0f - menuy - (j + 1) * yspace);
glVertex2f(-1.0f + (i+1) * xspace, 1.0f - menuy - j * yspace);
glVertex2f(-1.0f + i * xspace, 1.0f - menuy - j * yspace); // x, y
glEnd();
glEnd();
}
}
// Clear the color buffer (background)
// Draw a Red 1x1 Square centered at origin
// Each set of 4 vertices form a quad
// Red
time_t temp = time(NULL);
char buffer[256]; // make sure this is big enough!!!
snprintf(buffer, sizeof(buffer), "%g", difftime(temp, start));
std::ostringstream strs();
std::string str(buffer);
printToScreen(-1.0f ,1.0f - menuy, str);
if(s == dead){
//TODO figure out a better way to center text.
printToScreen(-0.5f + menuy ,0.0f - (menuy / 2) - menuy, std::string("Game over"));
printToScreen(-0.5f ,0.0f - (menuy / 2) - 2 * menuy, std::string("Space to Restart"));
}
if(s == complete){
printToScreen(-0.75f + menuy ,0.0f - (menuy / 2) - menuy, std::string("You Win!"));
printToScreen(-0.75f ,0.0f - (menuy / 2) - 2 * menuy, std::string("Space to Restart"));
}
glFlush(); // Render now
}
void idle(int val){
//std::cout <<"idle" <<std::endl;
// while(1){
//sleep(1);
Pair temp(snake.front().x, snake.front().y);
switch(d){
case up:
temp.y--;
break;
case right:
temp.x++;
break;
case down:
temp.y++;
break;
case left:
temp.x--;
break;
}
if(isSnake(temp.x, temp.y)){
s = dead;
glutPostRedisplay();
return;
}
if(temp.x < 0 || temp.x >= xdim || temp.y < 0 || temp.y >= ydim){
s = dead;
glutPostRedisplay();
return;
}
snake.push_front(temp);
if(foodx == temp.x && foody ==temp.y){
if(snake.size() == xdim * ydim){
s = complete;
glutPostRedisplay();
return;
}
while(isSnake(foodx, foody)){
foodx = std::rand() % xdim;
foody = std::rand() % ydim;
}
}
else{
snake.pop_back();
}
glutTimerFunc(val, idle, val);
glutPostRedisplay();
// }
}
void reset(){
std::srand(time(NULL));
d = up;
//prevdir = down;
s = alive;
menuy = .1f; //TODO actually calculate how tall menu should be
xdim = 15;
ydim = 15;
foodx = std::rand() % xdim;
foody = std::rand() % xdim;
windowX = 500;
windowY = 500;
windowY += static_cast<int>(windowY * menuy);
stepTime = 500;
start = time(NULL);
while(isSnake(foodx, foody)){
foodx = rand() % xdim;
foody = rand() % ydim;
}
snake.clear();
snake.push_back(Pair(xdim / 2, ydim / 2));
glutTimerFunc(stepTime, idle, stepTime);
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y){
float alpha = 1.0;
switch(key){
case 'w':
d = up;
break;
case 's':
d = down;
break;
case 'a':
d = left;
break;
case 'd':
d = right;
break;
case ' ':
if(s != alive){
reset();
}
break;
}
}
void SpecialInput(int key, int x, int y){
switch(key){
case GLUT_KEY_UP:
d = up;
break;
case GLUT_KEY_DOWN:
d = down;
break;
case GLUT_KEY_LEFT:
d = left;
break;
case GLUT_KEY_RIGHT:
d = right;
break;
}
}
void reshape(int width, int height){
windowX = 500;
windowY = 500;
windowY += static_cast<int>(windowY * menuy);
}
/* Main function: GLUT runs as a console application starting at main() */
main(int argc, char** argv){
int window1,window2;
std::srand(time(NULL));
d = up;
s = alive;
menuy = .1f; //TODO actually calculate how tall menu should be
xdim = 15;
ydim = 15;
foodx = std::rand() % xdim;
foody = std::rand() % xdim;
windowX = 500;
windowY = 500;
windowY += static_cast<int>(windowY * menuy);
stepTime = 500;
start = time(NULL);
while(isSnake(foodx, foody)){
foodx = rand() % xdim;
foody = rand() % ydim;
}
snake.push_back(Pair(xdim / 2, ydim / 2));
glutInit(&argc, argv);
// Initialize GLUT
//window1 = glutCreateWindow("Snake options");
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutInitWindowSize(windowX, windowY); // Set the window's initial width & height
window2 = glutCreateWindow("Snake"); // Create a window with the given title
//std::thread thread(idle);
glutIdleFunc(NULL);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(SpecialInput);
glutTimerFunc(stepTime, idle, stepTime);
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}