2D Tile Thing

Posts 1–2 of 2 · Page 1 of 1
2D Tile Thing
Made a 2D tile engine thing only have 1 tile.... I have no clue if i did it right cause i haven't looked at another source for this. I had a real managed gameloop but i got the JIT debugger error and it got annoying so i took it out and decided just to use a timer. Also used GetAsyncKeyState which is kinda bad. If I did something wrong or you have suggestions please tell me.

Code:
Option Explicit On
Option Infer Off

Imports System.Drawing
Public Class Form1

    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As IntPtr) As IntPtr

    Private MapBMP As Bitmap
    Private MapWidth As Integer = 5
    Private MapHeight As Integer = 5
    Private Gridspace As Integer = 64

    Private CharSpeed As Integer = 10
    Private CharX As Integer = 0
    Private CharY As Integer = 0

    Private CameraWidth As Integer = 1000
    Private CameraHeight As Integer = 1000

    Private FPS As Integer = 0
    Private FPSMax As Integer = 2

    Public Username As String = "LucasHeer123"

    Private Function CropMap(ByVal BMP As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap

        Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
        Dim cropped As Bitmap = BMP.Clone(rect, BMP.PixelFormat)
        Return cropped

    End Function

    Private Function Division(ByVal Number As Integer, ByVal Div As Integer) As Integer
        Return Number / Div
    End Function

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        Dim CameraBMP As Bitmap
        Dim CameraX As Integer = CameraWidth + (CharX) - (CameraWidth / 2) + (Gridspace / 2)
        Dim CameraY As Integer = CameraHeight + (CharY) - (CameraHeight / 2) + (Gridspace / 2)

        CameraBMP = New Bitmap(CropMap(MapBMP, CameraX, CameraY, CameraWidth, CameraHeight))

        e.Graphics.Clear(Me.BackColor)
        e.Graphics.DrawImage(CameraBMP, 0, 0)

        Static PlayerFontDrawPoint As Integer = 10
        Static PlayerFontSizeFound As Boolean
        Static PlayerFontCenterOffset As Integer

        If (PlayerFontSizeFound = False) Then
            Dim Instance As Graphics = e.Graphics
            Dim text As String = Username
            Dim font As New Font("Arial", PlayerFontDrawPoint)
            Dim returnValue As SizeF
            returnValue = Instance.MeasureString(text, font)

            Do Until returnValue.Width <= Gridspace
                PlayerFontDrawPoint -= 1
                font = New Font("Arial", PlayerFontDrawPoint)
                returnValue = Instance.MeasureString(text, font)
            Loop

            PlayerFontSizeFound = True

            PlayerFontCenterOffset = (Gridspace - returnValue.Width) / 2
        End If

        Dim PlayerLocation As Point = New Point(Division(CameraWidth, 2) - Division(Gridspace, 2), Division(CameraHeight, 2) - Division(Gridspace, 2))
        e.Graphics.FillRectangle(Brushes.Red, PlayerLocation.X, PlayerLocation.Y, Gridspace, Gridspace)
        e.Graphics.DrawRectangle(Pens.Black, 0, 0, CameraWidth, CameraHeight)

        e.Graphics.DrawString(Username, New Font("Arial", PlayerFontDrawPoint), Brushes.Black, PlayerLocation.X + PlayerFontCenterOffset, PlayerLocation.Y)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()

        Me.DoubleBuffered = True

        MapBMP = New Bitmap((CameraWidth * 2) + MapWidth * Gridspace + 1, (CameraHeight * 2) + MapHeight * Gridspace + 1)

        Dim G As Graphics
        G = Graphics.FromImage(MapBMP)

        For X As Integer = 0 To MapWidth - 1
            For Y As Integer = 0 To MapHeight - 1
                G.DrawImage(My.Resources.Tile, (CameraWidth) + X * Gridspace, (CameraHeight) + Y * Gridspace)
            Next
        Next

        G.Dispose()

        MainTim.Interval = (1000 / FPSMax)
        MainTim.Start()
    End Sub

    Private Sub HotKeys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MainTim.Tick
        Dim K() As Boolean = {GetAsyncKeyState(Keys.A), GetAsyncKeyState(Keys.D), GetAsyncKeyState(Keys.W), GetAsyncKeyState(Keys.S)}

        If (K(0) = True) Then
            If Not (CharX - CharSpeed < 0) Then
                CharX -= CharSpeed
            Else
                CharX = 0
            End If
        End If

        If (K(1) = True) Then
            If Not (CharX + CharSpeed + Gridspace > MapWidth * Gridspace) Then
                CharX += CharSpeed
            Else
                CharX = (MapWidth * Gridspace) - Gridspace
            End If
        End If

        If (K(2) = True) Then
            If Not (CharY - CharSpeed < 0) Then
                CharY -= CharSpeed
            Else
                CharY = 0
            End If
        End If

        If (K(3) = True) Then
            If Not (CharY + CharSpeed + Gridspace > MapHeight * Gridspace) Then
                CharY += CharSpeed
            Else
                CharY = (MapHeight * Gridspace) - Gridspace
            End If
        End If

        Me.Text = Me.Text + 1
        Invalidate()
    End Sub
End Class
ummm. what is this? can you show a picture or something?
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us