-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake sin menu allegro.txt
149 lines (146 loc) · 3.09 KB
/
snake sin menu allegro.txt
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
#include <windows.h>
#include <iostream>
#include <conio.h>
#include<stdlib.h>
#include <fstream>
#define ARRIBA 72 // números asociados a las flechas del teclado
#define ABAJO 80
#define IZQUIERDA 75
#define DERECHA 77
#define ESC 27
using namespace std;
void gotoxy(int x,int y) //funcion que posiciona el cursor en la coordenada xy
{
HANDLE hCon;
COORD dwPos;
dwPos.X=x;
dwPos.Y=y;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hCon,dwPos);
}
int cuerpo[200][2];
char tecla;
int n=1;
int tam = 10 ;
int x=10;
int y=12;
int dir=3;
int xc=30 , yc=15;
int velocidad = 100, h = 1;
int score = 0;
void pintar()
{
// lineas horizontales
for(int i=2; i<78; i++){
gotoxy(i,3);printf("%c",205);
gotoxy(i,23);printf("%c",205);
}
// lineas verticales
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){
xc = (rand()%73)+4;
yc = (rand()%19)+4;
tam++;
score += 10;
gotoxy(xc,yc);printf("%c",4);
cambiar_velocidad();
}
}
bool game_over(){
if (y == 3 || y == 23 || x == 2 || x == 77) {
ofstream archivo;
archivo.open("puntajes.txt", ios::app);
archivo << "Puntuación: " << score << endl;
archivo.close();
return false;
}
for(int j = tam-1; j > 0; j-- ){
if(cuerpo[j][0] == x && cuerpo[j][1] == y) {
ofstream archivo;
archivo.open("puntajes.txt", ios::app);
archivo << "Puntuación: " << score << endl;
archivo.close();
return false;
}
}
return true;
}
void puntos(){
gotoxy(3,1); printf("score %d",score);
}
int main()
{
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);
}
pintar();
system("pause>null");
return 0;
}