elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a Git (Primera Parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Petición en Juego de programación (Snake)
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Petición en Juego de programación (Snake)  (Leído 3,848 veces)
AJDFernadez

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Petición en Juego de programación (Snake)
« en: 22 Agosto 2022, 22:27 pm »

Buenas, hice un juego de la serpiente en C++ y desearía por simple estética como agregarle una "Animación de muerte" cada vez que la serpiente se acerca al punto de colisión que en este caso sería las paredes del juego. Aquí les tengo el código para checarlo:

Código:
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
 
using namespace std;
 
void gotoxy(int x, int y){
        HANDLE hCon;
        hCon=GetStdHandle(STD_OUTPUT_HANDLE);
       
        COORD dwPos;
        dwPos.X=x;
        dwPos.Y=y;
        SetConsoleCursorPosition(hCon,dwPos);
}
 
class Snake{
        public:
                int x,y;
                int dx,dy;
                char cuerpo;
};
 
class Fruta{
        public:
                int x,y;
                char cuerpo=254;
};
 
class Juego{
        public:
                void main();
        private:
                int score,nivel,velocidad;
                Snake snake[100];
                Fruta fruta;
                int tam=4;
                int c=21,f=64;
                bool gameover=false;
                void tablero();
                //void inicio();
                void genFruta();
                void genSnake();
                void loop();
                void tecla();
                void actualizar();
                void imprimir();
                void cfruta();
                void muerte();
                void puntos();
                //void menu();
                void portada();
                void muerte2();
       
};
 
void Juego::muerte2(){
        system("cls");
        int c=24,f=79,r;
        char key;
        char t=178;
        for(int i=0 ; i<f ; i++){
                gotoxy(i,0);
                cout<<t;
                gotoxy(i,c);
                cout<<t;
        }
        for(int i=0 ; i<=c ; i++){
                gotoxy(0,i);
                cout<<t;
                gotoxy(f,i);
                cout<<t;
        }
       
        string g4meover[]={"  ____                       ___"," / ___| __ _ _ __ ___   ___ / _ \\__   _____ _ __ ",
        "| |  _ / _` | '_ ` _ \\ / _ \\ | | \\ \\ / / _ \\ '__|","| |_| | (_| | | | | | |  __/ |_| |\\ V /  __/ | ",
        " \\____|\\__,_|_| |_| |_|\\___|\\___/  \\_/ \\___|_|"};
        r=2;
        for(int i=0;i<5;i++){
                gotoxy(15,r);
                r++;
                cout<<g4meover[i]<<endl;
        }
       
        string g4meover2[]={"   _________         _________","  /         \\       /         \\",
        " /  /~~~~~\\  \\     /  /~~~~~\\  \\"," |  |     |  |     |  |     |  |"," |  |     |  |     |  |     |  |",
        " |  |     |  |     |  |     |  |         /"," |  |     |  |     |  |     |  |       //","(X  X)    \\  \\_____/  /     \\  \\_____/ /",
        " \\__/      \\         /       \\        /","  |         ~~~~~~~~~         ~~~~~~~~","  ^"};
        for(int i=0;i<11;i++){
                gotoxy(20,r);
                cout<<g4meover2[i]<<endl;
                r++;
               
        }
        gotoxy(33,r);
        cout<<"Score: "<<score;
        r++;
        gotoxy(33,r);
        cout<<"Level: "<<nivel;
        gotoxy(8,23);
        cout<<"Presione ESC para salir o cualquier otra tecla para seguir jugando";
        key=getch();
        if(key==27)
                exit(1);
}
 
 
void Juego::portada(){
        system("cls");
        int c=24,f=79,r;
        char t=178;
        for(int i=0 ; i<f ; i++){
                gotoxy(i,0);
                cout<<t;
                gotoxy(i,c);
                cout<<t;
        }
        for(int i=0 ; i<=c ; i++){
                gotoxy(0,i);
                cout<<t;
                gotoxy(f,i);
                cout<<t;
        }
        string snkd[]={"       ---_ ......._-_--.","      (|\\ /      / /| \\  \\","      /  /     .'  -=-'   `.",
                "     /  /    .'             )","   _/  /   .'        _.)   /","  / o   o        _.-' /  .'",
                "  \\          _.-'    / .'*|","   \\______.-'//    .'.' \\*|","    \\|  \\ | //   .'.' _ |*|",
                "     `   \\|//  .'.'_ _ _|*|","      .  .// .'.' | _ _ \\*|","      \\`-|\\_/ /    \\ _ _ \\*\\",
                "                     \\ _ _ \\*","                      \\ _ _ \\ ","                       \\_"};
               
        r=2;
        for(int i=0;i<14;i++){
                gotoxy(20,r);
                r++;
                cout<<snkd[i]<<endl;
        }
       
        string snkl[]={"                     __     ","   _________  ____ _/ /_____","  / ___/ __ \\/ __ `/ //_/ _ \\",
        " (__  ) / / / /_/ / ,< /  __/","/____/_/ /_/\\__,_/_/|_|\\___/"};
        for(int i=0;i<5;i++){
                gotoxy(22,r);
                r++;
                cout<<snkl[i]<<endl;
        }
        gotoxy(23,23);
        cout<<"Precione ENTER para empezar";
        system("pause>dsdsd");
}
 
 
void Juego::puntos(){
        gotoxy(f+2,2);
        cout<<"Score: "<<(score-1)*10;
        gotoxy(f+2,4);
        cout<<"Level: "<<nivel;
        gotoxy(f+2,6);
        cout<<"Length: "<<tam;
       
       
}
void Juego::tablero(){
        char t=178;
        for(int i=1 ; i<f ; i++){
                gotoxy(i,1);
                cout<<t;
                gotoxy(i,c);
                cout<<t;
        }
        for(int i=1 ; i<=c ; i++){
                gotoxy(1,i);
                cout<<t;
                gotoxy(f,i);
                cout<<t;
        }
}
 
void Juego::genFruta(){
       
    fruta.x = 2+( rand() % (f-2) );
    fruta.y = 2+(rand() % (c-2));
    gotoxy(fruta.x,fruta.y);
    cout<<fruta.cuerpo;
       
}
 
void Juego::muerte(){
        if(snake[0].x==1 || snake[0].x==f || snake[0].y==1 || snake[0].y==c)
        gameover=true;
       
    for(int i=1;i<tam && gameover==false;i++){
        if(snake[0].x==snake[i].x && snake[0].y==snake[i].y){
            gameover=true;
        }
   
        }
}
 
void Juego::tecla(){
        int i;
    char key;
   
        if (!gameover){
                if (kbhit()==1){
                        key=getch();
       
                }
                if((key==72) && snake[0].dy !=1){
                        snake[0].dx=0;
                        snake[0].dy=-1;
                }
                if((key==80) && snake[0].dy !=-1){
                        snake[0].dx=0;
                        snake[0].dy=1;
                }
                if((key==75) && snake[0].dx !=1){
                        snake[0].dx=-1;
                        snake[0].dy=0;
                }
                if((key==77) && snake[0].dx !=-1){
                        snake[0].dx=1;
                        snake[0].dy=0;
                }
        }
}
 
void Juego::genSnake(){
        int i;
        snake[0].x=30;
        snake[0].y=10;
        snake[0].dx=1;
    snake[0].dy=0;
        snake[0].cuerpo=157;
       
       
       
    for(i=1;i < tam; i++){
        snake[i].x=snake[i-1].x-1;
        snake[i].y=snake[i-1].y;
        snake[i].cuerpo=184;
    }
   
 
    for(i=0; i<tam; i++){
        gotoxy(snake[i].x,snake[i].y);
        cout<<snake[i].cuerpo;
    }
}
 
void Juego::cfruta(){
        if(snake[0].x==fruta.x && snake[0].y==fruta.y){
                genFruta();
        tam+=1;
        snake[tam-1].cuerpo=184;
        score+=1;
        if(tam%10==0){
                        nivel++;
                        velocidad-=20;
                }
 
    }
}
void Juego::actualizar(){
        int i;
        gotoxy(snake[tam-1].x,snake[tam-1].y);
        cout<<" ";
       
    for (i= tam-1; i>0; i--){
        snake[i].x=snake[i-1].x;
        snake[i].y=snake[i-1].y;
    }
    snake[0].x += snake[0].dx;
    snake[0].y += snake[0].dy;
       
}
 
void Juego::imprimir(){
        int i;
        for(i= tam-1; i>=0; i--){
        gotoxy(snake[i].x,snake[i].y);
        cout<<snake[i].cuerpo;
    }
}
void Juego::loop(){
        while(!gameover){
                cfruta();
                puntos();
                actualizar();
                imprimir();
                tecla();
                tecla();
                tecla();
                muerte();
                Sleep(velocidad);
        }
}
 
void Juego::main(){
        system("color 0a");
        gameover=false;
        portada();
        system("cls");
        score=1;
        velocidad=50;
        tam=4;
        nivel=1;
        tablero();
        srand(time(NULL));
        genSnake();
        genFruta();
        loop();
        muerte2();
        main();
 
       
   
       
}
 
int main(){
        Juego j;
        j.main();
}
 


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
juego SNAKE en C# « 1 2 »
.NET (C#, VB.NET, ASP)
lokot3 14 40,582 Último mensaje 2 Junio 2006, 19:39 pm
por SeniorX
Juego snake en c++
Programación C/C++
Blaster 6 85,954 Último mensaje 7 Octubre 2012, 11:43 am
por fary
[C] Juego snake
Programación C/C++
Dacan 4 4,648 Último mensaje 22 Marzo 2013, 05:32 am
por Dacan
Mi juego Snake C++ / SDL 2.0
Programación C/C++
erest0r 7 7,146 Último mensaje 10 Diciembre 2014, 13:43 pm
por erest0r
Necesito ayuda con un juego de SNAKE EN ALLEGRO C++
Programación C/C++
ALDINPEREZ 2 2,632 Último mensaje 24 Noviembre 2019, 05:09 am
por K-YreX
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines