Mario Type game VB.net

Hello Friends ! today i will teach you how to make a platform game ...

This i bit advanced so i won't teach each thing . Trust me you can do it easily

First Add This Code To Windows Form outside subs

You need 3 timers - 1 Player(picturebox) some platforms(picturebox) labeled "G"




#Region "Imported Functions"
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short ' For Custom Key Down.
    Public Const VK_A = &H41 ' A
    Public Const VK_Z = &H54 ' Z
    Public Const VK_LSHIFT = 37 ' &HA0
    Public Const VK_RSHIFT = 39 ' &HA1
    Public Const VK_LKCONTROL = &HA2
    Public Const VK_RKCONTROL = &HA3
    Public Const VK_RIGHT = &H27
    Public Const VK_LEFT = &H25
    Public Const VK_UP = &H26
    Public Const VK_DOWN = &H28
    Public Const VK_SPACE = 32   '&H20
    Public Const VK_ESC = &H1B
#Region "GDK"
    Private Function GetRegion(ByVal bm As Bitmap, ByVal _
    bg_color As Color) As Region
        Dim new_region As New Region
        new_region.MakeEmpty()

        Dim rect As New Rectangle
        Dim in_image As Boolean = False
        Dim X As Integer

        For Y As Integer = 0 To bm.Height - 1
            X = 0
            Do While (X < bm.Width)
                If Not in_image Then
                    If Not bm.GetPixel(X, Y).Equals(bg_color) _
                        Then
                        in_image = True
                        rect.X = X
                        rect.Y = Y
                        rect.Height = 1
                    End If
                ElseIf bm.GetPixel(X, Y).Equals(bg_color) Then
                    in_image = False
                    rect.Width = (X - rect.X)
                    new_region.Union(rect)
                End If
                X = (X + 1)
            Loop

            ' Add the final piece if necessary.
            If in_image Then
                in_image = False
                rect.Width = (bm.Width - rect.X)
                new_region.Union(rect)
            End If
        Next Y

        Return new_region
    End Function
    Function SetRegion(ByVal Img As PictureBox) As Region
        Dim B As Bitmap = Img.Image
        Return GetRegion(B, B.GetPixel(0, 0))
    End Function

    Dim bm_source, bm_dest As Bitmap
    Function ResizeImage(ByVal Img As Image, ByVal H As Integer, ByVal W As Integer) As Image
        Try

            Dim scale_factor As Integer
            Dim img1 As New PictureBox

            scale_factor = Integer.Parse(10)

            bm_source = New Bitmap(Img)
            bm_dest = New Bitmap( _
                CInt(W), _
                CInt(H))

            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

            gr_dest.DrawImage(bm_source, 0, 0, W, H)
            ' tempCnt = True



        Catch ex As Exception

        End Try
        Return (bm_dest)
    End Function
#End Region
#End Region

 




This Functions will help in cropping and other things .
 
Now Detect if player is standing on Player_Move events..
 Define a Boolean onGround and add the following code



Private Sub Player_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Player.Move
        onGround = Nothing
        For Each n As PictureBox In Me.Controls
            If Player.Bounds.IntersectsWith(n.Bounds) AndAlso n.Tag = "G" Then
                onGround = True
            End If

        Next
        If Not onGround = True Then
            onGround = False
        End If
    End Sub

 Now for timer to detect and work as gravity

        If Jump.Enabled = False Then ' timer which makes player jump
            If onGround = False Then
                Player.Top += 10
            End If
        End If


 MAIN movement and main functions in another timer  


 

Player.Region = SetRegion(Player)

        If GetAsyncKeyState(VK_RIGHT) Then
            If PictureBox5.Left >= 577 Then

                For Each C As PictureBox In Me.Controls
                    If C.Tag = "G" Then
                        C.Left -= 10
                        Player.Left -= 1
                        Player.Left += 1
                    End If
                Next
            Else
                Player.Left += 10
            End If
        ElseIf GetAsyncKeyState(VK_LEFT) Then
            If Not PictureBox7.Left >= 10 Then

                For Each C As PictureBox In Me.Controls
                    If C.Tag = "G" Then
                        C.Left += 10
                        Player.Left -= 1
                        Player.Left += 1
                    End If
                Next
            Else : Player.Left -= 1
            End If

        End If
        If GetAsyncKeyState(VK_UP) AndAlso onGround Then
            Jump.Enabled = True
        End If


 Jump Timer :-


Dim i As Integer
    Private Sub Jump_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Jump.Tick
        i += 1
        Player.Top -= 10
        If i = 10 Then
            i = 0
            Jump.Enabled = False
        End If
    End Sub


 Congratulations You have done it

 



Source Code Coming soon 

0 comments:

Note: only a member of this blog may post a comment.

Translate