-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakegame.cpp
More file actions
194 lines (172 loc) · 3.38 KB
/
Snakegame.cpp
File metadata and controls
194 lines (172 loc) · 3.38 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
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <deque>
#include <ctime>
#include <string>
#include <stdexcept>
using namespace std;
class Snake
{
public:
char image;
short x,y;
};
class snakeGame
{
public:
deque <Snake> snake;
short height = 40;
short width = 120;
HANDLE hOut; COORD pos;
char direction;
bool eatFood=false;
bool beg;
double speed = 200;
short food_x;
short food_y;
int score=0;
snakeGame();
void gotoxy(short x, short y)
{
hOut = GetStdHandle (STD_OUTPUT_HANDLE);
pos = {x,y};
SetConsoleCursorPosition (hOut, pos);
}
void Map ()
{
for (int i=0; i<width; i=i+2) cout << "¡õ";
gotoxy (0,1);
for (int i=1; i<height;i++) cout << "¡õ"<<endl;
for (int i=1; i<height;i++)
{
gotoxy (width-2,i);
cout << "¡õ";
}
gotoxy (0,height-1);
for (int i=0; i<width;i=i+2) cout << "¡õ";
cout << endl;
}
void HideCursor ()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
void initSnake()
{
snake.push_back({'+',width/2,height/2});
for (int i=0; i<2; ++i)
snake.push_back({'+',width/2,(height/2+i+1)});
}
void createFood()
{
food_x = rand()%(width-4)+2;
food_y = rand()%(height-2)+1;
gotoxy(food_x,food_y);
cout <<'#';
}
void printSnake()
{
deque<Snake>::const_iterator iter=snake.begin();
for (; iter<snake.end();++iter)
{
gotoxy(iter->x,iter->y);
cout << iter->image;
}
}
void clearSnake(Snake &tail)
{
gotoxy(tail.x,tail.y);
cout << ' ';
}
void userInput()
{
switch (char ch=getch())
{
case 'w': if (direction != 's') direction = ch; break;
case 'a': if (direction != 'd') direction = ch; break;
case 's': if (direction != 'w') direction = ch; break;
case 'd': if (direction != 'a') direction = ch; break;
default: break;
}
}
void foodEaten()
{
createFood();
eatFood = true;
speed = speed * 0.6;
score++;
}
void judgeCrash()
{
if (snake.front().x==1 || snake.front().x==width-2 || snake.front().y==0 || snake.front().y==height-1)
{
gotoxy(1, height+1);
cout << "Game over. Your score is : "<<score<< " (Press any key to exist)"<<endl;
direction = getch();
runtime_error quit ("Bye Bye");
throw quit;
}
}
};
snakeGame::snakeGame()
{
HideCursor();
srand (time(0));
beg=true;
Snake tmp1, tmp2;
while (1)
{
if (beg)
{
Map ();
direction = getch();
initSnake();
createFood();
beg=eatFood=false;
}
tmp1= snake.front();
tmp2= snake.back();
snake.pop_back();
if (eatFood)
{
tmp2.image='+' ;
snake.push_back(tmp2);
eatFood = false;
}
else clearSnake(tmp2);
if (direction == 's') tmp1.y++;
else if (direction == 'w') tmp1.y--;
else if (direction == 'a') tmp1.x--;
else tmp1.x++;
try {judgeCrash();}
catch (runtime_error &quitSignal)
{
throw quitSignal;
}
snake.front().image = '+';
snake.push_front(tmp1);
printSnake();
Sleep(speed+20);
if (tmp1.x==food_x && tmp1.y==food_y)
foodEaten();
if (kbhit())
userInput();
}
}
int main ()
{
system("mode con cols=120 lines=45");
try
{
snakeGame game;
}
catch (runtime_error & gameEnd)
{
cout << gameEnd.what();
getch();
}
}