-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
196 lines (179 loc) · 5.24 KB
/
game.cpp
File metadata and controls
196 lines (179 loc) · 5.24 KB
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
#include<bits/stdc++.h>
#include<conio.h> //has key press event library(kbhit)(keyboard hit)
#include<windows.h>//to draw on colsole
using namespace std;
#define MAX_LENGTH 1000
const char DIR_UP='U';
const char DIR_DOWN='D';
const char DIR_RIGHT='R';
const char DIR_LEFT='L';
int consoleWidth,consoleHeight;
void initScreen(){// tto initialize the screen
// HANDLE is a predefined class in windows.h
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);//syntax
//GIVE THE HANDLE(CONTROL) OF THE WINDOW
CONSOLE_SCREEN_BUFFER_INFO csbi;//gives the information of the buffer
GetConsoleScreenBufferInfo(hConsole,&csbi);
consoleHeight=csbi.srWindow.Bottom-csbi.srWindow.Top+1;
consoleWidth=csbi.srWindow.Right-csbi.srWindow.Left+1;
}
struct Point{ //by default public members
int xCoord;
int yCoord;
Point(){
}
Point(int x,int y){
xCoord=x;
yCoord=y;
}
};
class Snake{ //by default private members
int length;
char direction;
public: // because we are making array and we need to specify the max length of the body, can also make private but we need to return a method.so to kkep it single we keeping it public
Point body[MAX_LENGTH];//stack memory
Snake(int x,int y){
length=1;
body[0]=Point(x,y);
direction=DIR_RIGHT;
}
int getLength(){
return length;
}
void changeDirection(char newDirection){
if(newDirection==DIR_UP && direction!=DIR_DOWN){
direction=newDirection;
}
else if(newDirection==DIR_UP && direction!=DIR_DOWN){
direction=newDirection;
}
else if(newDirection==DIR_DOWN && direction!=DIR_UP){
direction=newDirection;
}
else if(newDirection==DIR_LEFT && direction!=DIR_RIGHT){
direction=newDirection;
}
else if(newDirection==DIR_RIGHT && direction!=DIR_LEFT){
direction=newDirection;
}
}
bool move(Point food){
for(int i=length-1;i>0;i--){
body[i]=body[i-1];
}
switch(direction){
int val;
case DIR_UP:
val=body[0].yCoord;
body[0].yCoord=val-1;
break;
case DIR_DOWN:
val=body[0].yCoord;
body[0].yCoord=val+1;
break;
case DIR_LEFT:
val=body[0].xCoord;
body[0].xCoord=val-1;
break;
case DIR_RIGHT:
val=body[0].xCoord;
body[0].xCoord=val+1;
break;
}
for(int i=1;i<length;i++){
if(body[0].xCoord==body[i].xCoord && body[0].yCoord==body[i].yCoord){
return false;
}
}
if(food.xCoord==body[0].xCoord && food.yCoord==body[0].yCoord){
body[length]=Point(body[length-1].xCoord,body[length-1].yCoord);
length++;
}
return true;
}
};
class Board{
Snake *snake; //greated in heap(dynamic memory allocation)
const char SNAKE_BODY='O';
Point food;
const char FOOD='o';
int score;
public:
Board(){
spawnFood();
snake=new Snake(10,10);
score=0;
}
~Board(){
delete snake;
}
int getScore(){
return score;
}
void spawnFood(){
int x=rand()%consoleWidth;
int y=rand()%consoleHeight;
food=Point(x,y);
}
void displayCurrentScore(){
gotoxy(consoleWidth/2,0);
cout<<"Current Score:"<<score;
}
void gotoxy(int x,int y){//set the cursor position
COORD coord; //class
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void draw(){
system("cls");
for(int i=0;i<snake->getLength();i++){
gotoxy(snake->body[i].xCoord,snake->body[i].yCoord);//(->)DYNAMIC MEMORY ALLOCATION OF SNAKE
cout<<SNAKE_BODY;
}
gotoxy(food.xCoord,food.yCoord);//(.)STACK MEMMORY ALLLOCATION
cout<<FOOD;
displayCurrentScore();
}
bool update(){
bool isAlive=snake->move(food);
if(isAlive==false){
return false;
}
if(food.xCoord==snake->body[0].xCoord && food.yCoord==snake->body[0].yCoord){
score++;
spawnFood();
}
return true;
}
void getInput(){
if(kbhit()){
int key=getch();
if(key=='w'|| key=='W'){
snake->changeDirection(DIR_UP);
}
else if(key=='a'|| key=='A'){
snake->changeDirection(DIR_LEFT);
}
else if(key=='s'|| key=='S'){
snake->changeDirection(DIR_DOWN);
}
else if(key=='d'|| key=='D'){
snake->changeDirection(DIR_RIGHT);
}
}
}
};
int main(){
initScreen();
Board *board=new Board();
while (board->update())
{
board->getInput();
board->draw();
Sleep(100);
}
cout<<"Game Over"<<endl;
cout<<"Final Score is"<<board->getScore();
return 0;
}