OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Load a json map into pygame (using Tiled)

  • Thread starter Thread starter Giovanni Isaac
  • Start date Start date
G

Giovanni Isaac

Guest
I'm coming here to see if I could get some help with my project. I'm trying to map a dungeon crawler like game, and I want to create a map using Tiled. I managed to print the map using processing code, however, my project has to be in pytho, and I don't know how to translate my code. Even without trying to translate, I don't know how could I print a json map in pygame.

Does someone have some clues please? Thank you !

Firstly I tried to export my map into a csv file, but the import was.. weird and didn't created what I expected. The elements weren't at the right place for some reason. With the json export, I tried to translate the processing code into python but I didn't managed to do something which was working.

PS: by the way, I'm not very familiar with Stack Overlow, and so I don't really know how to properly ask for help, don't hesitate to tell me what you eventually need !

Here is my processing code to print the map :

Code:
JSONObject map;
PImage tilset;
PImage[] tile_BG;
PImage[] tile_sBG;
PImage[] tile_O;
PImage[] tile_Start;
int nb_tuile_x, nb_tuile_y, taille_tuile_x, taille_tuile_y;
int[] BG_layer;
int[] sBG_layer;
int[] O_layer;
int[] Start_layer;

void setup()
{
  size(320, 320);
  map = loadJSONObject("carte.json");
  taille_tuile_x= map.getInt("tileheight");
  taille_tuile_y= map.getInt("tilewidth");
  nb_tuile_x = map.getInt("width");
  nb_tuile_y = map.getInt("height");
  println("Les tuiles sont de dimmensions : "+taille_tuile_x+"px par "+taille_tuile_y+"px");
  println("La carte a pour dimension : "+nb_tuile_x+" tuiles de long sur "+nb_tuile_y+" tuiles de haut");
  println("Soit un total de : "+nb_tuile_x*nb_tuile_y+" tuiles");
  JSONArray layers = map.getJSONArray("layers");
  println("La carte est composée de "+layers.size()+" couches");
  JSONObject layer0 = layers.getJSONObject(0);
  JSONArray data0 = layer0.getJSONArray("data");
  JSONObject layer1 = layers.getJSONObject(1);
  JSONArray data1 = layer1.getJSONArray("data");
  JSONObject layer2 = layers.getJSONObject(2);
  JSONArray data2 = layer2.getJSONArray("data");
  JSONObject layer3 = layers.getJSONObject(3);
  JSONArray data3 = layer3.getJSONArray("data");
  BG_layer = data0.getIntArray();
  sBG_layer = data1.getIntArray();
  O_layer = data2.getIntArray();
  Start_layer = data3.getIntArray();
  tilset=loadImage("tiles.png");
  tile_BG = new PImage[BG_layer.length];
  tile_sBG = new PImage[sBG_layer.length];
  tile_O = new PImage[O_layer.length];
  tile_Start = new PImage[Start_layer.length];
  for (int i=0; i<BG_layer.length; i++)
  {
  int coordX_tuile=(BG_layer[i]%25)*16-16;
  int coordY_tuile=BG_layer[i]/25*16;
  tile_BG[i]=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
  }
  for (int i=0; i<tile_BG.length; i++)
  {
  if (BG_layer[i]!=0) image(tile_BG[i], (i%20)*16, (i/20)*16);
  }
  
  for (int i=0; i<sBG_layer.length; i++)
  {
  int coordX_tuile=(sBG_layer[i]%25)*16-16;
  int coordY_tuile=sBG_layer[i]/25*16;
  tile_sBG[i]=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
  }
  for (int i=0; i<tile_sBG.length; i++)
  {
  if (sBG_layer[i]!=0) image(tile_sBG[i], (i%20)*16, (i/20)*16);
  }
  
  for (int i=0; i<O_layer.length; i++)
  {
  int coordX_tuile=(O_layer[i]%25)*16-16;
  int coordY_tuile=O_layer[i]/25*16;
  tile_O[i]=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
  }
  for (int i=0; i<tile_O.length; i++)
  {
  if (O_layer[i]!=0) image(tile_O[i], (i%20)*16, (i/20)*16);
  }
  
  for (int i=0; i<Start_layer.length; i++)
  {
  int coordX_tuile=(Start_layer[i]%25)*16-16;
  int coordY_tuile=Start_layer[i]/25*16;
  tile_Start[i]=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
  }
  for (int i=0; i<tile_Start.length; i++)
  {
  if (Start_layer[i]!=0) image(tile_Start[i], (i%20)*16, (i/20)*16);
  }
  }
