-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNAKE.C
207 lines (200 loc) · 3.4 KB
/
SNAKE.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
/*
Snake Game in C language using graphics library.
*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
int udir = 0; // Up direction
int ddir = 0; // Down direction
int ldir = 0; // Left direction
int rdir = 1; // Right direction
int x[500];
int y[500];
int s_len = 3, block_size = 10;
int food_x, food_y;
int maxx, maxy;
void dev_info()
{
char *msg = "Developed by Ali Amir";
char solid_pattern[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
int max_height = getmaxy();
setfillpattern(solid_pattern, BLUE);
bar(0, maxy + 1, maxx, max_height);
settextstyle(0, 0, 3);
outtextxy((maxx - textwidth(msg)) / 2, maxy + ((30 - textheight(msg)) / 2), msg);
}
void init_pos()
{
int i;
for (i = 0; i < 500; i++)
{
x[i] = -1;
y[i] = -1;
}
}
int collision()
{
int i;
for (i = 1; i < s_len; i++)
{
if (x[0] == x[i] && y[0] == y[i])
{
return 1;
}
}
if (x[0] < 0 || x[0] > maxx || y[0] < 0 || y[0] > maxy)
{
return 1;
}
return 0;
}
void move()
{
int i;
for (i = s_len; i > 0; i--)
{
x[i] = x[(i - 1)];
y[i] = y[(i - 1)];
}
if (udir)
{
y[0] -= block_size;
}
else if (ddir)
{
y[0] += block_size;
}
else if (ldir)
{
x[0] -= block_size;
}
else if (rdir)
{
x[0] += block_size;
}
}
void locate_food()
{
int r;
srand(time(0));
r = (rand() % maxx) / block_size;
food_x = r * block_size;
r = (rand() % maxy) / block_size;
food_y = r * block_size;
}
void check_food()
{
if (x[0] == food_x && y[0] == food_y)
{
s_len++;
locate_food();
}
}
void draw_board()
{
int i;
char solid_pattern[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
setbkcolor(WHITE);
cleardevice();
setfillpattern(solid_pattern, BLUE);
bar(food_x, food_y, food_x + 10, food_y + 10);
setfillpattern(solid_pattern, RED);
bar(x[0], y[0], x[0] + block_size, y[0] + block_size);
setfillpattern(solid_pattern, GREEN);
for (i = 1; i < s_len; i++)
{
bar(x[i], y[i], x[i] + 10, y[i] + 10);
}
dev_info();
}
void change_dir(char key)
{
if ((key == 'a' || key == 75) && !rdir)
{
ldir = 1;
udir = 0;
ddir = 0;
}
else if ((key == 'd' || key == 77) && !ldir)
{
rdir = 1;
udir = 0;
ddir = 0;
}
else if ((key == 's' || key == 80) && !udir)
{
ddir = 1;
ldir = 0;
rdir = 0;
}
else if ((key == 'w' || key == 72) && !ddir)
{
udir = 1;
ldir = 0;
rdir = 0;
}
}
void init_game()
{
int i;
for (i = 0; i < s_len; i++)
{
x[i] = 50 - (i * block_size);
y[i] = 50;
}
locate_food();
}
void game_over()
{
char *msg = "GAME OVER";
setbkcolor(BLACK);
cleardevice();
settextstyle(0, 0, 3);
outtextxy((maxx - textwidth(msg)) / 2, (maxy - textheight(msg)) / 2, msg);
getch();
}
int main()
{
int gd = DETECT, gm;
char key;
clrscr();
initgraph(&gd, &gm, "C://TURBOC3//BGI");
maxx = getmaxx();
maxy = getmaxy() - 30;
setbkcolor(BLACK);
cleardevice();
init_pos();
init_game();
while (!collision())
{
delay(1500);
if (kbhit())
{
key = getch();
change_dir(key);
}
move();
if (kbhit())
{
key = getch();
change_dir(key);
}
check_food();
if (kbhit())
{
key = getch();
change_dir(key);
}
draw_board();
if (kbhit())
{
key = getch();
change_dir(key);
}
}
game_over();
closegraph();
return 0;
}