Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

Para Leo....quiero hacer una especie de ROGUELIKES ¿?

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: Para Leo....quiero hacer una especie de ROGUELIKES ¿? Responder citando

hola leo, soy tu amigo de algeciras....israel....hace años que no entro por aqui....

te cuento, queria hacer una especie de jueguecillo roguelikes....algo similar a esto:



la cuestion es que estoy un poco oxidado en cuando a programacion y no se como enfocarlo

crees que podrias hacerme algo de codigo cuando tengas tiempo claro está, sin prisa, para ayudarme con el enfoque?

gracias anticipadas


me refiero como "visualizar" en un TForm el mapeado de caracteres en colores.....quizas un string grid valdria???
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

bueno....me he puesto a ello y algo he conseguido

os pongo aqui el codigo fuente por si os sirve de guia por si alguno de vosotros planeaba hacer un roguelikes

personalmente he seguido un poco el Articulo #2 (Basic Mapping) de esta pagina que creo que os gustará:


http://www.kathekonta.com/rlguide/index.html



los componentes utilizados son un TStringGrid, 2 TEdit para controlar las variables x,y y un TBitButton y un TCheckbox para activar o desactivar la cuadricula rejilla


CODIGO Unit1.cpp

Código:
//---------------------------------------------------------------------------

#include <vcl>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

// Map dimensions
#define MAP_WIDTH 20
#define MAP_HEIGHT 15

// Tile Types
#define TILE_FLOOR 0
#define TILE_WALL 1


int nMapArray[MAP_HEIGHT][MAP_WIDTH] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};


// Function Prototypes
void DrawMap(void);



//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{

}
//---------------------------------------------------------------------------

void DrawMap(void)
{
  for( int y = 0; y < MAP_HEIGHT; y++ )
  {
   for( int x = 0; x <MAP_WIDTH>StringGrid1->Canvas->Brush->Color = clBlack;
                         Form1->StringGrid1->Cells[x][y] = ".";
                         Form1->StringGrid1->Refresh();
                         break;
          case TILE_WALL:
                         Form1->StringGrid1->Canvas->Brush->Color = clGreen;
                         Form1->StringGrid1->Cells[x][y] = "#";
                         Form1->StringGrid1->Refresh();
                         break;
        }  // end of Switch

    Form1->Edit2->Text = x;  Form1->Edit2->Refresh();
    Form1->Edit1->Text = y;  Form1->Edit1->Refresh();


     } // end of for loop
  } // end of for loop

}
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
 DrawMap();
 StringGrid1->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox_RejillaClick(TObject *Sender)
{
 if(CheckBox_Rejilla->Checked == true) StringGrid1->GridLineWidth = 1;
 else StringGrid1->GridLineWidth = 0;
}
//---------------------------------------------------------------------------




CODIGO de Unit1.h

Código:
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes>
#include <Controls>
#include <StdCtrls>
#include <Forms>
#include <Grids>
#include <Buttons>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:   // IDE-managed Components
   TStringGrid *StringGrid1;
   TBitBtn *BitBtn1;
   TEdit *Edit1;
   TEdit *Edit2;
   TCheckBox *CheckBox_Rejilla;
   void __fastcall BitBtn1Click(TObject *Sender);
   void __fastcall CheckBox_RejillaClick(TObject *Sender);
private:   // User declarations
public:      // User declarations
   __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



y el resultado es este



Con la REJILLA (GRID) activada:



Con la REJILLA (GRID) Invisible:
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

posteriormente he añadido en el evento ONDRAWCELL del TStringGrid el siguiente codigo para colorear un poco las "paredes" del mapa

Código:

