Thread: 2D Tile Thing

Results 1 to 2 of 2
  1. #1
    lucasheer1's Avatar
    Join Date
    Jul 2012
    Gender
    male
    Posts
    72
    Reputation
    10
    Thanks
    498

    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

  2. The Following User Says Thank You to lucasheer1 For This Useful Post:

    Nine11 (02-12-2017)

  3. #2
    Sazor98's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    X:13 Y:3 Z:7
    Posts
    154
    Reputation
    23
    Thanks
    64
    ummm. what is this? can you show a picture or something?

Similar Threads

  1. Blue tile thing
    By Whisper in forum Showroom
    Replies: 3
    Last Post: 11-26-2009, 03:08 PM
  2. Most illegal thing ever done?
    By arunforce in forum General
    Replies: 175
    Last Post: 04-25-2006, 09:09 AM
  3. worst thing that happend on gunz
    By [YouWillDie] in forum Gunz General
    Replies: 16
    Last Post: 02-09-2006, 10:59 PM
  4. Coolest thing ever (need headphones)
    By AthlaS in forum General
    Replies: 7
    Last Post: 01-28-2006, 09:39 AM
  5. Warrock spam thing
    By FlyingBrick in forum WarRock - International Hacks
    Replies: 0
    Last Post: 01-14-2006, 10:02 AM