• Inicio
  • Buscar
  • Ingresar
  • Registrarse

    • Hack x Crack - Comunidad de Seguridad informática »
    • Programación »
    • C / C++ »
    • Juego de Snake C++
    • Imprimir
    Páginas: [1]   Ir Abajo

    Autor Tema: Juego de Snake C++  (Leído 40753 veces)

    Desconectado doommachine

    • { L3 } Über
    • ***
    • Mensajes: 218
      • Ver Perfil
    Juego de Snake C++
    « en: Julio 07, 2016, 04:01:14 pm »
    Hola a todos, aquí les comparto un juego que hice en C++ del famoso juego de Snake.

    Aquí algunas capturas:







    Quiero aclarar que el juego solo funciona en windows ya que usa librerías como "windows.h"  para simular la función "gotoxy()" y algunos comandos de msdos como "color" y "pause".

    Por esa razón dejo el reto para quien tenga tiempo y quiera atreverse a modificar el código y hacer funcionar el juego en linux y luego publicarlo en el foro.

    Los controles del juegos son las flechas del teclado.

    El código fue escrito en dev C++ 5.11

    Código: C++
    1. #include <iostream>
    2. #include <windows.h>
    3. #include <stdlib.h>
    4. #include <conio.h>
    5. #include <time.h>
    6.  
    7. using namespace std;
    8.  
    9. void gotoxy(int x, int y){
    10.         HANDLE hCon;
    11.         hCon=GetStdHandle(STD_OUTPUT_HANDLE);
    12.        
    13.         COORD dwPos;
    14.         dwPos.X=x;
    15.         dwPos.Y=y;
    16.         SetConsoleCursorPosition(hCon,dwPos);
    17. }
    18.  
    19. class Snake{
    20.         public:
    21.                 int x,y;
    22.                 int dx,dy;
    23.                 char cuerpo;
    24. };
    25.  
    26. class Fruta{
    27.         public:
    28.                 int x,y;
    29.                 char cuerpo=254;
    30. };
    31.  
    32. class Juego{
    33.         public:
    34.                 void main();
    35.         private:
    36.                 int score,nivel,velocidad;
    37.                 Snake snake[100];
    38.                 Fruta fruta;
    39.                 int tam=4;
    40.                 int c=21,f=64;
    41.                 bool gameover=false;
    42.                 void tablero();
    43.                 //void inicio();
    44.                 void genFruta();
    45.                 void genSnake();
    46.                 void loop();
    47.                 void tecla();
    48.                 void actualizar();
    49.                 void imprimir();
    50.                 void cfruta();
    51.                 void muerte();
    52.                 void puntos();
    53.                 //void menu();
    54.                 void portada();
    55.                 void muerte2();
    56.        
    57. };
    58.  
    59. void Juego::muerte2(){
    60.         system("cls");
    61.         int c=24,f=79,r;
    62.         char key;
    63.         char t=178;
    64.         for(int i=0 ; i<f ; i++){
    65.                 gotoxy(i,0);
    66.                 cout<<t;
    67.                 gotoxy(i,c);
    68.                 cout<<t;
    69.         }
    70.         for(int i=0 ; i<=c ; i++){
    71.                 gotoxy(0,i);
    72.                 cout<<t;
    73.                 gotoxy(f,i);
    74.                 cout<<t;
    75.         }
    76.        
    77.         string g4meover[]={"  ____                       ___"," / ___| __ _ _ __ ___   ___ / _ \\__   _____ _ __ ",
    78.         "| |  _ / _` | '_ ` _ \\ / _ \\ | | \\ \\ / / _ \\ '__|","| |_| | (_| | | | | | |  __/ |_| |\\ V /  __/ | ",
    79.         " \\____|\\__,_|_| |_| |_|\\___|\\___/  \\_/ \\___|_|"};
    80.         r=2;
    81.         for(int i=0;i<5;i++){
    82.                 gotoxy(15,r);
    83.                 r++;
    84.                 cout<<g4meover[i]<<endl;
    85.         }
    86.        
    87.         string g4meover2[]={"   _________         _________","  /         \\       /         \\",
    88.         " /  /~~~~~\\  \\     /  /~~~~~\\  \\"," |  |     |  |     |  |     |  |"," |  |     |  |     |  |     |  |",
    89.         " |  |     |  |     |  |     |  |         /"," |  |     |  |     |  |     |  |       //","(X  X)    \\  \\_____/  /     \\  \\_____/ /",
    90.         " \\__/      \\         /       \\        /","  |         ~~~~~~~~~         ~~~~~~~~","  ^"};
    91.         for(int i=0;i<11;i++){
    92.                 gotoxy(20,r);
    93.                 cout<<g4meover2[i]<<endl;
    94.                 r++;
    95.                
    96.         }
    97.         gotoxy(33,r);
    98.         cout<<"Score: "<<score;
    99.         r++;
    100.         gotoxy(33,r);
    101.         cout<<"Level: "<<nivel;
    102.         gotoxy(8,23);
    103.         cout<<"Presione ESC para salir o cualquier otra tecla para seguir jugando";
    104.         key=getch();
    105.         if(key==27)
    106.                 exit(1);
    107. }
    108.  
    109.  
    110. void Juego::portada(){
    111.         system("cls");
    112.         int c=24,f=79,r;
    113.         char t=178;
    114.         for(int i=0 ; i<f ; i++){
    115.                 gotoxy(i,0);
    116.                 cout<<t;
    117.                 gotoxy(i,c);
    118.                 cout<<t;
    119.         }
    120.         for(int i=0 ; i<=c ; i++){
    121.                 gotoxy(0,i);
    122.                 cout<<t;
    123.                 gotoxy(f,i);
    124.                 cout<<t;
    125.         }
    126.         string snkd[]={"       ---_ ......._-_--.","      (|\\ /      / /| \\  \\","      /  /     .'  -=-'   `.",
    127.                 "     /  /    .'             )","   _/  /   .'        _.)   /","  / o   o        _.-' /  .'",
    128.                 "  \\          _.-'    / .'*|","   \\______.-'//    .'.' \\*|","    \\|  \\ | //   .'.' _ |*|",
    129.                 "     `   \\|//  .'.'_ _ _|*|","      .  .// .'.' | _ _ \\*|","      \\`-|\\_/ /    \\ _ _ \\*\\",
    130.                 "                     \\ _ _ \\*","                      \\ _ _ \\ ","                       \\_"};
    131.                
    132.         r=2;
    133.         for(int i=0;i<14;i++){
    134.                 gotoxy(20,r);
    135.                 r++;
    136.                 cout<<snkd[i]<<endl;
    137.         }
    138.        
    139.         string snkl[]={"                     __     ","   _________  ____ _/ /_____","  / ___/ __ \\/ __ `/ //_/ _ \\",
    140.         " (__  ) / / / /_/ / ,< /  __/","/____/_/ /_/\\__,_/_/|_|\\___/"};
    141.         for(int i=0;i<5;i++){
    142.                 gotoxy(22,r);
    143.                 r++;
    144.                 cout<<snkl[i]<<endl;
    145.         }
    146.         gotoxy(23,23);
    147.         cout<<"Precione ENTER para empezar";
    148.         system("pause>dsdsd");
    149. }
    150.  
    151.  
    152. void Juego::puntos(){
    153.         gotoxy(f+2,2);
    154.         cout<<"Score: "<<(score-1)*10;
    155.         gotoxy(f+2,4);
    156.         cout<<"Level: "<<nivel;
    157.         gotoxy(f+2,6);
    158.         cout<<"Length: "<<tam;
    159.        
    160.        
    161. }
    162. void Juego::tablero(){
    163.         char t=178;
    164.         for(int i=1 ; i<f ; i++){
    165.                 gotoxy(i,1);
    166.                 cout<<t;
    167.                 gotoxy(i,c);
    168.                 cout<<t;
    169.         }
    170.         for(int i=1 ; i<=c ; i++){
    171.                 gotoxy(1,i);
    172.                 cout<<t;
    173.                 gotoxy(f,i);
    174.                 cout<<t;
    175.         }
    176. }
    177.  
    178. void Juego::genFruta(){
    179.        
    180.     fruta.x = 2+( rand() % (f-2) );
    181.     fruta.y = 2+(rand() % (c-2));
    182.     gotoxy(fruta.x,fruta.y);
    183.     cout<<fruta.cuerpo;
    184.        
    185. }
    186.  
    187. void Juego::muerte(){
    188.         if(snake[0].x==1 || snake[0].x==f || snake[0].y==1 || snake[0].y==c)
    189.         gameover=true;
    190.        
    191.     for(int i=1;i<tam && gameover==false;i++){
    192.         if(snake[0].x==snake[i].x && snake[0].y==snake[i].y){
    193.             gameover=true;
    194.         }
    195.    
    196.         }
    197. }
    198.  
    199. void Juego::tecla(){
    200.         int i;
    201.     char key;
    202.    
    203.         if (!gameover){
    204.                 if (kbhit()==1){
    205.                         key=getch();
    206.        
    207.                 }
    208.                 if((key==72) && snake[0].dy !=1){
    209.                         snake[0].dx=0;
    210.                         snake[0].dy=-1;
    211.                 }
    212.                 if((key==80) && snake[0].dy !=-1){
    213.                         snake[0].dx=0;
    214.                         snake[0].dy=1;
    215.                 }
    216.                 if((key==75) && snake[0].dx !=1){
    217.                         snake[0].dx=-1;
    218.                         snake[0].dy=0;
    219.                 }
    220.                 if((key==77) && snake[0].dx !=-1){
    221.                         snake[0].dx=1;
    222.                         snake[0].dy=0;
    223.                 }
    224.         }
    225. }
    226.  
    227. void Juego::genSnake(){
    228.         int i;
    229.         snake[0].x=30;
    230.         snake[0].y=10;
    231.         snake[0].dx=1;
    232.     snake[0].dy=0;
    233.         snake[0].cuerpo=157;
    234.        
    235.        
    236.        
    237.     for(i=1;i < tam; i++){
    238.         snake[i].x=snake[i-1].x-1;
    239.         snake[i].y=snake[i-1].y;
    240.         snake[i].cuerpo=184;
    241.     }
    242.    
    243.  
    244.     for(i=0; i<tam; i++){
    245.         gotoxy(snake[i].x,snake[i].y);
    246.         cout<<snake[i].cuerpo;
    247.     }
    248. }
    249.  
    250. void Juego::cfruta(){
    251.         if(snake[0].x==fruta.x && snake[0].y==fruta.y){
    252.                 genFruta();
    253.         tam+=1;
    254.         snake[tam-1].cuerpo=184;
    255.         score+=1;
    256.         if(tam%10==0){
    257.                         nivel++;
    258.                         velocidad-=20;
    259.                 }
    260.  
    261.     }
    262. }
    263. void Juego::actualizar(){
    264.         int i;
    265.         gotoxy(snake[tam-1].x,snake[tam-1].y);
    266.         cout<<" ";
    267.        
    268.     for (i= tam-1; i>0; i--){
    269.         snake[i].x=snake[i-1].x;
    270.         snake[i].y=snake[i-1].y;
    271.     }
    272.     snake[0].x += snake[0].dx;
    273.     snake[0].y += snake[0].dy;
    274.        
    275. }
    276.  
    277. void Juego::imprimir(){
    278.         int i;
    279.         for(i= tam-1; i>=0; i--){
    280.         gotoxy(snake[i].x,snake[i].y);
    281.         cout<<snake[i].cuerpo;
    282.     }
    283. }
    284. void Juego::loop(){
    285.         while(!gameover){
    286.                 cfruta();
    287.                 puntos();
    288.                 actualizar();
    289.                 imprimir();
    290.                 tecla();
    291.                 tecla();
    292.                 tecla();
    293.                 muerte();
    294.                 Sleep(velocidad);
    295.         }
    296. }
    297.  
    298. void Juego::main(){
    299.         system("color 0a");
    300.         gameover=false;
    301.         portada();
    302.         system("cls");
    303.         score=1;
    304.         velocidad=110;
    305.         tam=4;
    306.         nivel=1;
    307.         tablero();
    308.         srand(time(NULL));
    309.         genSnake();
    310.         genFruta();
    311.         loop();
    312.         muerte2();
    313.         main();
    314.  
    315.        
    316.    
    317.        
    318. }
    319.  
    320. int main(){
    321.         Juego j;
    322.         j.main();
    323. }
    324.  

    Saludos.
    En línea

    Desconectado zamoraxam

    • { L3 } Über
    • ***
    • Mensajes: 257
      • Ver Perfil
    Re:Juego de Snake C++
    « Respuesta #1 en: Julio 07, 2016, 07:32:23 pm »
    buenas doommachine :), por las capturas se ve muy bonito el juego que haz creado, mas tarde leere algo sobre c++ e intentare ejecutarlo para poder jugar unas partidas y comprender como esta echo :)
    En línea

    Desconectado josegallo23%

    • { L0 } Ñuub
    • Mensajes: 2
      • Ver Perfil
    Re:Juego de Snake C++
    « Respuesta #2 en: Enero 08, 2017, 10:24:07 pm »
    Oye bro , como puesdo ejecutar tu juego en windows?
    En línea

    Desconectado tatoluckyfox

    • universo infinito... curioso cuanto menos...
    • { L2 } Nativo Digital
    • **
    • Mensajes: 119
    • ¿Por qué subir, pudiendo bajar?
      • Ver Perfil
    Re:Juego de Snake C++
    « Respuesta #3 en: Enero 08, 2017, 11:10:02 pm »
    Compilándolo.
    https://sourceforge.net/projects/orwelldevcpp/
    « Última modificación: Enero 08, 2017, 11:17:20 pm por tatoluckyfox »
    En línea

    Desconectado josegallo23%

    • { L0 } Ñuub
    • Mensajes: 2
      • Ver Perfil
    Re:Juego de Snake C++
    « Respuesta #4 en: Febrero 03, 2017, 11:33:33 pm »
    Gracias bro :D
    En línea

    Desconectado Mano2

    • { L0 } Ñuub
    • Mensajes: 14
      • Ver Perfil
    Re:Juego de Snake C++
    « Respuesta #5 en: Febrero 10, 2017, 10:54:00 am »
    lo mismo digo, bro! buen curro!
    En línea
    Linux lover hehe

    Desconectado lex852

    • { L0 } Ñuub
    • Mensajes: 1
      • Ver Perfil
    Re:Juego de Snake C++
    « Respuesta #6 en: Mayo 17, 2017, 05:26:16 am »
    Hola  :), soy nuevo en esto y estoy también haciendo este juego pero en C, pero no se como hacerle para que se mueva solo. (Estoy programando en Linux).
    En línea

    Desconectado samuel69127

    • { L0 } Ñuub
    • Mensajes: 1
      • Ver Perfil
    Re:Juego de Snake C++
    « Respuesta #7 en: Marzo 18, 2019, 11:25:47 pm »
    tengo un proyecto en la universidad quisiera saber si existe la posiblidad de que en vez de comer circulos comiera palabras
    En línea

    • Imprimir
    Páginas: [1]   Ir Arriba
    • Hack x Crack - Comunidad de Seguridad informática »
    • Programación »
    • C / C++ »
    • Juego de Snake C++
     

    • SMF | SMF © 2013, Simple Machines
    • XHTML
    • RSS
    • WAP2
    Va un mudo y le dice a un sordo: Hack x Crack usa cookies. Pues eso... Learn more