-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake Game.cpp
149 lines (121 loc) · 2.98 KB
/
Snake Game.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
#include<graphics.h>
#include<time.h>
#include<iostream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
int endfunc(int another);
int main(){
//direction
int gd,gm,rx=200,ry=200,x[200]={0},y[200]={0},d;
//Food eaten
int food=1, chk= getmaxx();
//directions
int dir=1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
setfillstyle(SOLID_FILL,YELLOW);//(style,color)
x[0]=200,y[0]=200;
d=1;
int length = 1;
// The continuous animation
for(;;){
//Score board
setcolor(LIGHTCYAN);
settextstyle(BOLD_FONT, 0,2);
// Background
setfillstyle(SOLID_FILL,DARKGRAY);
bar(0,0,getmaxx(),getmaxy());
char arr[50];
sprintf(arr, "Score is :%d", food-2);
outtextxy(20, 40, arr);
//IF the food is taken
if(x[0]==rx && y[0]==ry ){
length += 1;
food = food+1;
//make food
do{
rx = (1+rand()%(getmaxx()-10));
ry = (1+rand()%(getmaxy()-10));
}while(rx<=10 && ry<=10);
rx=rx/10;
rx=rx*10;
ry=ry/10;
ry=ry*10;
setfillstyle(HATCH_FILL,YELLOW);
}
//FOOD STYLE
setfillstyle(SOLID_FILL,RED);
bar(rx,ry,rx+10,ry+10);
// FOUR BOUNDARIES
setfillstyle(SOLID_FILL, LIGHTBLUE);
bar(0,0,getmaxx(),10);
bar(0,0,10,getmaxy());
bar(0,getmaxy(),getmaxx(),getmaxy()-10);
bar(getmaxx()-10,10,getmaxx(),getmaxy());
if(GetAsyncKeyState(VK_RIGHT)){d=1;}
else if(GetAsyncKeyState(VK_LEFT)){ d=2;}
else if(GetAsyncKeyState(VK_UP)){ d=3;}
else if(GetAsyncKeyState(VK_DOWN)) {d=4;}
else{d=0;}
switch(d){
case 0:
if(dir==1){x[0]=x[0]+10;}
else if(dir==2){x[0]=x[0]-10;}
else if(dir==3){ y[0]=y[0]-10;}
else if(dir==4) {y[0]=y[0]+10;}
else{d=0;}
break;
case 1:
x[0]=x[0]+10;
dir=1;
break;
case 2:
x[0]= x[0]-10;
dir=2;
break;
case 3:
dir=3;
y[0]=y[0]-10;
break;
case 4:
dir=4;
y[0]= y[0]+10;
break;
}
for(int i = 0; i < length;i++){
setfillstyle(HATCH_FILL,YELLOW);
bar(x[i],y[i],x[i]+10,y[i]+10);
}
for(int i= 199; i >0;i--){
x[i] = x[i-1];
y[i] = y[i -1];
}
delay(100);
if(x[0] >= getmaxx() || x[0]<=0|| y[0]<=0 || y[0]>=getmaxy()){
endfunc(food);
break;
}
for(int i = 2; i < length;i++){
if(x[0] == x[i] && y[0] == y[i]){
chk = i;
break;
}
}
if(x[0] == x[chk] && y[0] == y[chk]){
endfunc(food);
break;
}
}
return 0;
}
int endfunc(int another){
setfillstyle(SOLID_FILL,LIGHTRED);
another=another-2;
bar(0,0,getmaxx(),getmaxy()-10);
//char s = (char)another;
system("cls");
cout << "Score is : " << another<<endl;
getch();
return 0;
}