0% found this document useful (0 votes)
22 views6 pages

Juegovivorita

This C++ program implements a snake game. It includes function definitions for drawing the game scenario, storing and drawing the snake's position, handling key presses to control movement, checking for collisions, updating the score, and the main game loop. Global variables track the snake's position, direction, speed, size, and score. The main function displays a menu and calls the game loop, which redraws and moves the snake each frame until a collision occurs.

Uploaded by

kevinchoqe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Juegovivorita

This C++ program implements a snake game. It includes function definitions for drawing the game scenario, storing and drawing the snake's position, handling key presses to control movement, checking for collisions, updating the score, and the main game loop. Global variables track the snake's position, direction, speed, size, and score. The main function displays a menu and calls the game loop, which redraws and moves the snake each frame until a collision occurs.

Uploaded by

kevinchoqe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<iostream>

#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>

using namespace std;


//VARIABLES GLOBALES
int x = 12;
int y = 14;
int i = 0;
int serpiente[200][2];
int s = 1;
int dir = 3;
int velocidad = 100;
int tamanio = 5;
char teclas;
int puntuacion = 0;
int h = 1;
int xc = 20;
int yc = 8;
int r;
//CONSTANTES
#define IZQUIERDA 75
#define DERECHA 77
#define ARRIBA 72
#define ABAJO 80
#define ESC 27

void gotoxy(int x, int y){


printf("%c[%d;%dH", 27, y, x);
}

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);

gotoxy(28,14); printf(" ...GAME OVER...");


gotoxy(29,16); printf(" ....F I N ....");
}

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;
}

You might also like