forked from morgothaufheroin88/Snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
283 lines (255 loc) · 5.42 KB
/
main.c
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
/*****************
* Snake *
* by CX9PS3 *
* License:GPLv3 *
*****************
*/
//load headers
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <GL/glut.h>
//set size for game field and size of window
#define m 20
#define n 30
#define scale 25
int w=scale * n;
int h=scale * m;
int SnakeSpeed;
int dir,length=1,Score=0;
//set coordinates for snake elements
struct
{
int x;
int y;
}s[100];
int AppleX,AppleY;
//create apple in random position in field
void NewApple()
{
srand(time(NULL));
AppleX=rand()%n;
AppleY=rand()%m;
}
//set coordinates for apple and color
void DrawApple()
{
glColor3f(1.0f,0.0f,0.0f);
glRectf(AppleX*scale,AppleY*scale,(AppleX+1)*scale,(AppleY+1)*scale);
}
//TODO Make beutyfull background
/*struct Flowers
{
int x;
int y;
float r;
float g;
float b;
} Flower[50];
float RGBConventer(float a)
{
return a/255;
}
void CreateFlower(int i)
{
srand(time(NULL));
Flower[i].x=rand()%n;
Flower[i].y=rand()%m;
Flower[i].r=RGBConventer(rand()%255);
Flower[i].g=RGBConventer(rand()%255);
Flower[i].b=RGBConventer(rand()%255);
}
void DrawFlowers()
{
for(int i=0;i<50;i++)
{
CreateFlower(i);
}
for(int i=0;i<50;i++)
{
glColor3f(Flower[i].r,Flower[i].g,Flower[i].b);
glRectf(Flower[i].x*scale/2,Flower[i].y*scale/2,(Flower[i].x+1)*scale/2,((Flower[i].y+1)*scale)/2);
}
}
*/
//set coordinates for text and color,this text show player score
void DrawScore(int x,int y,int score)
{
//integer to char
char str;
str=score+'0';
glColor3f(1.0, 0.0,0.0);
glRasterPos2d(x,y);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str);
}
void Snake()
{
//set coordinates for elements of snake
for(int i=length;i>0;i--)
{
s[i].x=s[i-1].x;
s[i].y=s[i-1].y;
}
//set direction for snake
switch (dir)
{
case 0 :
s[0].y+=1;
break;
case 1 :
s[0].x-=1;
break;
case 2 :
s[0].x+=1;
break;
case 3 :
s[0].y-=1;
break;
default:
break;
}
///collision with apple
if((s[0].x==AppleX)&&(s[0].y==AppleY))
{
length++;
Score++;
NewApple();
}
//collision with border of field
if(s[0].x>n) dir = 1;
if(s[0].y>m) dir = 3;
if(s[0].x<0) dir = 2;
if(s[0].y<0) dir = 0;
//collision snake head with another parts of snake
for (int i = 1; i < length; i++)
{
if((s[0].x==s[i].x)&&(s[0].y==s[i].y)&& length>4)
{
length=1;
Score=0;
}
}
}
//set controls
void Keyboard(int key,int x,int y)
{
switch (key)
{
case 101: dir = 0; break;//up
case 103: dir = 3; break;//left
case 100: dir = 1; break;//right
case 102: dir = 2; break;//down
case GLUT_KEY_F1: glutFullScreen();break;//enable fullscreen
case GLUT_KEY_F2:glutPositionWindow(0,0);glutReshapeWindow(w, h);break;//disable fullscreen
case GLUT_KEY_F12:exit(EXIT_SUCCESS);break;//exit from game
}
}
//set snake color and position
void DrawSnake()
{
glColor3f(1.0f,0.415f,0.415f);
for(int i=0;i<length;i++)
{
glRectf(s[i].x*scale,s[i].y*scale,(s[i].x+1)*scale,(s[i].y+1)*scale);
}
}
//draw lines on field
void DrawScene()
{
glColor3f(0.48f,0.8f,0.5f);
glBegin(GL_LINES);
for(int i=0;i<w;i+=scale)
{
glVertex2f(i,0);
glVertex2f(i,h);
}
for(int i=0;i<h;i+=scale)
{
glVertex2f(0,i);
glVertex2f(w,i);
}
glEnd();
}
//render all
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.443f,0.766f,0.443f,0.0f);
//DrawFlowers();
DrawApple();
DrawScene();
DrawSnake();
DrawScore(w-20,h-20,Score);
glFlush();
}
void timer()
{
display();
Snake();
glutTimerFunc(SnakeSpeed,timer,0);
}
void ShowHelp()
{
printf("--easy - easy difficult\n");
printf("--normal - middle difficult\n");
printf("--hard - hard difficult\n");
printf("--help - show this\n");
printf("Controls:\n");
printf("F1 - fullscreen on\n");
printf("F2 - fullscreen off\n");
printf("F12 - exit from game\n");
printf("Arrows - control snake\n");
}
int main(int argc,char *argv[])
{
//program flags
if(argc>1)
{
if(!strcmp(argv[1],"--easy"))
{
printf("You choose easy mode \n");
SnakeSpeed=75;
}
else if(!strcmp(argv[1],"--normal"))
{
printf("You choose normal mode \n");
SnakeSpeed=50;
}
else if(!strcmp(argv[1],"--hard"))
{
printf("You choose hard mode \n");
SnakeSpeed=35;
}
else if(!strcmp(argv[1],"--help"))
{
ShowHelp();
return 1;
}
else
{
printf("Error,Unknown key of command\n");
ShowHelp();
return 1;
}
}
else
{
printf("Error!Use flags!!!\n");
return 1;
}
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(w,h);
glutCreateWindow("Snake");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
s[0].x=10;
s[0].y=10;
NewApple();
glutDisplayFunc(display);
glutTimerFunc(SnakeSpeed,timer,0);
glutSpecialFunc(Keyboard);
glutMainLoop();
}