void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
        TGridDrawState State)
{
  // standard colors
 ////////////////////////////////
 // clBlack   = TColor($000000);
 // clMaroon  = TColor($000080);
 // clGreen   = TColor($008000);
 // clOlive   = TColor($008080);
 // clNavy    = TColor($800000);
 // clPurple  = TColor($800080);
 // clTeal    = TColor($808000);
 // clGray    = TColor($808080);
 // clSilver  = TColor($C0C0C0);
 // clRed     = TColor($0000FF);
 // clLime    = TColor($00FF00);
 // clYellow  = TColor($00FFFF);
 // clBlue    = TColor($FF0000);
 // clFuchsia = TColor($FF00FF);
 // clAqua    = TColor($FFFF00);
 // clLtGray  = TColor($C0C0C0); // clSilver alias
 // clDkGray  = TColor($808080); // clGray alias
 // clWhite   = TColor($FFFFFF);

   TRect Casilla;//variable necesaria para pintar la celda

  if ((ACol>=0) && (ARow>=0))
  {
      if (StringGrid1->Cells[ACol][ARow] == "#")
            StringGrid1->Canvas->Brush->Color = clDkGray;
   
      else if (StringGrid1->Cells[ACol][ARow] == ".")
            StringGrid1->Canvas->Brush->Color = clBlack;



   StringGrid1->Canvas->FillRect(Rect);
   StringGrid1->Canvas->TextOut(Rect.Left,Rect.Top,StringGrid1->Cells[ACol][ARow]);
  }
}
//---------------------------------------------------------------------------




quedando el mapa resultante de esta manera:

Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

lo que de momento no he conseguido.....es cambiar el color de solo algunos caracteres del mapa......los que yo quiera

porque si cambio el StringGrid->Font->Color me cambia el color de todos los caracteres

a alguien se le ocurre o sabe como se hace?
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: Responder citando

vale....ya lo he solucionado....por suerte era sencillo


el codigo del OnDrawCell quedaría de la siguiente manera

Código:
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
        TGridDrawState State)
{
  // standard colors
 ////////////////////////////////
 // clBlack   = TColor($000000);
 // clMaroon  = TColor($000080);
 // clGreen   = TColor($008000);
 // clOlive   = TColor($008080);
 // clNavy    = TColor($800000);
 // clPurple  = TColor($800080);
 // clTeal    = TColor($808000);
 // clGray    = TColor($808080);
 // clSilver  = TColor($C0C0C0);
 // clRed     = TColor($0000FF);
 // clLime    = TColor($00FF00);
 // clYellow  = TColor($00FFFF);
 // clBlue    = TColor($FF0000);
 // clFuchsia = TColor($FF00FF);
 // clAqua    = TColor($FFFF00);
 // clLtGray  = TColor($C0C0C0); // clSilver alias
 // clDkGray  = TColor($808080); // clGray alias
 // clWhite   = TColor($FFFFFF);

   TRect r(Rect);
   StringGrid1->Canvas->FillRect(r);


  if ((ACol>=0) && (ARow>=0))
  {
   if (StringGrid1->Cells[ACol][ARow] == "#")
    StringGrid1->Canvas->Brush->Color = clDkGray;
   else if (StringGrid1->Cells[ACol][ARow] == ".")
   {
     StringGrid1->Canvas->Font->Color = clLime;
    StringGrid1->Canvas->Brush->Color = clBlack;

   }
   else if(StringGrid1->Cells[ACol][ARow] == "$")
    {

      StringGrid1->Canvas->Font->Color = clYellow;
    }


   StringGrid1->Canvas->FillRect(Rect);
   StringGrid1->Canvas->TextOut(Rect.Left,Rect.Top,StringGrid1->Cells[ACol][ARow]);
  }
}
//---------------------------------------------------------------------------



y el resultado seria este:

Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mensaje    Asunto: continuando con el capitulo III Responder citando

Bueno, he retocado el codigo en base al capitulo III del tutorial de roguelikes que os puse en el otro post

como resultado una serie de optimizaciones para el dibujado de tiles del mapa asi como la inclusion de un mini-handler de teclado para mover el icono del personaje mediante las teclas de cursor detectando tambien si el tile es pasable (se puede atravesar) o si es solido y no se puede pasar por el sitio en cuestion.

tambien he ampliado el mapa agrandandolo un poco

el codigo ha quedado de la siguiente manera

Código:
//---------------------------------------------------------------------------

#include <vcl>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

// Map dimensions
#define MAP_WIDTH 48
#define MAP_HEIGHT 30

int nPlayerX = 0;
int nPlayerY = 0;
AnsiString backup_fondo;

