Generating a hexagon map [newbie]

I’ve googled and read previous Q&A on this topic and nothing has clicked for me yet.

I would like to learn how, starting from an empty new project, step-by-step, and without use of a sprite or pre-made image, to generate with only code, a small 2D hexagon map with the ability to color fill individual hexagons and move a sprite marker around the map.

The ultimate goal is a 2D map (perhaps no more than 100 tiles in any direction) that supports scrolling and zoom.

I’ve read Amit Patel’s excellent page on hexagons Hexagonal Grids and look forward to learning how to put that into use once I’ve learned how to generate a usable map space.

Thanks for any help that gets me started!

check this site Unity C# and Shader Tutorials this guy have great tutorials and have one about hexagon grid

1 Like

I’ve done a Hexagonal map in pure .net (vb actually), as a test piece but could workout the right hex tile that the mouse was over, one day I’ll work it out, wanted my map editor to be able to do all three square, isometric, hexagon

here is my vb.net code

Public Class Form1
Private mBitmap As Bitmap
Private mTile As Bitmap
Dim mSrc As Rectangle
Dim mTileWidth As Int32
Dim mTileHeight As Int32

Dim mGap As Int32 = 3
  Dim mStartX As Int32 = 0
  Dim mStartY As Int32 = 0

Public Sub New()
  InitializeComponent()
  Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  mBitmap = Bitmap.FromFile(Application.StartupPath & "\" & "Images\tiles.png")

  mTile = New Bitmap(64, 64)

  Using mGraphics = Graphics.FromImage(mTile)
   Dim mDest As Rectangle = New Rectangle(0, 0, mTile.Width, mTile.Height)
   mSrc = New Rectangle(0, 0, mTile.Width, mTile.Height)
   mGraphics.DrawImage(mBitmap, mDest, mSrc, GraphicsUnit.Pixel)
  End Using

  mTileWidth = mTile.Width
  mTileHeight = mTile.Height

End Sub

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
  Dim mDestRect As Rectangle

  Dim mX As Int32 = 0
  Dim mY As Int32 = 0


  Using mFont As New Font("Segoe UI", 7.0F, FontStyle.Bold, GraphicsUnit.Point)
   Using mBrush As New SolidBrush(Color.FromArgb(128, Color.Black))
    For row As Int32 = 0 To 9
     For column As Int32 = 0 To 9

      If (row Mod 2) = 1 Then
        mX = (column * mTileWidth) - 32
      Else
          mX = (column * mTileWidth)
      End If

      mY = (row * mTileHeight) - (row * 16)

      'mY += row * mGap
      'mX += column * mGap

      mDestRect = New Rectangle(mStartX + mX, mY, mTile.Width, mTile.Height)
      e.Graphics.DrawImage(mTile, mDestRect, mSrc, GraphicsUnit.Pixel)

     Dim mREct As New Rectangle((mStartX + mX) + mTile.Width * 0.25F, mY + mTile.Height * 0.25F, mTile.Width * 0.5F, mTile.Height * 0.5F)
     e.Graphics.FillEllipse(mBrush, mREct)

     TextRenderer.DrawText(e.Graphics, String.Format("[{0},{1}]", column, row), mFont, mDestRect, Color.White, TextFormatFlags.NoPadding Or TextFormatFlags.VerticalCenter Or TextFormatFlags.HorizontalCenter)

     Next column
    Next row
   End Using
  End Using
End Sub

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove

  getSelectedHexagon(e.X, e.Y)
'Dim mRow As Int32
' If e.Y < 64 Then
'  mRow = 0
' Else
'    mRow = Math.Floor(e.Y / mTileHeight)
' End If


' Dim mColumn As Object


'  If e.X < 64 Then
'   mColumn = 0
'  Else
'mColumn = Math.Floor(e.X / mTileWidth)
'  End If

'Me.LblRow.Text = "Row is: " & mRow.ToString()
'Me.LblColumn.Text = "Column is: " & mColumn.ToString()
End Sub


Private Sub getSelectedHexagon(ByVal x As Int32, y As Int32)
Dim gridHeight As Int32 = mTileHeight * 0.75
Dim gridWidth As Int32 = mTileWidth
Dim halfWidth As Int32 = 32

    ' Find the row and column of the box that the point falls in.
    Dim row As Int32 = Math.Floor(y / gridHeight)
    Dim column As Int32

    Dim rowIsOdd As Boolean = (row Mod 2) = 1

    'Is the row an odd number?
    If rowIsOdd = True Then ' Yes: Offset x to match the indent of the row
        column = (((x + halfWidth) - mStartX) / gridWidth)
    Else ' No: Calculate normally
        column = ((x - mStartX) / gridWidth)
    End If

'Dim columnOffset As Single = row * halfWidth
'column = (x + columnOffset) / gridWidth

Me.LblRow.Text = "Row is: " & (row - 1).ToString()
Me.LblColumn.Text = "Column is: " & (column - 1).ToString()
End Sub

End Class
1 Like