วันศุกร์ที่ 16 มกราคม พ.ศ. 2552

Draw Maps In VB.NET With DrawImage Method





' VB.NET Games Source Code

Public Class Form1

Dim g_Mps As New KPKMaps
Dim g_ImgMps As System.Drawing.Image
Dim g_Graphics As System.Drawing.Graphics
Dim Rows As Integer = 1
Dim Cols As Integer = 1
Dim DestRect(,) As Rectangle
Dim SrcRect(,) As Rectangle

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
Me.Text = " Draw Maps In VBDotnet With DrawImage Method "
Me.BackColor = Color.Black
g_Graphics = Me.CreateGraphics
g_Mps.LoadMaps("D:\Maps\VBDotnet_Ball_Block_01.MAPS")
g_ImgMps = Image.FromFile("D:\Image Games\Ball_Block_Maps_02.Bmp")
Timer1.Interval = 1
Timer1.Enabled = True

ReDim DestRect(g_Mps.GetMapsWidth, g_Mps.GetMapsHeight)
ReDim SrcRect(g_Mps.GetMapsWidth, g_Mps.GetMapsHeight)

For Rows = 1 To g_Mps.GetMapsHeight
For Cols = 1 To g_Mps.GetMapsWidth
DestRect(Cols, Rows).X = g_Mps.GetTilePosX(Cols, Rows)
DestRect(Cols, Rows).Y = g_Mps.GetTilePosY(Cols, Rows)
DestRect(Cols, Rows).Width = g_Mps.GetTileWidth(Cols, Rows)
DestRect(Cols, Rows).Height = g_Mps.GetTileHeight(Cols, Rows)
SrcRect(Cols, Rows).X = g_Mps.GetTileX(Cols, Rows)
SrcRect(Cols, Rows).Y = g_Mps.GetTileY(Cols, Rows)
SrcRect(Cols, Rows).Width = g_Mps.GetTileWidth(Cols, Rows)
SrcRect(Cols, Rows).Height = g_Mps.GetTileHeight(Cols, Rows)
Next Cols
Next Rows
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
For Rows = 1 To g_Mps.GetMapsHeight
For Cols = 1 To g_Mps.GetMapsWidth
g_Graphics.DrawImage(g_ImgMps, DestRect(Cols, Rows), SrcRect(Cols, Rows), GraphicsUnit.Pixel)
Next Cols
Next Rows
End Sub
End Class

' KPKMaps.vb ( Class )

Public Class KPKMaps
Private Structure STR_MAPS
Dim TileX As Integer
Dim TileY As Integer
Dim TileWidth As Integer
Dim TileHeight As Integer
Dim TilePosX As Integer
Dim TilePosY As Integer
Dim TileMoveState As Byte
End Structure

Dim strMaps(,) As STR_MAPS
Dim MapsWidth As Integer
Dim MapsHeight As Integer
Dim TmpRows As Integer
Dim TmpCols As Integer

Public Sub LoadMaps(ByVal sFilename As String)
Dim nFileNum As Integer
nFileNum = FreeFile()
FileOpen(nFileNum, sFilename, OpenMode.Binary, OpenAccess.Read)
FileGet(nFileNum, MapsWidth)
FileGet(nFileNum, MapsHeight)

ReDim strMaps(MapsWidth + 1, MapsHeight + 1)
For TmpRows = 1 To MapsHeight
For TmpCols = 1 To MapsWidth
FileGet(nFileNum, strMaps(TmpCols, TmpRows).TileX)
FileGet(nFileNum, strMaps(TmpCols, TmpRows).TileY)
FileGet(nFileNum, strMaps(TmpCols, TmpRows).TileWidth)
FileGet(nFileNum, strMaps(TmpCols, TmpRows).TileHeight)
FileGet(nFileNum, strMaps(TmpCols, TmpRows).TilePosX)
FileGet(nFileNum, strMaps(TmpCols, TmpRows).TilePosY)
FileGet(nFileNum, strMaps(TmpCols, TmpRows).TileMoveState)
Next TmpCols
Next TmpRows

FileClose(nFileNum)
End Sub

Public Function GetMapsWidth() As Integer
GetMapsWidth = MapsWidth
End Function

Public Function GetMapsHeight() As Integer
GetMapsHeight = MapsHeight
End Function

Public Function GetTileX(Optional ByVal nCols As Integer = 1, Optional ByVal nRows As Integer = 1) As Integer
GetTileX = strMaps(nCols, nRows).TileX
End Function

Public Function GetTileY(ByVal nCols As Integer, ByVal nRows As Integer) As Integer
GetTileY = strMaps(nCols, nRows).TileY
End Function

Public Function GetTileWidth(ByVal nCols As Integer, ByVal nRows As Integer) As Integer
GetTileWidth = strMaps(nCols, nRows).TileWidth
End Function

Public Function GetTileHeight(ByVal nCols As Integer, ByVal nRows As Integer) As Integer
GetTileHeight = strMaps(nCols, nRows).TileHeight
End Function

Public Function GetTilePosX(ByVal nCols As Integer, ByVal nRows As Integer) As Integer
GetTilePosX = strMaps(nCols, nRows).TilePosX
End Function

Public Function GetTilePosY(ByVal nCols As Integer, ByVal nRows As Integer) As Integer
GetTilePosY = strMaps(nCols, nRows).TilePosY
End Function

Public Function GetTileMoveState(ByVal nCols As Integer, ByVal nRows As Integer) As Integer
GetTileMoveState = strMaps(nCols, nRows).TileMoveState
End Function
End Class

' Design VB.NET Source Code Games By Fernando Firaht

ไม่มีความคิดเห็น:

แสดงความคิดเห็น