How to make a beautiful code to read informations about a level from a file

General

jicehel

5 years ago

Hello, i would add in my code the data about levels in the directory levels of the game.

For each levels, i would have a file named levelX.sok where x is the number of the level.

In each of this file i would have the data. 

For example for level0, the file would be levels/level0.sok and in this file where will be:

19 11
    #####          
    #   #          
    #   #          
  ###   ##        
  #      #        

# ## #  

  # ## #####  ..

            @$..

### # ##  ..

    #     #########
    #######       

(The structure of the file is the official sok file for sokoban to use the thousends levels existing)

Can someone explain me how to make a good code for Mera to read these files and use data as i can't use gb.save.get in this case or i missed something ? I think that in my case i have to open a file and read it but is it well how i have to do it ? 


is it ok to write something like:

#include <Gamebuino-Meta.h>
#include "Graphics.h"


char NB_COLONNES = ;
char NB_LIGNES;
char X; // Position hrizontale du personnage
char Y; // Position verticale du personnage 

char niveau[NB_LIGNES_NIVEAUX][NB_COLONNES_NIVEAUX];
char state;


void setup() {
   state="INIT";
}


void loop(){
   if (state=="INIT") ChargeNiveau();
    while(!gb.update());  
    gb.display.clear();
   // On affiche le contenu de chacune des cases  
   if (state=="RUN") DessineNiveau(X,Y);
}


void Charge_Niveau() {
    FILE* fichier = NULL;
    char temp[] = {0};
    fichier = fopen("levels/level0.sok", "r");
    if (fichier != NULL)
    {
        fscanf(fichier, "%d %d", NB_COLONNES, NB_LIGNES);
        for (int ligne=0;ligne<NB_LIGNES;ligne++) {
                fscanf(fichier, "%s", temp);
                for (int colonne=0;colonne<NB_COLONNES;colonne++) {
                       niveau[ligne][colonne] =
                       if (niveau[ligne][colonne]=='@'){
                              X = colonne; 
                              Y = ligne;
                       }
                }
        } 
        fclose(fichier);
    }
}


Please correct my faults, or bad practices or explain my how i should proceed.

Else, i have had another problem of text display when adding my text of the level data (at the end, the text begin to be title until normal)

alxm

NEW 5 years ago

The standard C file functions won't work here, you have to use the SdFat library which is also what the gb.save functions use under the hood. The Meta library already sets it up and provides an SdFat SD object, so it's ready to use. Check out some of the example code to get started, and also look at other projects and how they use it, like the Meta library itself or Siegfried's tilemap streamer. Since everything is already set up, you can jump straight to the myFile = SD.open("test.txt", ... part at line 46.

As for level loading code, I think you're on the right track. You open a text file and read the first line, which tells you the width and height of the map data that follows. Then you read each subsequent line and match each character in it to a tile code, or check if the character is the player start position marker.

jicehel

NEW 5 years ago

Many thanks for your answer, i'll try alxm and i'll put here if i success with these good informations.

jicehel

NEW 5 years ago

Deleted text - had big errors in .. I have to hide it ...

alxm

5 years ago

What's the error when you compile? You don't need to include SdFat.h by the way, Gamebuino-Meta.h includes it.

alxm

NEW 5 years ago

jicehel jicehel

What's the error when you compile? You don't need to include SdFat.h by the way, Gamebuino-Meta.h includes it.

jicehel

NEW 5 years ago

Yes, thanks. It was the error as is was declaring something that already exist. Thanks. I'll be able to continue now. Thanks for your help and support

jicehel

NEW 5 years ago

Argh, i have make many try but i don't success to display something on my screen or on the serial. I have to go working but say me if you see another error on this test code:

#include <Gamebuino-Meta.h>

#include "Graphics.h"
File myFile;

#define NB_LIGNES_NIVEAUX 11
#define NB_COLONNES_NIVEAUX 19

char X; // Position hrizontale du personnage
char Y; // Position verticale du personnage 

char niveau[NB_LIGNES_NIVEAUX][NB_COLONNES_NIVEAUX];


