-
Notifications
You must be signed in to change notification settings - Fork 0
/
newSnake4.cpp
249 lines (238 loc) · 4.22 KB
/
newSnake4.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
//new start using new algo to move snake !st March 2013
//19:40 Things done here : Perfect step by step movement of snake.
// Also not if snake is moving left, it can't move right and if right i/p is pressed it will continue moving left
//same for up and down
// Now moves continuously, game over if hits boundary
//Now added random colors
//Now added egg also,length increases, seems final.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#define lmax 8
void assignPosition(int x[],int y[],int length,char ch,int radius)
{
/*int xend=x[length-1],yend=y[length-1];
for(int i=length-1;i>=1;i--)
{
x[i]=x[i-1];
y[i]=y[i-1];
}
if(ch=='a')
{
x[0]=x[1]-2*radius;
y[0]=y[1];
}
else if(ch=='d')
{
//x[0]=x[length-1]+2*radius;
x[0]=xend;
y[0]=yend;
}
else if(ch=='w')
{
x[0]=x[1]
}*/
if(ch=='a')
{
/*x[length-1]=x[0]-2*radius;
y[length-1]=y[0];
printf("champu");*/
for(int i=length-1;i>=1;i--)
{
x[i]=x[i-1];
y[i]=y[i-1];
}
x[0]=x[1]-2*radius;
y[0]=y[1];
}
else if(ch=='d')
{
/*x[0]=x[length-1]+2*radius;
y[0]=y[length-1];*/
/*for(int i=0;i<=length-2;i++)
{
x[i]=x[i+1];
y[i]=y[i+1];
}
x[length-1]=x[length-1]+2*radius;
y[length-1]=y[length-1];*/
for(int i=length-1;i>=1;i--)
{
x[i]=x[i-1];
y[i]=y[i-1];
}
x[0]=x[0]+2*radius;
}
else if(ch=='w')
{
/*x[length-1]=x[0];
y[length-1]=y[0]-2*radius;*/
for(int i=length-1;i>=1;i--)
{
x[i]=x[i-1];
y[i]=y[i-1];
}
x[0]=x[1];
y[0]=y[0]-2*radius;
}
else if(ch=='x')
{
/*x[length-1]=x[0];
y[length-1]=y[0]+2*radius;*/
for(int i=length-1;i>=1;i--)
{
x[i]=x[i-1];
y[i]=y[i-1];
}
x[0]=x[1];
y[0]=y[0]+2*radius;
}
}
void draw(int x[],int y[],int length,int radius)
{
for(int i=0;i<length;i++)
{
//circle(x[i],y[i],radius);
ellipse(x[i],y[i],0,360,radius,radius);
setfillstyle(i,rand());
fillellipse(x[i],y[i],radius,radius);
}
}
void egg(int x,int y)
{
circle(x,y,5);
}
int checkVicinity(int xe, int ye, int xs,int ys,int radius) //return true if near egg
{
int flag=0;
if(abs(xe-xs)<=radius && abs(ye-ys)<=radius)
{
flag=1;
}
return flag;
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");
char ch='a';
int length=5; // Set initial length of snake as 3
//int lmax=10; //max length
int radius=10;
int xPosition[lmax],yPosition[lmax];
for(int i=0;i<length;i++)
{
xPosition[i]=100+i*2*radius;
yPosition[i]=100;
}
draw(xPosition,yPosition,length,radius);
char temp=ch,temp2;
int xegg=rand()%640,yegg=rand()%480;
int eggFlag=0;
while(ch!='p')
{
int temp2=checkVicinity(xegg,yegg,xPosition[0],yPosition[0],radius);
if(temp2==1)
{
length=length+1;
if(ch=='a')
{
for(int i=length;i>=1;i--)
{
xPosition[i]=xPosition[i-1];
}
xPosition[0]=xPosition[1]-2*radius;
yPosition[0]=yPosition[1];
}
else if(ch=='d')
{
xPosition[length]=xPosition[length-1]-2*radius;
yPosition[length]=yPosition[length-1];
}
}
if(length>=lmax)
{
printf("You Win!\n");
break;
}
//eggFlag=checkVicinity(xegg,yegg,xPosition[0],yPosition[0],radius);
eggFlag=temp2;
if(eggFlag==0)
{
egg(xegg,yegg);
}
else
{
xegg=rand()%640;
yegg=rand()%480;
}
for(int i=0;i<10000;i++) //to slow down
{
for(int j=0;j<1600;j++)
{
}
}
if(kbhit())
{
temp2=getch();
if(temp=='a' && temp2=='d')
{
ch=temp;
}
else if(temp=='d' && temp2=='a')
{
ch=temp;
}
else if(temp=='w' && temp2=='x')
{
ch=temp;
}
else if(temp=='x' && temp2=='w')
{
ch=temp;
}
else
{
ch=temp2;
}
temp=ch;
}
/*temp2=getch();
if(temp=='a' && temp2=='d')
{
ch=temp;
}
else if(temp=='d' && temp2=='a')
{
ch=temp;
}
else if(temp=='w' && temp2=='x')
{
ch=temp;
}
else if(temp=='x' && temp2=='w')
{
ch=temp;
}
else
{
ch=temp2;
}
temp=ch;*/
cleardevice();
assignPosition(xPosition,yPosition,length,ch,radius);
if(xPosition[0]<0 || xPosition[0]>639 || yPosition[0]<0 || yPosition[0]>479)
{
printf("Game Over\n");
break;
}
draw(xPosition,yPosition,length,radius);
/*for(i=0;i<length;i++)
{
printf("%d %d for %d \n ",xPosition[i],yPosition[i],i+1);
}*/
}
getch();
closegraph();
}