Juegovivorita
Juegovivorita
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
void dibujarescenario(){
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute(hConsole, 1);
for (i=2 ; i<68 ; i++){
gotoxy(i,2); printf("%c",220);
gotoxy(i,28); printf("%c",220);
}
for (i=2 ; i<28 ; i++){
gotoxy(2,i); printf("%c",219);
gotoxy(68,i); printf("%c",219);
}
gotoxy(2,2); printf("%c",220);
gotoxy(68,2); printf("%c",220);
gotoxy(2,28); printf("%c",219);
gotoxy(68,28); printf("%c",219);
}
void guardarposicion(){
serpiente[s][0]=x;
serpiente[s][1]=y;
s++;
if(s==tamanio){
s = 1;
}
void dibujarserpiente(){
for (i=1 ; i<tamanio ; i++){
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute(hConsole, 10);
gotoxy(serpiente[i][0],serpiente[i][1]); printf("%c",157);
}
void borrarserpiente(){
for (i=1 ; i<tamanio ; i++){
gotoxy(serpiente[i][0], serpiente[i][1]); printf(" ");
}
}
void movimientoserpiente(){
if(kbhit()){
teclas=getch();
switch(teclas){
case ARRIBA:
if(dir!=2){
dir=1;
}
break;
case ABAJO:
if(dir!=1){
dir=2;
}
break;
case IZQUIERDA:
if(dir!=3){
dir=4;
}
break;
case DERECHA:
if(dir!=4){
dir=3;
}
break;
}
}
}
void velocidadserpiente(){
if(puntuacion==h*20){
velocidad = velocidad - 10;
h++;
}
}
void dibujarcomida(){
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute(hConsole, 2);
if(x== xc && y== yc){
srand(time(NULL));
xc = (rand()%62)+4;
yc = (rand()%22)+4;
tamanio = tamanio + 1;
puntuacion = puntuacion + 10;
gotoxy(xc,yc); printf("%c",254);
}
velocidadserpiente();
void juegoterminado(){
system("cls");
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute(hConsole, 4);
gotoxy(4,15); printf("%c",220);
gotoxy(5,15); printf("%c",220);
gotoxy(6,15); printf("%c",219);
gotoxy(7,15); printf("%c",219);
gotoxy(7,14); printf("%c",220);
gotoxy(7,13); printf("%c",220);
gotoxy(7,12); printf("%c",219);
gotoxy(7,11); printf("%c",219);
gotoxy(8,11); printf("%c",220);
gotoxy(9,11); printf("%c",220);
gotoxy(10,11); printf("%c",219);
gotoxy(11,11); printf("%c",219);
gotoxy(14,15); printf("%c",220);
gotoxy(14,14); printf("%c",220);
gotoxy(14,13); printf("%c",219);
gotoxy(14,12); printf("%c",219);
gotoxy(14,11); printf("%c",220);
gotoxy(15,11); printf("%c",220);
gotoxy(16,12); printf("%c",219);
gotoxy(17,13); printf("%c",219);
gotoxy(18,11); printf("%c",220);
gotoxy(18,12); printf("%c",220);
gotoxy(18,13); printf("%c",219);
gotoxy(18,15); printf("%c",219);
gotoxy(22,15); printf("%c",220);
gotoxy(22,14); printf("%c",220);
gotoxy(22,13); printf("%c",219);
gotoxy(22,12); printf("%c",219);
gotoxy(22,11); printf("%c",220);
gotoxy(23,11); printf("%c",220);
gotoxy(23,13); printf("%c",219);
gotoxy(24,11); printf("%c",219);
gotoxy(24,12); printf("%c",220);
gotoxy(24,13); printf("%c",220);
gotoxy(24,14); printf("%c",219);
gotoxy(24,15); printf("%c",219);
bool choque(){
if(x==2 || x==68 || y==2 || y==28){
juegoterminado();
return false;
}
for(int j=tamanio-1;j>0;j--){
if (serpiente[j][0]==x && serpiente[j][1]==y){
return false;
}
}
return true;
}
void mostrarpuntaje(){
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute(hConsole, 6);
gotoxy(80,20);printf("PUNTAJE ACTUAL: %d ", puntuacion);
}
void juego(){
system("cls");
gotoxy(xc,yc); printf("%c",254);
dibujarescenario();
while(teclas != ESC && choque()){
borrarserpiente();
guardarposicion();
dibujarserpiente();
movimientoserpiente();
movimientoserpiente();
dibujarcomida();
choque();
mostrarpuntaje();
if(dir==1){y--;}
if(dir==2){y++;}
if(dir==3){x++;}
if(dir==4){x--;}
Sleep(velocidad);
}
system("pause>null");
int main(){
int opcion = 1;
while (opcion !=0){
system("cls");
cout<<"****************************************************************************
***********"<<endl;
cout<<"** PROGRAMACION II - JUEGO DEL SNAKE
**"<<endl;
cout<<"** UPDS
**"<<endl;
cout<<"**__________________________________________________________________________
_________**"<<endl;
cout<<"** _---_ ......_----.
**"<<endl;
cout<<"** (o), / / /(0) |
**"<<endl;
cout<<"** / / .' -=-' `.
**"<<endl;
cout<<"** / / .' )
**"<<endl;
cout<<"** _/ / .' _.) / |
**"<<endl;
cout<<"** / o o _.-' / .' |
**"<<endl;
cout<<"** |' _ ,.,.__..-' / .' ' |
**"<<endl;
cout<<"** ''|/'''|/''. ' // ;' |;
**"<<endl;
cout<<"** ' ' / // .'.' _ |;
**"<<endl;
cout<<"** / // .'.' _ |;
**"<<endl;
cout<<"** /|_/|.; /'/.'.' | _ _'|;
**"<<endl;
cout<<"** '|.____./' '_ _ | '' ' /.
**"<<endl;
cout<<"** '_ _ ''''' '/.
**"<<endl;
cout<<"** |'_ _ / ....'/.
**"<<endl;
cout<<"** |___________|/.:
**"<<endl;
cout<<"**__________________________________________________________________________
_________**"<<endl;
cout<<"** INICIAR EL
JUEGO .....................................1 *'"<<endl;
cout<<"**
SALIR .....................................0
**"<<endl;
cout<<"****************************************************************************
***********"<<endl;
cout<<"SELECCIONE UNA OPCION:
"<<endl;
cin>>opcion;
switch(opcion)
{
case 0:
cout<< "SALIR... ";
getch();
break;
case 1:
juego();
getch();
break;
default:
break;
}
}
return 0;
}