void setup() {
  int c;
  gb.begin();
  gb.display.clear();
  gb.display.println("Setup start");
  SerialUSB.begin(9600);
  SerialUSB.println("Début");
  myFile = SD.open("level0.sok");
  // Serial.println(myFile);
  // gb.display.println(myFile);
  if (myFile) {
    while (myFile.available()) {
        SerialUSB.print(myFile.read());
    }
    /* c = myFile.read();
    Serial.println(c);
    gb.display.println(c);
    c = myFile.read();
    Serial.println(c);
    Serial.println("Boucle");
    while ((c = myFile.read()) > 0) Serial.print(char(c)); 
    Serial.println("Fin");*/
    myFile.close();
  } else {
    SerialUSB.println("Echec d'ouverture du fichier");
  }
  X = -1;
  Y = -1;
}


void trouve_position_perso() {
    for (int ligne=0;ligne<NB_LIGNES_NIVEAUX;ligne++) {
     for (int colonne=0;colonne<NB_COLONNES_NIVEAUX;colonne++) {
       if (niveau[ligne][colonne]=='@'){
           X = colonne;
           Y = ligne;
       }
     }
   } 
}

void loop() {
  while(!gb.update());
  // gb.display.clear();
  // Initialisation de la position du personnage pour cette étape
  // if (X==-1 && Y==-1) trouve_position_perso();

  // On affiche le contenu de chacune des cases
  // DessineNiveau(X,Y);
}

jicehel

NEW 5 years ago

When i'll have understood, i'll make another tuto for Sukoban to expalin how to use data files. I'm sure that some good programmers know how to use it but when others like me will understand it, it's will make us able to make more great games and to let us make some evolutive games as end user will be able to edit / change and propose levels for some games. Or change sprites and monster roster depending of the current zone in an adventure game... But first i have to success. Now, there is no more error but no result too yet... I make another error of use, but as i don't see anything i don't success to debug. It's probably a stupid error.

jicehel

NEW 5 years ago

I have an idea, as i make it on arduino IDE, does it simulate SD card ? My question is when i make tries: there is nothing on the SD card as it's on hard drive but does Arduino IDE emulate a SD card or have i to make an repesitory and test in a folder on the Meta with all data in ?

jicehel

NEW 5 years ago

Someone have already done it ? Can i test it from the IDE or have i to compile and transfert on SD card to test ?

Sutchig

NEW 5 years ago

Hi, i am afraid you have to test it on real hardware. Back in the classic times there were  simbuino which could emulate a sd card. But the arduino ide doesnt contain any emulation. All you can see on arduino ide is if your code is valid - not that it really works :/

jicehel

NEW 5 years ago

Thanks all for your help and yes, it was what i had to do: try with prog in the SD card (and corect some errors).

This is my test program and it read well the data.

Thanks for your help

#include <Gamebuino-Meta.h>

#include "Graphics.h"
File myFile;

#define NB_LIGNES_NIVEAUX 11
#define NB_COLONNES_NIVEAUX 19

char X; // Position hrizontale du personnage
char Y; // Position verticale du personnage 

char niveau[NB_LIGNES_NIVEAUX][NB_COLONNES_NIVEAUX];


void setup() {
  int c;
  gb.begin();
  gb.display.clear();
  gb.display.println("Setup start");
  myFile = SD.open("level0.sok");
  if (myFile) {
    while ((c = myFile.read()) > 0) {
      gb.display.print(char(c)); 
    }
    myFile.close();
  } else {
    gb.display.println("Echec d'ouverture du fichier");
  }
  X = -1;
  Y = -1;
}


void trouve_position_perso() {
    for (int ligne=0;ligne<NB_LIGNES_NIVEAUX;ligne++) {
     for (int colonne=0;colonne<NB_COLONNES_NIVEAUX;colonne++) {
       if (niveau[ligne][colonne]=='@'){
           X = colonne;
           Y = ligne;
       }
     }
   } 
}

void loop() {
  while(!gb.update());
  // gb.display.clear();
  // Initialisation de la position du personnage pour cette étape
  // if (X==-1 && Y==-1) trouve_position_perso();

  // On affiche le contenu de chacune des cases
  // DessineNiveau(X,Y);
}


dreamer3

NEW 5 years ago

Funny, I was just thinking about doing Sokoban the other day.

jicehel

NEW 5 years ago

;)  I wait to see if chris-scientist make a third part of the tuto on the Sokoban Subjet to make programs oriented objects and after i'll make another for files (to load levels and save progression)