• Viernes 29 de Marzo de 2024, 16:11

Autor Tema:  Problema con la colision  (Leído 2374 veces)

DIASQ

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Problema con la colision
« en: Martes 9 de Diciembre de 2014, 20:12 »
0
Buenas tardes queria saber si era posible que me ayudaran. Tengo un codigo de laberinto pero no logro que choque contra las paredes. Al contrario se las come. Tengo este problema. Dejo el codigo a continuacion:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include <cstdlib>
#include <iostream>
#include <dos.h>
#include <windows.h>
#define Arriba 72
#define Izquierda 75
#define Derecha 77
#define Abajo 80
using namespace std;

 int lab[50][50];

int i,j;
void Inicializar()
{
     for(int i=1; i<16; i++)
     {
             for(int j=1; j<31; j++)
             {
                     if(i==1||j==1 || i==15||j==30) lab[j]=0;
                     else lab[j]=0;
                     }
             }
     }
void camino(int x, int y)
{
    while(y!=31&&x!=16)
     {
             int a=rand()%66;
             switch(a)
             {
                      case 0:
                           if(x+1!=15 && lab[x+1][y]!=1)
                           {
                                      x++;
                                      lab
  • [y]=1;

                                      }
                           break;
                      case 1:
                           if(x-1!=0 && lab[x-1][y]!=1)
                           {
                                     x--;
                                     lab
  • [y]=1;

                                      }
                           break;
                      case 2:
                           if(x+1!=30 && lab
  • [y+1]!=1)

                           {
                                      y++;
                                      lab
  • [y]=1;

                                      if(x==31) lab
  • [y]=2;

                                      }
                           break;


                      }
                 }//fin while
}
void imprimir()
{
    for(i=1;i<16;i++)
   {
      for(j=1;j<31;j++)
      {
         if(lab[j]==0)cout<<(char)177;

         else if (lab[j]==1)cout<<" ";

            else cout<<lab[j];
                     }
                     cout<<endl;
      }
}
void gotoxy(int x, int y){
    HANDLE hCon;
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD dwPos;
    dwPos.X = x;
    dwPos.Y = y;
    SetConsoleCursorPosition(hCon, dwPos);
}
void OcultarCursor(){
    HANDLE hCon;
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    cci.dwSize = 50;
    cci.bVisible = false;
    SetConsoleCursorInfo(hCon,&cci);
}
class monito
      {int z,y;
      public:
             monito(int _z, int _y): z(_z), y(_y){}
             void pintar();
             void borrar();
             void mover();
      };
void monito::pintar()
{
    gotoxy(z,y);
    printf("%c",2);
}
void monito::borrar()
{
    gotoxy(z,y);
    printf(" ");
}
//para moverte, si tu!
void monito::mover()
     {
                  char tecla;
         if(kbhit()){ //funcion que sirve para detectar que tecla se presiona
            tecla = getch(); //leemos la tecla
            gotoxy(z,y);//manda llamar la funcion
            borrar();
            if(tecla == Izquierda){
                    z--;
                if( lab[z][y] !=1){
                    gotoxy(z,y);
                    pintar();
                }
                else{
                    z++;
                }
            }
            if(tecla == Derecha){
                    z++;
                if( lab[z][y] !=1){
                    gotoxy(z,y);
                    pintar();
                }
                else{
                    z--;
                }

            }
            if(tecla == Arriba){
                    y--;
            }
            if(tecla == Abajo){
                    y++;
            }
            gotoxy(z,y);
            pintar();
     }
     }
main()
{
    OcultarCursor();
    bool gameover = false; //sentencia del while para saber cuando termine
    char tecla; //variable para saber la tecla que se presiona
   srand(unsigned(time(NULL)));
    Inicializar();
    int x=rand()%3;
    camino(x,1);
    lab[1][1]=1;
    lab[2][1]=1;
    imprimir();
    monito M(0,0);
    while(!gameover)
{
    M.pintar();

    M.mover();
}
}