Quantcast
Viewing all articles
Browse latest Browse all 56

[VB.Net] 2d Map Tile-Based Engine

Features
Allows you to render images in a panel to simulate a map in a game.

Drawbacks
It's pure visual basic.net, which isn't necessarily a drawback, but still vb.net is not know for it's outstanding graphics rendering. That's why most games made in vb.net use XNA or DirectX to handle the graphics.

Plans
Maybe make it multi-layered. With multi-layered engines you can create the bottom layer as just ground sprites and then the upper layer(s) would store structures(buildings, tree, etc..) and movables(creatures, npc's, etc..)

Notes
Thanks to Jacob Roman for the idea of showing only the tiles needing to be render on the screen(view thread here), I've created a pure vb.net 2d game engine. In the example, I load the map_tiles into a double buffered panel to try and improve the load time.

Full Project
Attachment 98637

Source Code
Form1 Class:
Code:

Option Strict On
Option Explicit On
Public Class Form1
    Private map(49, 49) As Map_Tile
    Private view_map(9, 9) As Map_Tile
    Private current_position As Point
    Private moving As Boolean = False

    Private Sub load_map(ByVal txt_file As String, ByVal delim As String)
        Dim str() As String = txt_file.Split({delim}, StringSplitOptions.RemoveEmptyEntries)

        For x As Integer = 0 To 49
            For y As Integer = 0 To 49
                Dim tile As New Map_Tile
                Dim i As Integer = CInt(str(y).Substring(x, 1))
                With tile
                    .Size = New Size(50, 50)
                    .Tag = i.ToString
                    Select Case i
                        Case 0
                            .Image = My.Resources.water
                            .IsPassable = False
                        Case 1
                            .Image = My.Resources.grass
                            .IsPassable = True
                        Case 2
                            .Image = My.Resources.dirt
                            .IsPassable = True
                    End Select
                End With

                map(x, y) = tile
            Next
        Next

    End Sub

    Private Sub move_map()
        Dim x, y As Integer
        x = current_position.X
        y = current_position.Y

        Double_Buffered_Panel1.Controls.Clear()

        Dim counter_x As Integer = 0
        Dim counter_y As Integer = 0
        For col As Integer = x - 4 To x + 5
            For row As Integer = y - 4 To y + 5

                view_map(counter_x, counter_y) = map(col, row)
                If col = x AndAlso row = y Then
                    view_map(counter_x, counter_y).IsCenter = True
                Else
                    view_map(counter_x, counter_y).IsCenter = False
                End If
                counter_y += 1
                If counter_y = 10 Then
                    counter_y = 0
                    counter_x += 1
                End If
            Next
        Next

        For col1 As Integer = 0 To 9
            For row1 As Integer = 0 To 9
                view_map(col1, row1).Location = New Point(col1 * 50, row1 * 50)

                Double_Buffered_Panel1.Controls.Add(view_map(col1, row1))
            Next
        Next
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        current_position = New Point(21, 8)

        Call load_map(My.Resources.map_file, Environment.NewLine)
        Call move_map()
    End Sub

    Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim x, y As Integer
        x = current_position.X
        y = current_position.Y

        Select Case e.KeyCode
            Case Keys.Left
                If map(x - 1, y).IsPassable AndAlso moving = False Then
                    current_position.X -= 1
                    moving = True
                    Call move_map()
                End If
            Case Keys.Right
                If map(x + 1, y).IsPassable AndAlso moving = False Then
                    current_position.X += 1
                    moving = True
                    Call move_map()
                End If
            Case Keys.Up
                If map(x, y - 1).IsPassable AndAlso moving = False Then
                    current_position.Y -= 1
                    moving = True
                    Call move_map()
                End If
            Case Keys.Down
                If map(x, y + 1).IsPassable AndAlso moving = False Then
                    current_position.Y += 1
                    moving = True
                    Call move_map()
                End If
        End Select
    End Sub

    Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        moving = False
    End Sub
End Class

Map_Tile:
Code:

Option Strict On
Option Explicit On
Public Class Map_Tile
    Inherits PictureBox

    Private ident As Integer
    Public Property ID() As Integer
        Get
            Return ident
        End Get
        Set(ByVal value As Integer)
            ident = value
        End Set
    End Property

    Private passable As Boolean
    Public Property IsPassable() As Boolean
        Get
            Return passable
        End Get
        Set(ByVal value As Boolean)
            passable = value
        End Set
    End Property

    Private center As Boolean
    Public Property IsCenter() As Boolean
        Get
            Return center
        End Get
        Set(ByVal value As Boolean)
            center = value
            If IsCenter Then
                Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
            Else
                Me.BorderStyle = Windows.Forms.BorderStyle.None
            End If
        End Set
    End Property

End Class

Double_Buffered_Panel:
Code:

Option Strict On
Option Explicit On
Public Class Double_Buffered_Panel
    Inherits Panel

    Public Sub New()
        Me.DoubleBuffered = True

        SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)

        UpdateStyles()
    End Sub
End Class

The text file named My.Resources.map_file
Quote:

00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000001111111111111111111111111111100000000000
00000000001111111111111111111111111111100000000000
00000000001111111111111111111111111111100000000000
00000000001111111111222221111111111111100000000000
00000000001111111111222221111111111111100000000000
00000000001111111111222221111111111111100000000000
00000000001111111111222221111111111111100000000000
00000000001111111111222221111111111111100000000000
00000000001111111111222222222211111111100000000000
00000000001111111111222222222211111111100000000000
00000000001111111111222222222211111111100000000000
00000000001111111111222222222211111111100000000000
00000000001111111111222222222211111111100000000000
00000000001111111111111112222211111111100000000000
00000000001111111111111112222211111111100000000000
00000000001111111111111112222211111111100000000000
00000000001111111111111112222211111111100000000000
00000000001111111111111112222211111111100000000000
00000000001111111111111112222211111111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111111111122222111100000000000
00000000001111111111111111111122222111100000000000
00000000001111111111111111111122222111100000000000
00000000001111111111111111111122222111100000000000
00000000001111111111111111111122222111100000000000
00000000001111111111111111111122222111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111112222222222111100000000000
00000000001111111111111111111111111111100000000000
00000000001111111111111111111111111111100000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
Attached Files

Viewing all articles
Browse latest Browse all 56

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>