// Tile Types
struct TILE_TYPE
{
char nCharacter; // ASCII character for this tile type
long int nColorCode; // Color code for this tile type
bool bPassable; // Set to true if you can walk on this tile
};
// Global array used to define all tile types used in the game
TILE_TYPE nTileIndex[] = {
{ '.', clSilver, true }, // (0) TILE_ROCKFLOOR
{ '#', clWhite, false}, // (1) TILE_WALL
{ '$', clMaroon, false }, // (2) TILE_GOLD
{ '&', clMaroon, true }, // (3) TILE_TREE
{ '.', clLime, true }, // (4) TILE_GRASS
{ '~', clGreen, false }, // (5) TILE_WATER
{ '+', clYellow, true }, // (5) TILE_CLOSEDDOOR
{ '/', clAqua, true } // (6) TILE_OPENDOOR
};


int nMapArray[MAP_HEIGHT][MAP_WIDTH] = {
{ 4, 3, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 4, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 4, 3, 1, 1, 1, 1, 1, 4, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 3, 3, 1, 0, 0, 0, 1, 4, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 1, 0, 0, 0, 1, 4, 4, 4, 1, 0, 2,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 5, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};


// Function Prototypes
void DrawMap(void);
bool IsPassable( int x, int y );
void DrawTile( int x, int y );



//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{

}
//---------------------------------------------------------------------------

void DrawMap(void)
{
  for( int y = 0; y < MAP_HEIGHT; y++)
  {
   for(int x=0; x <MAP_WIDTH>Edit2->Text = x;  Form1->Edit2->Refresh();
      Form1->Edit1->Text = y;  Form1->Edit1->Refresh();
     } // end of for loop
  } // end of for loop


Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = "@";
Form1->StringGrid1->Refresh();

}


// IsPassable Function ///////////////////////////////////////////////////////////////////
//
// This function analyzes the coordinates of the map array specified and returns
// true if the coordinate is passable (able for the player to occupy), false if not.
//
bool IsPassable( int x, int y )
{
// Before we do anything, make darn sure that the coordinates are valid
if( x <0>= MAP_WIDTH || y <0>= MAP_HEIGHT )
return false;
// Store the value of the tile specified
int nTileValue = nMapArray[y][x];
// Return true if it's passable
return nTileIndex[nTileValue].bPassable;
}

 // DrawTile Function /////////////////////////////////////////////////////////////////////
//
// Draws a map tile for the map coordinates specified.
//
void DrawTile( int x, int y )
{
  int nType = nMapArray[y][x];

  Form1->StringGrid1->Canvas->Font->Color = nTileIndex[nType].nColorCode;
  Form1->StringGrid1->Cells[x][y] = nTileIndex[nType].nCharacter;
}

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
 DrawMap();
 StringGrid1->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox_RejillaClick(TObject *Sender)
{
 if(CheckBox_Rejilla->Checked == true) StringGrid1->GridLineWidth = 2;
 else StringGrid1->GridLineWidth = 0;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
        TGridDrawState State)
{
  // standard colors
 ////////////////////////////////
 // clBlack   = TColor($000000);
 // clMaroon  = TColor($000080);
 // clGreen   = TColor($008000);
 // clOlive   = TColor($008080);
 // clNavy    = TColor($800000);
 // clPurple  = TColor($800080);
 // clTeal    = TColor($808000);
 // clGray    = TColor($808080);
 // clSilver  = TColor($C0C0C0);
 // clRed     = TColor($0000FF);
 // clLime    = TColor($00FF00);
 // clYellow  = TColor($00FFFF);
 // clBlue    = TColor($FF0000);
 // clFuchsia = TColor($FF00FF);
 // clAqua    = TColor($FFFF00);
 // clLtGray  = TColor($C0C0C0); // clSilver alias
 // clDkGray  = TColor($808080); // clGray alias
 // clWhite   = TColor($FFFFFF);

   TRect r(Rect);
   StringGrid1->Canvas->FillRect(r);
   //StringGrid1->Canvas->Brush->Style = bsClear;
   //DrawText(StringGrid1->Canvas->Handle, StringGrid1->Cells[ACol][ARow].c_str(),
   //         StringGrid1->Cells[ACol][ARow].Length(), &Rect,
   //         DT_CENTER | DT_VCENTER | DT_SINGLELINE );


  if ((ACol>=0) && (ARow>=0))
  {
   if (StringGrid1->Cells[ACol][ARow] == "#")
   {
    StringGrid1->Canvas->Font->Color = clWhite;
    StringGrid1->Canvas->Brush->Color = clBlack;
   }
   else if (StringGrid1->Cells[ACol][ARow] == ".")
   {
    StringGrid1->Canvas->Font->Color = clLime;
    StringGrid1->Canvas->Brush->Color = clBlack;

   }
   else if(StringGrid1->Cells[ACol][ARow] == "$")
    {

      StringGrid1->Canvas->Font->Color = clYellow;
    }


   else if(StringGrid1->Cells[ACol][ARow] == "^")
    {

      StringGrid1->Canvas->Font->Color = clWhite;
    }
   else if(StringGrid1->Cells[ACol][ARow] == "&")
    {

      StringGrid1->Canvas->Font->Color = clGreen;
    }
   else if(StringGrid1->Cells[ACol][ARow] == "~")
    {

      StringGrid1->Canvas->Font->Color = clAqua;
    }

//   else if (StringGrid1->Cells[ACol][ARow] == "%")
//    StringGrid1->Canvas->Brush->Color = clLime;
//   else if (StringGrid1->Cells[ACol][ARow] == "&")
//    StringGrid1->Canvas->Brush->Color = clLime;
//   else if (StringGrid1->Cells[ACol][ARow] == "?")
//    StringGrid1->Canvas->Brush->Color = clLime;
//   else if (StringGrid1->Cells[ACol][ARow] == "^")
//    StringGrid1->Canvas->Brush->Color = clLime;

   StringGrid1->Canvas->FillRect(Rect);
   StringGrid1->Canvas->TextOut(Rect.Left,Rect.Top,StringGrid1->Cells[ACol][ARow]);
  }
}
//---------------------------------------------------------------------------



