Arcade Prehacks

Results 1 to 2 of 2
  1. #1
    Senior Member
    Join Date
    Jun 2008
    Location
    Dra'kar
    Posts
    1,263

    Basic Game Making in VB2010

    REQUIREMENTS
    Visual Basic 2010


    INDEX
    BASIC VB 2010 KNOWLEDGE
    1. How to make a form Hide.
    2. How to make a form show priority, and form behind not able to be clicked
    3. How to use the If...Then Statement
    4. How to use the Else Statement
    5. How to use the Select Case Statement
    6. Random Numbers
    7. Using Progress Bars
    8. Using Modules for global functions
    9. Using Classes for global variables

    BASIC GAME KNOWLEDGE
    10. How to make Secret Codes/Cheat Codes
    11. Inventory and Equipment
    12. Levels and Experience

    Tutorial;
    1. How to make form Hide.
    Say you want to click a button on form1 and have form2 appear, and then have form1 disappear. To do this you will have to use the .Hide() function.

    First, double click your button on form one and that the following code before your form2.show line:
    Code:
    Me.Hide()
    Example:
    Code:
        Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
            Me.Hide()
            form2.Show()
        End Sub
    2. How to make a form show priority, and form behind not able to be clicked
    Say you want to click a button on form1 and have form2 appear, but you want form1 to stay visible but not clickable while form2 is still visible. This is where the .ShowDialog(*?*) command comes in to play.
    *?*: This is where you enter the form that you want to be unclickable, in this example that would be form1.
    Double click your button on form1 and enter the following code:
    Code:
    Form2.ShowDialog(Me)
    Example:
    Code:
    Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
            form2.ShowDialog(Me)
        End Sub
    3. How to use the If...Then Statement
    The If..Then statement is the computers way of logically asking a question. Using If...Then statement comes in very handy for testing one or two conditions.
    Code:
    If *condition* Then
        *statement*
    End If
    Example:
    Code:
    If form2.visible = true Then
        form1.button1.visible = false
    End If
    4. How to use the Else Statement
    The Else state is what happens when the original if statement returns false.
    Code:
    If *condition* Then
        *statement*
    Else
        *statement*
    End If
    Example:
    Code:
    If form2.visible = true Then
        form1.button1.visible = false
    Else
        form1.button2.visible = false
    End If
    5. How to use the Select Case Statement
    The Select Case Statement gets a variable and chooses which 'path' to take depending on the contents of the variable.
    Code:
    Dim int As Integer = 1
    Select Case (int)
    case 1
        *statement*
    case 2
        *statement*
    End Select
    Example:
    Code:
    Dim int As Integer = 1
    Select Case (int)
    case 1
        button1.visible = false
    case 2
        button2.visible = false
    End Select
    6. Getting Random Numbers
    There are ways of having the computer randomly generate a number
    Code:
    Dim int As Integer
    Dim ran As System.Random = New System.Random
    int = ran.Next(*begNum*, *endNum*)
    Example:
    Code:
    Dim int As Integer
    Dim ran As System.Random = New System.Random
    int = ran.Next(1, 2500)
    7. Using Progress Bars
    Progress bars are used to track either the growing progress of something, or decaying progress of something.
    Code:
    If ProgressBar1.Value + *condition* > ProgressBar1.Maximum
    ProgressBar1.Value = ProgressBar1.Maximum
    Else
    ProgressBar1.Value = ProgressBar1.Value + *condition*
    End If [/code]
    Example:
    Code:
    If ProgressBar1.Value - enemyHit > ProgressBar1.Minimum
    ProgressBar1.Value = ProgressBar1.Minimum
    Else
    ProgressBar1.Value = ProgressBar1.Value - enemyHit
    End If[/code]

    8. Using Modules for global functions
    Modules are a lot like classes, the only difference is that a module does not need to be Imported or referenced to in order to be used.
    So first, in the top left corner click the 'Add New Item' button. Then select module and name it whatever. In this new blank code page that appears you can define all your functions/subs or properties.
    Code:
    Module Module1
        Public Function getCurrentGold()
            Dim gold As Integer
            gold = _SETTINGS.cGold
            Return gold
        End Function
    End Module
    Then when using you can just call the function and it will be performed:
    Code:
    label1.text = getCurrentGold()
    9. Using Class for global Variables
    Classes are a lot like Modules only the class has to imported in order to be used, or referenced to. So first, in the top left corner click the 'Add New Item' button. Then select class and name it whatever. In this new blank code page that appears you can define all your functions/subs or properties.

    Code:
    Public Class _SETTINGS
        Public Shared Property Name As String
        Public Shared Property Level As Integer
        Public Shared Property Gold As Integer 
        Public Shared Property hasSave As Boolean
    And to use them:
    Code:
    imports Program._SETTINGS
    
    Public Class Form1
        Setting1 = 1
    OR
    Code:
    Public Class Form1
        _SETTINGS.Setting1 = 1
    11. Inventory and Equipment
    It seems to me that most people get stuck when trying to implement a good and efficient Inventory/System. So here I'll explain a way to make an efficient system for all your needs.
    First create a class and name it whatever. Then in the first few lines of that class we are going to define two dictionarys for equipped equipment and items in your inventory:
    Code:
    Public Clas HERO
    Dim equipment as New Dictionary(Of RpgEquipmentSlot, RpgEquipment)
    Dim inventory as New Dictionary(Of RpgItem, Integer)
    We have not yet defined RpgEquipmentSlot, RpgEquipment or RpgItem so that will give you errors for the moment. But we will define them next. So next we will need to create one enum, and two classes(inside this class, not a new item)

    Code:
    public enum RpgEquipmentSlot
        Head 'Head Slot
        Body 'Chest Slot
        Legs 'Leg Slot
        Boots 'Feet Slot
        'Place all character equipment slots here
    end enum
    
        Public Class RpgItem
            Public Property Name As String 'Name of Item
            Public Property Stackable As Boolean 'Is the item stackable?
            Public Property Resource As String 'Name of image that corresponds to item
            Public Property Price As Integer 'Price of the item in game
        End Class
    
        Public Class RpgEquipment
            Inherits RpgItem
    
            Public Property Slot As RpgEquipmentSlot 'What slot does it go in, head - hat, body - shirt, legs - pants
        End Class
    Now we are going to need two more classes(not new items) for our actions, equip, unequip, additem, removeitem. So we will add the following:
    Code:
    Public Class RpgCharacter
        Public Property Equipment As New Dictionary(Of RpgEquipmentSlot, RpgEquipment)
        Public Property Inventory As New RpgInventory
    
        Public Function Equip(item As RpgEquipment) As Boolean
            If Inventory.ItemCount(item) = 0 Then Return False
            Inventory.RemoveItem(item)
            If Equipment.ContainsKey(item.Slot) Then
                Dim currentEquipment As RpgEquipment = Equipment(item.Slot)
                If Not Inventory.AddItem(currentEquipment) Then
                    'Inventory is full and previous equipment could not be added to inventory...
                    'drop or discard or overflow as required
                End If
            End If
            Equipment(item.Slot) = item
            Return True
        End Function
    End Class
    
    Public Class RpgInventory
        Public Property InventorySlots As Integer = 40 'Max number of slots in your inventory
    
        Public Function AddItem(item As RpgItem) As Boolean
            If inventory.ContainsKey(item) AndAlso item.Stackable Then 'Checks if the inventory already has the item, if so then if it is stackable
                inventory(item) += 1
                Return True
            End If
            If inventory.Count < InventorySlots Then
                inventory(item) = 1
                Return True
            End If
            Return False
        End Function
    
        Public ReadOnly Property InventoryCount As Integer
            Get
                Return inventory.Count
            End Get
        End Property
    
        Public Function ItemCount(item As RpgItem) As Integer
            If inventory.ContainsKey(item) Then
                Return inventory(item)
            End If
            Return 0
        End Function
    
        Public Function RemoveItem(item As RpgItem) As Boolean
            If inventory.ContainsKey(item) Then
                If inventory(item) > 1 Then
                    inventory(item) -= 1
                    Return True
                Else
                    inventory.Remove(item)
                    Return True
                End If
            End If
            Return False
        End Function
    End Class
    Now no game can have this without having items so we are now going to define all of our items in another class(yes a new item). So click add new item and add another class, name it _ITEMS.
    From here we can define a public property then the item name 'As' then whether it is a item or equipable (RpgItem, RpgEquipment), and then edit its properties by item. You will also need to Import your Hero class, or else VB wont recognize the item type.
    So each item should look something like this:
    Code:
    Imports ArenaFightersII._HERO
    
    Public Class ITEMS
    
        Public Shared blueWizClothBody As New RpgEquipment With {.Name = "Wizard's Robe", .Slot = RpgEquipmentSlot.Body, .Resource = "clothBody", .Price = 55}
        Public Shared blueWizClothLegs As New RpgEquipment With {.Name = "Wizard's Bottom", .Slot = RpgEquipmentSlot.Legs, .Resource = "clothLegs", .Price = 45}
        Public Shared blueWizClothBoots As New RpgEquipment With {.Name = "Wizard's Shoes", .Slot = RpgEquipmentSlot.Feet, .Resource = "clothBoots", .Price = 15}
    Now as a quick example when you click button1 it will add a hat to your inventory then when clicking button2 it will equip it.
    Button1
    Code:
    dim item as RpgItem
    item = _ITEMS.hat
    RpgInventory.addItem(item)
    Button2
    Code:
    dim item as RpgItem
    item = _ITEMS.hat
    RpgCharacter.Equip(item)
    12. Levels and Experience
    All games are fun when you have a goal to work towards, like leveling up and gaining experience. Implementing levels and experience in you game is actually very easy. Inside a module we are going to create a function that checks if we have 'levelup' if so it sets our level +1 and resets our experience

    So in a module write these lines as our level up function, be sure to import our settings class if you are using one.
    Code:
    public function levelUp()
    dim newLevel as integer
    if _SETTINGS.cExp > _SETTINGS.expTillLevel then
        newLevel = _SETTINGS.cLevel + 1
        _SETTINGS.cExp = 0
    return newLevel
    end function
    now for gaining experience: Were going to use a button as our 'gain event' so it would look something like this:
    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        _SETTINGS.cExp = _SETTINGS.cExp + Exp 'Exp is however much experience you want the player to gain
        levelUp()
    End Sub
    If you want multiple levels with multiple amount of expTillLevel then on the game load you can create a new dictionary and add every level and value then check it in the levelUp function
    for example:
    on Game load
    Code:
    dim lvlExp as new Dictionary(of Integer, Integer)
    lvlExp.Add(1, 50)
    lvlExp.Add(2, 100)
    lvlExp.Add(3, 150)
    lvlExp.Add(4, 200)
    ' etc etc etc
    then the button click would look like:
    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        _SETTINGS.cExp = _SETTINGS.cExp + Exp 'Exp is however much experience you want the player to gain
        levelUp()
    End Sub
    and the levelUp would look like
    Code:
        Public Function LevelUp()
            Dim newLevel As Integer
            If _SETTINGS.cExp > _SETTINGS.ExpTillLevel Then
                newLevel = _SETTINGS.cLevel + 1
                _SETTINGS.cExp = 0
                If lvlExp.containskey(newLevel) Then
                    _SETTINGS.ExpTillLevel = lvlExp.TryGetValue(newLevel)
                Else
                    _SETTINGS.ExpTillLevel = -1 'Final level fail safe no more levels to be gained
                End If
            End If
            Return newLevel
        End Function

    I hope this helps some people out there just beginning to make games using visual basic. If you have any questions please leave a reply and I will try to help you. If you are trying to learn the do not copy and paste because you will learn nothing.

    --Magnite7


    FOR THOSE INTERESTED IN DOWNLOADING THE AF2 OPEN SOURCE< YOU CAN DOWNLOAD IT HERE:
    Code:
    http://www.mediafire.com/file/8ggtozx3ff8y3vz/Arena_Fighters_2_Dark_Ages.rar
    This open source has a lot of what s included in this tutorial implemented in, great for education purposes. Although this is an ALPHA version of the game so not everything has been coded, and not everything works. Have fun!
    Last edited by Magnite7; 08-26-2012 at 09:52 PM. Reason: Added AF2 open source link

  2. #2
    Senior Member
    Join Date
    Jan 2010
    Location
    TRMI
    Posts
    4,616
    great tut, i can reallyread the whole thing now, but i skimmed it, good job

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •