none
Can this code be used to randomly generate terrain? RRS feed

  • Question

  • GraphicsWindow.Height = 500
    GraphicsWindow.Width = 700
    GraphicsWindow.CanResize = "False"

    Sub CreateGrid
      x_pos_1 = 5
      y_pos_1 = 5
      While (y_pos_1 < 500)
        For i = 1 To 14
          map_row[i] = Shapes.AddImage("C:\Users\John\Desktop\VideoGame\Images\50_grid_square.png")
          Shapes.Move(map_row[i], x_pos_1, y_pos_1)
          x_pos_1 = x_pos_1 + 50
        EndFor
        y_pos_1 = y_pos_1 + 50
        x_pos_1 = x_pos_1 - 700
      EndWhile
    EndSub

    CreateGrid()

    Or be used to do anything useful?

    Monday, July 1, 2013 4:06 AM

Answers

  • You are right, GetPixel() only reads the values from the background :-(

    You could separate the logic and the view of your map. This means you have one array (map) that stores only what terrain is at this position. Another array (map_shape) stores the corresponding shape that represents the terrain graphical.

    I changed your code a little bit for demonstration, I hope that's ok. To run it with your graphics, you have to but them into a folder called gfx or change

    _gfx_path = Program.Directory + "\gfx"

    to

    _gfx_path = Program.Directory + "\Images"

    if your program (.bs file) is at

    C:\Users\John\Desktop\VideoGame\

    GraphicsWindow.Height = 500
    GraphicsWindow.Width = 700
    GraphicsWindow.CanResize = "False"
    
    GFXLoad()
    CreateMap()
    ShowMapGraphical()
    ShowMapTextual()
    
    Sub CreateMap
      For x = 1 To 14
        For y = 1 To 10
          number = Math.GetRandomNumber(Array.GetItemCount(GFX))
          array_number_to_index = Array.GetAllIndices(GFX)
          map[x][y] = array_number_to_index[number]
        EndFor
      EndFor
    EndSub
    
    Sub ShowMapGraphical
        For x = 1 To 14
        For y = 1 To 10
          map_shape[x][y] = Shapes.AddImage(GFX[map[x][y]])
          Shapes.Move(map_shape[x][y], 5 + (x-1)*50,  5 + (y-1)*50)
        EndFor
      EndFor
    EndSub
    
    Sub ShowMapTextual
        For x = 1 To 14
          For y = 1 To 10
          map_label = Shapes.AddText(map[x][y])
          Shapes.Move(map_label[x][y], 5 + (x-1)*50,  5 + (y-1)*50)
        EndFor
      EndFor
    EndSub
    
    
    ' load graphics  -  store all images in the GFX array
    ' example: access "./gfx/SaMpLe.Png" with GFX["sample"]
    Sub GFXLoad
      _gfx_path = Program.Directory + "\gfx"
      _gfx_filenames = File.GetFiles(_gfx_path)
      For i = 1 to Array.GetItemCount(_gfx_filenames)
        _gfx_index = Text.GetSubTextToEnd(_gfx_filenames[i], Text.GetLength(_gfx_path)+2)
        _gfx_index = Text.GetSubText(_gfx_index, 1, Text.GetLength(_gfx_index) - 4)
        _gfx_index = Text.ConvertToLowerCase(_gfx_index)
        GFX[_gfx_index] = ImageList.LoadImage(_gfx_filenames[i])
      EndFor
    EndSub

    Tuesday, July 2, 2013 7:32 AM

All replies

  • I think your code is useful.

    You could for example insert different images (randomly selected) to create your terrain.

    Something like:

    terrain[1] = ImageList.LoadImage(...)
    terrain[2] = ImageList.LoadImage(...)
    terrain[3] = ImageList.LoadImage(...)
    ...
    
    Sub CreateGrid
      ...
      While (y_pos_1 < 500)
        For i = 1 To 14
          map_row[i] = Shapes.AddImage(terrain[Math.GetRandomNumber(Array.GetItemCount(terrain))])
          Shapes.Move(map_row[i], x_pos_1, y_pos_1)
          ...
        EndFor
        ...
      EndWhile
    EndSub


    Monday, July 1, 2013 8:37 AM
  • Thank you for your reply!

    I have modified the code a little bit:

    GraphicsWindow.Height = 500
    GraphicsWindow.Width = 700
    GraphicsWindow.CanResize = "False"
    GraphicsWindow.BackgroundColor = "Black"
    GraphicsWindow.Title = "Random Tree Generator!"

    Sub CreateMap
      random_img[1] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[2] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[3] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[4] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[5] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[6] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[7] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[8] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[9] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[10] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[11] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[12] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[13] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[14] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[15] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[16] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[17] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[18] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[19] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[20] = "C:\Users\John\Desktop\VideoGame\Images\stone_grid.png"
      x_pos_1 = 5
      y_pos_1 = 5
      While (y_pos_1 < 500)
        For i = 1 To 14
          map_row[i] = Shapes.AddImage(random_img[Math.GetRandomNumber(20)])
          Shapes.Move(map_row[i], x_pos_1, y_pos_1)
          x_pos_1 = x_pos_1 + 50
        EndFor
        y_pos_1 = y_pos_1 + 50
        x_pos_1 = x_pos_1 - 700
      EndWhile
    EndSub

    CreateMap()

    Is there a more efficient way to create an Array so that a certain image appears less than the others?

    Also after the images are placed into the Graphics Window is there anyway to interact with them?  Such as a MouseDown event to say chop down a tree?  I cannot figure out how I have stored the images with the For loop and so I can't access the indices.  Any help is appreciated!!!

    Monday, July 1, 2013 8:43 AM
  • By the way that current code does generate a nice random terrain:)
    Monday, July 1, 2013 8:44 AM
  • Is there a more efficient way to create an Array so that a certain image appears less than the others?

    You could store in a separate array (for example random_freq) how likely it should be that the image gets selected.

    In your code this means for the first image:

    random_img[1] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
    random_freq[1] = 9

    Additionally you have a variable freq that stores the sum of all random_freq entries. The following code choose randomly an image:

    random_freq[1] = 10
    random_freq[2] = 15
    random_freq[3] = 20
    freq = 45
    
    number = Math.GetRandomNumber(freq)
    random_index = 1
    While number - random_freq[random_index] > 0
      number = number - random_freq[random_index]
      random_index = random_index + 1
    EndWhile
    
    ' randomly selected image is random_img[random_index]

    Monday, July 1, 2013 9:08 AM
  • Also after the images are placed into the Graphics Window is there anyway to interact with them?

    With Shapes.GetTop() and Shapes.GetLeft() you get the current position of your shape (only reliable if the shape doesn't move - Shapes.Animate() is not used).

    With ImageList.GetWidthOfImage() and ImageList.GetHeightOfImage() you get the width and height of the image.

    And finally with GraphicsWindow.MouseX and GraphicsWindow.MouseY you get the current position of your mouse pointer.

    So one solution could be to calculate for every shape if the mouse position is within this shape or not.

    Monday, July 1, 2013 9:16 AM
  • Can you post the images online??

    random_img[1] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[2] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[3] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[4] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[5] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[6] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[7] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[8] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[9] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[10] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[11] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[12] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[13] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[14] = "C:\Users\John\Desktop\VideoGame\Images\grass_grid.png"
      random_img[15] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[16] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[17] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[18] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[19] = "C:\Users\John\Desktop\VideoGame\Images\tree_grid.png"
      random_img[20] = "C:\Users\John\Desktop\VideoGame\Images\stone_grid.png"

    We can see any of these unless you do.


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Monday, July 1, 2013 8:17 PM
    Answerer
  • Are you creating a Minecraft-Type game?

    ardiezc_quazhulu was here.

    Tuesday, July 2, 2013 12:58 AM
  • Florian,

    I actually tried that, but I could not find a way to differentiate between the stone, tree, or grass images.  I tried using GetPixel but I guess that only detects the background color, not images.  I was trying to find a way to possibly draw the images to the background, or somehow detect image color.

    Also I tried to find a way to maybe use the indices of the Arrays that are created for each For loop.  For one it does not store the Image name, but just a number.  1;1;1;2;1;3;1;4, like that all the way to 14.  I will try to use ImageList.

    ardiezc_quazhulu,

    Actually I want to create a tiny city builder type game.  For now just some simple elements and maybe the ability to build one or two buildings.  Thought I'd work on the grid and map first.  I'll store the images and post the link.  I'm sorry, I guess I thought everyone had access to my C: drive, lol.

    Tuesday, July 2, 2013 3:53 AM
  • Hi Florian,

    Thank you for that code, I'll have to try it.

    Would it be able to detect what image is in that particular coordinate?  I tried using GetPixel, but that didn't work.  I guess it just detects the background.  I was also trying to figure out the Array created by the For loop.  If I could access the indices, it might be easier to find a way to detect them.

    hello ardiezc_quazhulu,

    I actually want to build a small city builder game.  Just simple stuff first.  Simple resources, and just a couple options for buildings.  I want to add food and stuff later too.  I tried to post the images but my account has to be verified first.

    Look forward to your replies:)

    Tuesday, July 2, 2013 4:14 AM
  • You are right, GetPixel() only reads the values from the background :-(

    You could separate the logic and the view of your map. This means you have one array (map) that stores only what terrain is at this position. Another array (map_shape) stores the corresponding shape that represents the terrain graphical.

    I changed your code a little bit for demonstration, I hope that's ok. To run it with your graphics, you have to but them into a folder called gfx or change

    _gfx_path = Program.Directory + "\gfx"

    to

    _gfx_path = Program.Directory + "\Images"

    if your program (.bs file) is at

    C:\Users\John\Desktop\VideoGame\

    GraphicsWindow.Height = 500
    GraphicsWindow.Width = 700
    GraphicsWindow.CanResize = "False"
    
    GFXLoad()
    CreateMap()
    ShowMapGraphical()
    ShowMapTextual()
    
    Sub CreateMap
      For x = 1 To 14
        For y = 1 To 10
          number = Math.GetRandomNumber(Array.GetItemCount(GFX))
          array_number_to_index = Array.GetAllIndices(GFX)
          map[x][y] = array_number_to_index[number]
        EndFor
      EndFor
    EndSub
    
    Sub ShowMapGraphical
        For x = 1 To 14
        For y = 1 To 10
          map_shape[x][y] = Shapes.AddImage(GFX[map[x][y]])
          Shapes.Move(map_shape[x][y], 5 + (x-1)*50,  5 + (y-1)*50)
        EndFor
      EndFor
    EndSub
    
    Sub ShowMapTextual
        For x = 1 To 14
          For y = 1 To 10
          map_label = Shapes.AddText(map[x][y])
          Shapes.Move(map_label[x][y], 5 + (x-1)*50,  5 + (y-1)*50)
        EndFor
      EndFor
    EndSub
    
    
    ' load graphics  -  store all images in the GFX array
    ' example: access "./gfx/SaMpLe.Png" with GFX["sample"]
    Sub GFXLoad
      _gfx_path = Program.Directory + "\gfx"
      _gfx_filenames = File.GetFiles(_gfx_path)
      For i = 1 to Array.GetItemCount(_gfx_filenames)
        _gfx_index = Text.GetSubTextToEnd(_gfx_filenames[i], Text.GetLength(_gfx_path)+2)
        _gfx_index = Text.GetSubText(_gfx_index, 1, Text.GetLength(_gfx_index) - 4)
        _gfx_index = Text.ConvertToLowerCase(_gfx_index)
        GFX[_gfx_index] = ImageList.LoadImage(_gfx_filenames[i])
      EndFor
    EndSub

    Tuesday, July 2, 2013 7:32 AM
  • Interesting.  Thank you Florian:)  And no I do not mind at all!:)
    Tuesday, July 2, 2013 7:57 AM