void __fastcall TForm1::StringGrid1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)

{
     int nDeltaX;
    int nDeltaY;

// Process the input
switch(Key)
{
// Move up
case VK_UP:
nDeltaX = 0;
nDeltaY = -1;
break;
// Move left
case VK_LEFT:
nDeltaX = -1;
nDeltaY = 0;
break;
// Move right
case VK_RIGHT:
nDeltaX = 1;
nDeltaY = 0;
break;
// Move down
case VK_DOWN:
nDeltaX = 0;
nDeltaY = 1;
break;
// Escape key
case VK_ESCAPE:
// Quit the program
Form1->Close();
// Ignore all other keys
default:
break;
}
// Check and see if we're allowed to move in the direction specified
if( IsPassable(nPlayerX + nDeltaX, nPlayerY + nDeltaY) )
{
// If allowed, move in the direction specified

Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = backup_fondo;
backup_fondo = Form1->StringGrid1->Cells[nPlayerX+nDeltaX][nPlayerY+nDeltaY];
nPlayerX += nDeltaX;
nPlayerY += nDeltaY;
Form1->StringGrid1->Cells[nPlayerX][nPlayerY] = "@";
}

}
//---------------------------------------------------------------------------



y el archivo de cabecera Unit.h sigue mas o menos igual salvo que hemos añadido el evento StringGridKeyDown para controlar las pulsaciones del teclado

Código:

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes>
#include <Controls>
#include <StdCtrls>
#include <Forms>
#include <Grids>
#include <Buttons>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:   // IDE-managed Components
   TStringGrid *StringGrid1;
   TBitBtn *BitBtn1;
   TGroupBox *GroupBox1;
   TCheckBox *CheckBox_Rejilla;
   TEdit *Edit1;
   TEdit *Edit2;
   TLabel *Label1;
   TLabel *Label2;
   void __fastcall BitBtn1Click(TObject *Sender);
   void __fastcall CheckBox_RejillaClick(TObject *Sender);
   void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
          TGridDrawState State);
   void __fastcall StringGrid1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift);

private:   // User declarations
public:      // User declarations
   __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif




el resultado podéis verlo aqui:





no se porque sale mal el codigo truncado en la funcion DrawMap()
asi que os dejo el archivo comprimido con el codigo fuente


http://www.qualinost.net/descargas/ROGUELIKES.rar
Volver arriba
Ver perfil de usuario Enviar mensaje privado
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.