Best way to make world borders for a game.

edited February 2018 in How To...

Hey guys so I play around with coding games but I am wondering what the best way to set boundaries for a player, the way I use now is using co-ordinates points when the player reaches them he can't pass but it becomes excruciating for bigger projects any ideas ? (I was looking at the Sprites library and noticed that the tank demo uses colors then overlaid with the map and I presume when the player interacts with those colors it triggers a certain action but still not sure how to use that).

Answers

  • Context please.

    Show some code

    2D or 3D Game? Maze? Map?

    When you have a data map like a 2d array the border should be part of the map

  • Yes sorry I meant for 2d something along the lines of pacman for example I made pacman and coding each side of the wall for the pacman not to go through the walls and took 800 lines all together so something along those lines to code / work with. What would be more efficent ?

  • edited February 2018

    Separate the code and the data of the level

    Eg you decode the labyrinth as a text file that you load

    Think of this map as grid

    Each cell is a ghost a wall a path or food element

    The text file

    WWWWWW....
    WPWFG......
    WP.....
    

    Or generate randomly which has disadvantages

    Copy the text into a grid where you place pacman in using a nested for loop. The grid here is a 2D array (see tutorials!!!) of type class Cell that you have to write.

    Cell[][] grid;

    Now when pacman or ghost moves basically check the next field in the current direction if it’s a Path or wall etc. and either move there or stop

    Note that this is not an easy game to make although it’s old, especially movement of ghosts but there are good examples in the forum

    Chrisir ;-)

  • In the tank demo all the graphics are sprites and all the sprites are the same size (w x h) so the whole game is like a chessboard grid. The tank is prevented from going through boundaries by checking sprite collisions (Sprites supports pixel level collision detection)

    The demo also uses a simple image to define the starting positions of the boundaries, targets and sprites but you could use a text file instead.

  • @quark and @Chrisir so if I made something like lets say a square grid of 1's around and told the character once he meets that kind of number he cannot pass through and different number eat etc. (I saw this used in minecraft where each type of block had it own number assigned to it).

  • Yes, I think so

  • If you don’t like W, just use 1

    ;-)

  • How do I load the text file into the program/ 2d array ?

  • edited February 2018

    use loadStrings and then a for loop over it

    Then use another for loop and use

    grid[j][i]=loadedText[i].charAt(j);

    ;-)

  • edited February 2018 Answer ✓

    so let's say this is your map in the text file (no empty line or return allowed at its end! Place the cursor at the end of last data line and press delete often to remove those!)

    111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    100001111111100000000001000301100000000000000000000001000001100000000000000000000001000001
    100000000000100000000001000001100000000000100000000001000001100000000000100000000001000001
    100001111110100000000001000001100001111110100000000001000001100001111110100000000001000001
    100001000000111111000001000001100001000000111111000001000001100001000000111111000001000001
    100001011111100000000001000001100001011111100000000001000001100001011111100000000001000001
    100001000000100000000001000001100001000000100000000001000001100001000000100000000001000001
    100001111100100000000001000001100001111100100000000001000001100001111100100000000001000001
    100001000000100000000001000001100001000000100000000001000001100001000000100000000001000001
    100001000000100000000001000001100001000000100000000001000001100001000000100000000001000001
    100001011111111111000001000001100001011111111111000001000001100001011111111111000001000001
    100001000000100000000001000001100001000000100000000001000001100001000000100000000001000001
    100001000000100000000000000001100001000000100000000000000001100001000000100000000000000001
    111111111100100000000001000001111111111100100000000001000001111111111100100000000001000001
    100001000000100000000001000001100001000000100000000001000001100001000000100000000001000001
    101001000000100000000000000001101001000000100000000000000001101001000000100000000000000001
    101001000000111111000001000001101001000000111111000001000001101001000000111111000001000001
    101101000000100000000001000001101101000000100000000001000001101101000000100000000001000001
    101001000000100000000001000001101001000000100000000001000001101001000000100000000001000001
    101001111100100000000000000001101001111100100000000000000001101001111100100000000000000001
    101001000000100000000001000001101001000000100000000001000001101001000000100000000001000001
    101001000000100000000001000001101001000000100000000001000001101001000000100000000001000001
    101001000000111111000000000001101001000000111111000000000001101001000000111111000000000001
    101001000000000000000001000001101001000000000000000001000001101001000000000000000001000001
    111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    

    then we could read and display it with this sketch.

    please note that the grid is getting its size from the text file.

    Also, since nearly all commands have x,y as an order, so has the grid. So i is the row number (Y) but is the 2nd parameter in grid[ j ][ i ], and j is X.

    E.g. here: grid[j][i]=loadedText[i].charAt(j);

    Best, Chrisir ;-)

    // https : // forum.processing.org/two/discussion/comment/117351/#Comment_117351
    
    // what variables are needed for map / maze 
    int rows = 0;
    int cols = 0;
    
    // the map
    int[][] grid;
    
    // -------------------------------------------
    //
    void setup () {
      // this runs only once
      // 
      size(840, 760);
    
      String[] loadedText = loadStrings("map1.txt");
      println (loadedText.length);
      println (loadedText[1].length());
    
      cols=loadedText[1].length(); // length of a line in text file  
      rows=loadedText.length;      // number of lines 
    
      grid =new int[cols][rows];
    
      // working 2D array, nested for loop
      // loop rows
      for (int i = 0; i < rows; i++) {
        //loop cols
        if (loadedText[i].length()==0) {
          println("empty line in text file "+i); 
          exit();
          return;
        }
        for (int j = 0; j < cols; j++) {
          grid[j][i]=loadedText[i].charAt(j);
        } // for
      } // for
      //
    }
    
    void draw () {
      // this runs on and on
      //
      background(0);
    
      // loop rows
      for (int i = 0; i < rows; i++) {
        //loop cols
        for (int j = 0; j < cols; j++) {
    
          if (grid[j][i]=='1') 
            fill(255, 0, 0);
          else fill(0, 255, 0);
    
          float sz=7;
          rect ( j*sz, i*sz, sz, sz);
        } // for
      } // for
    }
    //
    
This discussion has been closed.