Results 1 to 3 of 3

Thread: random tile map, HELP!

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    12

    random tile map, HELP!

    hi, im fairly new to vb 2010, and i have been working on a tile map that generates random tiles to form a 2d map. Not like the maps in most rpgs but more like the map in mario, where you travel side to side. I would much appreciate it if someone could provide a SIMPLE system to do this. I have read up on arrays so i think thats the best choice. But how would you choose randomly between 3 variables, dirt, nothing, and rock to go into each position on the form. Note that i am using the form.paint to write all my graphics. If you dont understand anything, please ask. By the way, im using a 10 x 3 tile map, so i would need 30 individual tiles.

    Please Reply soon!

  2. #2
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: random tile map, HELP!

    Ok, Here we go. the only things in the project are a Form, and one Button. the rest is code:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     'Created this Enum, just to make things easier to keep track of later
    4.     Enum Tiles As Integer
    5.         None = 0
    6.         Dirt = 1
    7.         Rock = 2
    8.     End Enum
    9.  
    10.     Dim Map(9, 2) As Integer
    11.     '...Map(x, y) - This should make it easier.
    12.     'Map is a 2 dimentional Array, of 10x3 (0-9, 0-2)
    13.     'This will act as your Map
    14.  
    15.     'Note that I made the map Global. It would be better to make
    16.     'it local and pass it to the functions, but I wanted to
    17.     'keep things simple for this example
    18.  
    19.     'The Random Number code, I adapted from DeveloperFusion.com... Credit where due.
    20.     Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
    21.     Public Function GetRandomNumber(ByVal Low As Integer, _
    22.                                     ByVal High As Integer) As Integer
    23.         Return objRandom.Next(Low, High + 1)
    24.     End Function
    25.  
    26.  
    27.     Private Sub CreateMap()
    28.  
    29.         'These two integers are going to keep track of what x/y position we are at
    30.         'while we randomly choose what tile to put in each place
    31.         Dim x As Integer
    32.         Dim y As Integer
    33.  
    34.         For x = 0 To 9
    35.             For y = 0 To 2
    36.  
    37.                 'Now we set each tile position to either 0, 1, or 2.
    38.                 'each represent a different tile (see ENUM)
    39.                 Map(x, y) = GetRandomNumber(0, 2)
    40.             Next y
    41.         Next x
    42.  
    43.         'Your Map Now holds all of the 'Randomly Generated' Map Data
    44.  
    45.     End Sub
    46.  
    47.     Private Function DrawMap() As Bitmap
    48.  
    49.  
    50.         'These three images will hold your Tile pictures.
    51.         'It makes things cleaner later.
    52.         Dim None As Image = Image.FromFile("C:\None.png")
    53.         Dim Dirt As Image = Image.FromFile("C:\Dirt.png")
    54.         Dim Rock As Image = Image.FromFile("C:\Rock.png")
    55.  
    56.         'Setting these to the height and width of your tiles
    57.         'makes it so we don't have to worry about the position
    58.         'of your tiles. They'll just fall into place
    59.         '(I'm assuming all your tiles are the same size)
    60.         Dim tilewidth = None.Width
    61.         Dim tileheight = None.Height
    62.  
    63.         'Create a Bitmap the size of your map to hold your image data
    64.         Dim BitMAP As New System.Drawing.Bitmap(tilewidth * 10, tileheight * 3)
    65.         'Create a Graphics object on which to draw to the bitmap
    66.         Dim M As Graphics = Graphics.FromImage(BitMAP)
    67.  
    68.         'Same Reason as above for the X and Y, they track the loop.
    69.         Dim x As Integer
    70.         Dim y As Integer
    71.  
    72.         'CurrentX/Y keep track of the REAL position of where the
    73.         'Tiles will be drawn. they're each just the X/Y multiplied
    74.         'by the Height or Width of the tiles
    75.         Dim CurrentX As Integer
    76.         Dim CurrentY As Integer
    77.  
    78.         For x = 0 To 9
    79.             For y = 0 To 2
    80.  
    81.                 'Find the spot to draw the tile
    82.                 CurrentX = x * tilewidth
    83.                 CurrentY = y * tileheight
    84.  
    85.                 'This is where the Enum comes in, makes things easier to read
    86.                 Select Case Map(x, y)
    87.                     Case Tiles.None
    88.                         M.DrawImageUnscaled(None, New Point(CurrentX, CurrentY))
    89.                     Case Tiles.Dirt
    90.                         M.DrawImageUnscaled(Dirt, New Point(CurrentX, CurrentY))
    91.                     Case Tiles.Rock
    92.                         M.DrawImageUnscaled(Rock, New Point(CurrentX, CurrentY))
    93.                 End Select
    94.  
    95.             Next y
    96.         Next x
    97.  
    98.         'Return the Image we've been making
    99.         'so that you can draw it wherever you want.
    100.         Return BitMAP
    101.  
    102.     End Function
    103.  
    104.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    105.         CreateMap()
    106.     End Sub
    107.  
    108.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    109.         e.Graphics.DrawImageUnscaled(DrawMap(), New Point(0, 0))
    110.     End Sub
    111.  
    112.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    113.         CreateMap()
    114.         Me.Refresh()
    115.     End Sub
    116. End Class

    Here's a screenshot of how it worked for me.
    Last edited by stepdragon; Oct 19th, 2011 at 09:26 PM.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    12

    Re: random tile map, HELP!

    Wow! This is perfect. Exactly what I was looking for. Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width