-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
210 lines (143 loc) · 2.77 KB
/
snake.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include<windows.h>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
#define ARRIBA 72
#define IZQUIERDA 75
#define DERECHA 77
#define ABAJO 80
#define ESC 27
int cuerpo[200][2];
int n = 1;
int tam = 3;
int x = 10, y = 12;
int dir = 3;
int xc = 30, yc = 15;
int velocidad = 100, h = 1;
int score = 0;
char tecla;
void gotoxy(int x, int y){
HANDLE hCon;
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hCon, dwPos);
}
void pintar() {
//Lineas Horizontales
for(int i=2; i<78; i++){
gotoxy(i, 3); printf("%c", 205);
gotoxy(i, 23); printf("%c", 205);
}
//Lineas Vertical
for(int i=4; i<23; i++){
gotoxy(2, i); printf("%c", 186);
gotoxy(77, i); printf("%c", 186);
}
//Esquinas
gotoxy(2, 3); printf("%c", 201);
gotoxy(2, 23); printf("%c", 200);
gotoxy(77, 3); printf("%c", 187);
gotoxy(77, 23); printf("%c", 188);
}
void guardar_posicion(){
cuerpo[n][0] = x;
cuerpo[n][1] = y;
n++;
if(n == tam) n = 1;
}
void dibujar_cuerpo(){
for(int i=1; i<tam; i++){
gotoxy(cuerpo[i][0], cuerpo[i][1]); printf("*");
}
}
void borrar_cuerpo(){
gotoxy(cuerpo[n][0], cuerpo[n][1]); printf(" ");
}
void teclear(){
if(kbhit()){
tecla = getch();
switch(tecla){
case ARRIBA:
if(dir != 2)
dir = 1;
break;
case ABAJO:
if(dir != 1)
dir = 2;
break;
case DERECHA:
if(dir != 4)
dir = 3;
break;
case IZQUIERDA:
if(dir != 3)
dir = 4;
break;
}
}
}
void cambiar_velocidad(){
if(score == h*20){
velocidad -= 10;
h++;
}
}
void comida(){
if(x == xc && y == yc){
//Coordenadas para que la comida se pinte dentro del margen.
xc = (rand()%73)+4;
yc = (rand()%19)+4;
tam++;
score += 10;
gotoxy(xc, yc); printf("%c", 4);
cambiar_velocidad();
}
}
bool game_over(){
//Choque con los muros
if(y == 3 || y == 23 || x == 2 || x == 77) return true;
//Choque con el cuerpo
for(int j=tam-1; j>0; j--){
if(cuerpo[j][0] == x && cuerpo[j][1] == y)
return true;
}
return false;
}
void puntos(){
gotoxy(3,1); printf("score %d", score);
}
int main(){
int op;
cout<<"Seleccione una opcion\n";
cout<<"[1]INICIAR JUEGO\n";
cout<<"[2]SALIR\n";
cin>>op;
system("cls");
switch(op){
case 1:
pintar();
gotoxy(xc, yc); printf("%c", 4);
while(tecla != ESC && !game_over()){
borrar_cuerpo();
guardar_posicion();
dibujar_cuerpo();
comida();
puntos();
teclear();
teclear();
if(dir == 1) y--;
if(dir == 2) y++;
if(dir == 3) x++;
if(dir == 4) x--;
Sleep(velocidad);
}
system("pause>null");
return 0;
break;
case 2:
break;
}
}