void draw(){}
<p>I'm coming here to see if I could get some help with my project. I'm trying to map a dungeon crawler like game, and I want to create a map using Tiled. I managed to print the map using processing code, however, my project has to be in pytho, and I don't know how to translate my code. Even without trying to translate, I don't know how could I print a json map in pygame.</p>
<p>Does someone have some clues please? Thank you !</p>
<p>Firstly I tried to export my map into a csv file, but the import was.. weird and didn't created what I expected. The elements weren't at the right place for some reason. With the json export, I tried to translate the processing code into python but I didn't managed to do something which was working.</p>
<p>PS: by the way, I'm not very familiar with Stack Overlow, and so I don't really know how to properly ask for help, don't hesitate to tell me what you eventually need !</p>
<p>Here is my processing code to print the map :</p>
<pre><code>JSONObject map;
PImage tilset;
PImage[] tile_BG;
PImage[] tile_sBG;
PImage[] tile_O;
PImage[] tile_Start;
int nb_tuile_x, nb_tuile_y, taille_tuile_x, taille_tuile_y;
int[] BG_layer;
int[] sBG_layer;
int[] O_layer;
int[] Start_layer;

void setup()
{
size(320, 320);
map = loadJSONObject("carte.json");
taille_tuile_x= map.getInt("tileheight");
taille_tuile_y= map.getInt("tilewidth");
nb_tuile_x = map.getInt("width");
nb_tuile_y = map.getInt("height");
println("Les tuiles sont de dimmensions : "+taille_tuile_x+"px par "+taille_tuile_y+"px");
println("La carte a pour dimension : "+nb_tuile_x+" tuiles de long sur "+nb_tuile_y+" tuiles de haut");
println("Soit un total de : "+nb_tuile_x*nb_tuile_y+" tuiles");
JSONArray layers = map.getJSONArray("layers");
println("La carte est composée de "+layers.size()+" couches");
JSONObject layer0 = layers.getJSONObject(0);
JSONArray data0 = layer0.getJSONArray("data");
JSONObject layer1 = layers.getJSONObject(1);
JSONArray data1 = layer1.getJSONArray("data");
JSONObject layer2 = layers.getJSONObject(2);
JSONArray data2 = layer2.getJSONArray("data");
JSONObject layer3 = layers.getJSONObject(3);
JSONArray data3 = layer3.getJSONArray("data");
BG_layer = data0.getIntArray();
sBG_layer = data1.getIntArray();
O_layer = data2.getIntArray();
Start_layer = data3.getIntArray();
tilset=loadImage("tiles.png");
tile_BG = new PImage[BG_layer.length];
tile_sBG = new PImage[sBG_layer.length];
tile_O = new PImage[O_layer.length];
tile_Start = new PImage[Start_layer.length];
for (int i=0; i<BG_layer.length; i++)
{
int coordX_tuile=(BG_layer%25)*16-16;
int coordY_tuile=BG_layer/25*16;
tile_BG=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
}
for (int i=0; i<tile_BG.length; i++)
{
if (BG_layer!=0) image(tile_BG, (i%20)*16, (i/20)*16);
}

for (int i=0; i<sBG_layer.length; i++)
{
int coordX_tuile=(sBG_layer%25)*16-16;
int coordY_tuile=sBG_layer/25*16;
tile_sBG=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
}
for (int i=0; i<tile_sBG.length; i++)
{
if (sBG_layer!=0) image(tile_sBG, (i%20)*16, (i/20)*16);
}

for (int i=0; i<O_layer.length; i++)
{
int coordX_tuile=(O_layer%25)*16-16;
int coordY_tuile=O_layer/25*16;
tile_O=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
}
for (int i=0; i<tile_O.length; i++)
{
if (O_layer!=0) image(tile_O, (i%20)*16, (i/20)*16);
}

for (int i=0; i<Start_layer.length; i++)
{
int coordX_tuile=(Start_layer%25)*16-16;
int coordY_tuile=Start_layer/25*16;
tile_Start=tilset.get(coordX_tuile, coordY_tuile, 16, 16);
}
for (int i=0; i<tile_Start.length; i++)
{
if (Start_layer!=0) image(tile_Start, (i%20)*16, (i/20)*16);
}
}
void draw(){}
</code></pre>
 
Top