-
Notifications
You must be signed in to change notification settings - Fork 1
/
finalgamew.cpp
183 lines (164 loc) · 4.54 KB
/
finalgamew.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
#include<iostream>
#include<ncurses.h>
#include<unistd.h>
#include<cstdlib>
using namespace std;
int main()
{
initscr(); //intializing the curses
WINDOW*win=newwin(30, 100, 3, 2); //creating window
raw();
noecho();
start_color(); //initialzing colors
init_pair(1,COLOR_RED, COLOR_WHITE);
wbkgd(win, COLOR_PAIR(1));
curs_set(FALSE); //erase cursor
int a[100],b[30]; //arrays to store snake coordinates
int fox=16; //arrays to store food coordinates
int foy=10;
int l=10; //initailzing snake's lenght
int jk=0,g,f; //flag values used
int k=3;
//--------------------------------------------------------------------------------------------------------------------------------------
//INTIALIZING SNAKE COORDINATES
for(int i=0;i<l;i++)
{
a[i]=i+2;
b[i]=10;
}
//------------------------------------------------------------------------------------------------------------------------------------
for(;;)
{
wborder(win, '#', '#', '#', '#', '+', '+', '+', '+');// Border function
wrefresh(win);
//------------------------------------------------------------------------------------------------------------------------------------
//MOVEMENT FUNCTION
int z; //input from the user
int f=a[0]; int g=b[0]; //grabbing snake tail coordinates so to print space after snake moved by 1 unit
for(int i=0;i<l-1;i++) //copying snake points coordinates to the point just behind them
{
a[i]=a[i+1];
b[i]=b[i+1];
}
keypad(stdscr,TRUE); //Allowing special characters to be enable(arrows keys)
nodelay(stdscr,TRUE); //taking input without stopping the game
z=getch();
switch(z)
{
case KEY_UP:if(k==2)
{
k=2;
}
else
{
k=5;
}
break;
case KEY_DOWN:if(k==5)
{
k=5;
}
else
{
k=2;
}
break;
case KEY_RIGHT:if(k==1)
{
k=1;
}
else
{
k=3;
}
break;
case KEY_LEFT:if(k==3)
{
k=3;
}
else
{
k=1;
}
break;
}
if(k==5) //storing new coordinates if snake's head
{
b[l-1]-=1;
}
if(k==2)
{
b[l-1]+=1;
}
if(k==3)
{
a[l-1]+=1;
}
if(k==1)
{
a[l-1]-=1;
}
//----------------------------------------------------------------------------------------------------------------------------------
//PRINTING SNAKE AFTER UPDATING
wmove(win,g,f);
wprintw(win," ");
for(int i=0;i<l;i++)
{
wmove(win,b[i],a[i]);
wprintw(win,"0");
}
wrefresh(win);
//-----------------------------------------------------------------------------------------------------------------------------------
//UPDATE FUNCTION
if(fox==a[l-1]&&foy==b[l-1])
{
l+=1; //increasing the lenght
a[l-1]=a[l-2]; //storing new points coordinates as that of snake
b[l-1]=b[l-2];
wmove(win,foy,fox); //erasing eaten food
wprintw(win," ");
wrefresh(win);
label2: //Getting new food's coordinates
srand ( time(NULL) );
int v=1+rand()%98;
int bv=1+rand()%28;
for(int i=0;i<l;i++)
{
if(v==a[i]&&bv==b[i])
{goto label2;}
}
fox=v;
foy=bv;
}
//----------------------------------------------------------------------------------------------------------------------------------
//PRINTING FOOD AFTER UPDATE
wmove(win,foy,fox);
wprintw(win,"*");
wrefresh(win);
usleep(100000);
//------------------------------------------------------------------------------------------------------------------------------------
//GAME OVER FUNCTION
int gh;
for(int i=0;i<l-2;i++) //Checking if snake has bitten himself
{
if(a[l-1]==a[i]&&b[l-1]==b[i])
{
system("CLEAR");
cout<<"----------------------------------------GAME OVER--------------------------------------------";
jk=1;
}
}
if(a[l-1]==99||b[l-1]==29||a[l-1]==1||b[l-1]==1)//Checking if snake has hitten boundary
{
system("CLEAR");
cout<<"---------------------------------------GAME OVER---------------------------------------------";
jk=1;
}
//----------------------------------------------------------------------------------------------------------------------------------------
if(jk==1) //exiting the loop if any of above two conditions are true
goto end;
}
end:
endwin();//ending the curses
return 0;
}