-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
189 lines (176 loc) · 3.71 KB
/
snake.cpp
File metadata and controls
189 lines (176 loc) · 3.71 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
#include<iostream>
#include<conio.h>
#include<Windows.h>
using namespace std;
enum Direction{STOP = 0, LEFT, RIGHT, UP, DOWN};
Direction dir;
bool gameOver;
const int height = 20;
const int width = 20;
int headX, headY, fruitX, fruitY, score;
int tailx[100], taily[100];
int tail_len;
void setup();
void draw();
void input();
void logic();
int main(){
char start;
cout << "\t-------------------------------" << endl;
cout << "\t\t :Snake King:" << endl;
cout << "\t-------------------------------" << endl;
cout << "\tPress 's' to option: ";
cin >> start;
if(start == 's'){
setup();
while(!gameOver){
draw();
input();
logic();
Sleep(30);
system("cls");
}
}
}
void setup(){
gameOver = false;
dir = STOP;
headX = width/2;
headY = height/2;
fruitX = rand()%width;
fruitY = rand()%height;
score = 0;
}
void draw(){
system("cls");
// Uper Border
cout << "\t\t";
for(int i = 0; i < width-8; i++){
cout << "||";
}
cout << endl;
// Snake, fruit, space and side borders
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
// left border
if(j == 0){
cout << "\t\t||";
}
// snake head
if(i == headY && j == headX){
cout << "O";
}
// fruit
else if(i == fruitY && j == fruitX){
cout << "*";
}
// space, snake tail
else{
bool print = false;
// tail
for(int k = 0; k < tail_len; k++){
if(tailx[k] == j && taily[k] == i){
cout << "o";
print = true;
}
}
// space
if(!print){
cout << " ";
}
}
// right border
if(j == width-1){
cout << "||";
}
}
cout << endl;
}
// Lower Border
cout << "\t\t";
for(int i = 0; i < width-8; i++){
cout << "||";
}
cout << endl;
cout << "\t\t\tScore: " << score << endl;
}
void input(){
if(_kbhit())
switch (getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
default:
break;
}
}
void logic(){
// tail logic
int prevx = tailx[0];
int prevy = taily[0];
int prev2x, prev2y;
tailx[0] = headX;
taily[0] = headY;
for(int i = 1; i < tail_len; i++){
prev2x = tailx[i];
prev2y = taily[i];
tailx[i] = prevx;
taily[i] = prevy;
prevx = prev2x;
prevy = prev2y;
}
// direction logic
switch (dir)
{
case LEFT:
headX--;
break;
case RIGHT:
headX++;
break;
case UP:
headY--;
break;
case DOWN:
headY++;
break;
default:
break;
}
// touch walls
if(headX >= width){
headX = 0;
}
else if(headX < 0){
headX = width - 1;
}
if(headY >= height){
headY = 0;
}
else if(headY < 0){
headY = height - 1;
}
// snake bite itself
for(int i = 0; i < tail_len; i++){
if(tailx[i] == headX && taily[i] == headY){
gameOver = true;
}
}
// snake eat fruit
if(headX == fruitX && headY == fruitY){
score += 10;
fruitX = rand()%width;
fruitY = rand()%height;
tail_len++;
}
}