-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanke game.cpp
195 lines (163 loc) · 2.74 KB
/
sanke 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
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
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <vector>
#include <cmath>
using namespace std;
bool game_over;
const int latime = 40;
const int lungime = 20;
int ltail;
vector<int> tailX(100,0);
vector<int> tailY(100,0);
int x, y, fruitX, fruitY, score;
enum directie { stop = 0, stanga, dreapta, sus, jos };
directie dir;
void game() {
game_over = false;
dir = stop;
x = latime / 2;
y = lungime / 2;
fruitX = (rand() % latime-1);
if (fruitX == 0)
fruitX++;
fruitY = (rand() % lungime-1);
if (fruitY == 0)
fruitY++;
score = 0;
}
void print() {
system("cls");
//top
for (int i = 0; i < latime; i++) {
if (i == 0)
cout << '#';
else
if (i == latime - 1)
cout << '#';
else
cout << '#';
}
cout << endl;
//mid
for (int j = 0; j < lungime; j++)
for (int i = 0; i < latime; i++) {
if (i == 0)
cout << '#';
else
if (i == latime - 1)
cout << '#' << endl;
else
if (j == y and i == x)//head
cout << (char)254;
else
if (i == fruitX and j == fruitY)
cout << (char)14;//fruit
else
{
bool ok = false;
for (int k = 0; k < ltail; k++) {
if (tailX[k] == i and tailY[k] == j) {
cout << 'o';
ok = true;
}
}
if (!ok)
cout << ' ';
}
}
//botom
for (int i = 0; i < latime; i++) {
if (i == 0)
cout << '#';
else
if (i == latime - 1)
cout << '#' << endl;
else
cout << '#';
}
cout << endl;
cout << "Score: " << score;
}
void input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
dir = stanga;
break;
case 'd':
dir = dreapta;
break;
case 's':
dir = jos;
break;
case 'w':
dir = sus;
break;
case 'x':
game_over = true;
break;
}
}
}
void logic() {
int prevX = tailX[0];//0
int prevY = tailY[0];//0
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < ltail; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir)
{
case stanga:
x--;
break;
case dreapta:
x++;
break;
case sus:
y--;
break;
case jos:
y++;
break;
default:
break;
}
if (x >= latime-1 or x<=0 or y >= lungime or y<0)
game_over = true;
if (x == fruitX and y == fruitY) {
score++;
fruitX = (rand() % latime-1) ;
fruitY = (rand() % lungime-1);
if (fruitX == 0)
fruitX++;
if (fruitY == 0)
fruitY++;
ltail++;
}
for (int i = 0; i < ltail; i++)
if (tailX[i] == x and tailY[i] == y)
game_over = true;
}
int main(){
srand(time(NULL));
game();
while (!game_over) {
print();
input();
logic();
Sleep(abs(15-score));
}
return 0;
}