VB.NET - Simple Game

oussama_1 3 Tallied Votes 937 Views Share

Here's a simple game code, open a new vb project and copy paste this code and hit run.
the game contains 3 levels, all you have to do is to shoot the smiley faces to earn money using mouse clicks, also there's a boss enemy on level 3.
I didn't have much time to add more levels to the game (actually i only tried to play it once :p), here's my point of this code "Just for fun" try to import some enemy characters(in a picturebox) and add more levels and add some sound effects to the shooting or when the enemies get hurt or whatever you would like to do.

Begginnerdev commented: Not bad! +9
Public Class Form1
    Dim rewards As Integer = 0
    Dim speed As Integer = 300
    Dim i As Integer = 12
    Dim ii As Integer
    Dim lives As Integer = 3
    Dim damage As Integer = 10
    Dim levelNum As Integer = 1
    Dim result As String = "True"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadGame()
        Call Levels(1)
    End Sub
    Private Const WM_NCLBUTTONDOWN = &HA1
    Private Const HTCAPTION = 2
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As Integer, ByVal wMsg As Integer, _
         ByVal wParam As Integer, ByVal lParam As String) As Integer
    Private Declare Sub ReleaseCapture Lib "user32" ()
    Public Sub loadGame()

        With Me
            .Size = New Size(291, 447)
            .BackColor = Color.SteelBlue
            .Cursor = Cursors.Cross
            .ForeColor = Color.White
            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
            .Text = "Smilies"
        End With
        Dim display As PictureBox
        display = New PictureBox
        display.Location = New Point(0, 0)
        display.Size = New Size(Me.Width, 40)
        display.BackColor = Color.Black
        Me.Controls.Add(display)
        AddHandler display.MouseDown, AddressOf MoveForm
        AddHandler display.MouseEnter, AddressOf CursorChange
        Dim close As New Button
        Me.Controls.Add(close)
        close.Text = "X"
        close.BackColor = Color.Black
        close.ForeColor = Color.White
        close.FlatStyle = FlatStyle.Flat
        close.FlatAppearance.BorderSize = 0
        close.Size = New Size(15, 20)
        close.Location = New Point(Me.Width - 25, 10)
        AddHandler close.Click, AddressOf close_click
        Dim levelText As New Label
        levelText.Name = "levelText"
        Me.Controls.Add(levelText)
        levelText.Location = New Point(8, 14)
        levelText.Size = New Size(150, 15)
        levelText.Text = String.Concat("Level 1  Lives ", lives.ToString, "  Money: ", rewards.ToString)
        levelText.ForeColor = Color.White
        levelText.BackColor = Color.Black
        display.SendToBack()
    End Sub
    Private Sub close_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub
    Private Sub MoveForm(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim lHwnd As Int32
        lHwnd = Me.Handle
        If lHwnd = 0 Then Exit Sub
        ReleaseCapture()
        SendMessage(lHwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
    End Sub
    Private Sub CursorChange(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Cursor = Cursors.Arrow
    End Sub
    Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
        Me.Cursor = Cursors.Cross
    End Sub
    Public Sub Levels(ByVal x As Integer)
        Select Case x
            Case 1
                ii = i
                rewards = 0
                speed = 200
                Enemy1(i)
                damage = 10
                lives = 3
                levelNum = 1
                Dim timer As New Timer
                timer.Start()
                AddHandler timer.Tick, AddressOf Timer_Tick
            Case 2
                ii = i
                speed = 100
                i += 14
                Enemy1(i)
                levelNum = 2
            Case 3
                ii = 1
                levelNum = 3
                speed = 50
                damage = 2
                Boss()
                Dim timer2 As New Timer
                timer2.Start()
                AddHandler timer2.Tick, AddressOf Timer2_Tick
        End Select
    End Sub
    Public Sub Enemy1(ByVal x As Integer)
        For b = 1 To x
            Dim blood(b) As PictureBox
            blood(b) = New PictureBox
            blood(b).Location = New Point(110, -60 - (b * 180))
            blood(b).Size = New Size(60, 5)
            blood(b).Name = "blood" + b.ToString
            blood(b).BackColor = Color.Red
            Dim enemy(b) As PictureBox
            enemy(b) = New PictureBox
            enemy(b).Location = New Point(110, -50 - (b * 180))
            enemy(b).Size = New Size(60, 60)
            enemy(b).Name = "enemy" + b.ToString
            AddHandler enemy(b).Click, AddressOf enemies_Shoot
            AddHandler enemy(b).Paint, AddressOf characters
            Me.Controls.Add(enemy(b))
            Me.Controls.Add(blood(b))
        Next
    End Sub
    Public Sub Boss()
        Dim Bblood As PictureBox
        Bblood = New PictureBox
        Bblood.Location = New Point(50, -150)
        Bblood.Size = New Size(160, 5)
        Bblood.Name = "Bblood"
        Bblood.BackColor = Color.Red
        Dim BBOSS As PictureBox
        BBOSS = New PictureBox
        BBOSS.Location = New Point(50, -140)
        BBOSS.Size = New Size(270, 190)
        BBOSS.Name = "BBOSS"
        AddHandler BBOSS.Click, AddressOf BBOSS_Shoot
        AddHandler BBOSS.Paint, AddressOf Bcharacters
        Me.Controls.Add(BBOSS)
        Me.Controls.Add(Bblood)

    End Sub
    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        sender.Interval = speed
        For v = 1 To i
            If IsNothing(Me.Controls("enemy" + v.ToString)) Then
            Else
                Me.Controls("enemy" + v.ToString).Location = New Point(Me.Controls("enemy" + v.ToString).Location.X, Me.Controls("enemy" + v.ToString).Location.Y + 20)
                Me.Controls("blood" + v.ToString).Location = New Point(Me.Controls("blood" + v.ToString).Location.X, Me.Controls("blood" + v.ToString).Location.Y + 20)
                If Me.Controls("enemy" + v.ToString).Location.Y > 420 Then
                    Me.Controls("blood" + v.ToString).Dispose()
                    Me.Controls("enemy" + v.ToString).Dispose()
                    ii -= 1
                    If lives < 1 Then
                        sender.stop()
                        For XZ = 1 To i
                            If IsNothing(Me.Controls("enemy" + XZ.ToString)) Then
                            Else
                                Me.Controls("blood" + XZ.ToString).Dispose()
                                Me.Controls("enemy" + XZ.ToString).Dispose()
                            End If
                        Next
                        sender.dispose()
                        Levels(1)
                    Else
                        lives -= 1
                    End If


                End If
            End If
        Next
        For Each control In Me.Controls
            If TypeOf (control) Is Label Then
                control.Text = String.Concat("Level ", levelNum.ToString, "  Lives ", lives.ToString, "  Money: ", rewards.ToString)
            End If
        Next
        If rewards = 80 Then
            If Not damage = 40 Then
                Dim Note As New Button
                Note.Name = "BtnNote"
                Me.Controls.Add(Note)
                Note.Text = "Upgrade" & vbNewLine & "Damage" & vbNewLine & "Cost: 80"
                Note.BackColor = Color.Red
                Note.ForeColor = Color.White
                Note.FlatStyle = FlatStyle.Flat
                Note.FlatAppearance.BorderSize = 0
                Note.Size = New Size(60, 60)
                Note.Location = New Point(15, 60)
                AddHandler Note.Click, AddressOf note_click
                AddHandler Note.MouseUp, AddressOf clear
            End If
        End If
        If ii < 0 Then
            If levelNum = 2 Then
                Levels(3)
                For VV = 1 To i
                    On Error Resume Next
                    Me.Controls("blood" + VV.ToString).Dispose()
                    On Error Resume Next
                    Me.Controls("enemy" + VV.ToString).Dispose()
                Next
                sender.dispose()
            End If
            If levelNum = 1 Then
                Levels(2)
            End If

        End If
    End Sub
    Private Sub note_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        damage = 40
        rewards -= 80

    End Sub
    Private Sub clear(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        For XX = 0 To 10
            On Error Resume Next
            Me.Controls("BtnNote").Dispose()
        Next
    End Sub
    Private Sub enemies_Shoot(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For v = 1 To i
            If sender.name.ToString.EndsWith(v.ToString) Then
                On Error Resume Next
                If Me.Controls("blood" + v.ToString).Width = 1 Then
                    Me.Controls("blood" + v.ToString).Dispose()
                    Me.Controls("enemy" + v.ToString).Dispose()
                    ii -= 1
                    rewards += 10
                ElseIf Me.Controls("blood" + v.ToString).Width <= 20 Then
                    Me.Controls("blood" + v.ToString).Size = New Size(1, 5)
                Else
                    Me.Controls("blood" + v.ToString).Size = New Size((Me.Controls("blood" + v.ToString).Width - damage), 5)
                End If
            End If
        Next
    End Sub
    Private Sub BBOSS_Shoot(ByVal sender As System.Object, ByVal e As System.EventArgs)
        On Error Resume Next
        If Me.Controls("Bblood").Width = 1 Then
            Me.Controls("BBOSS").Dispose()
            Me.Controls("Bblood").Dispose()
            result = "YOU WIN"
            ii -= 1
            rewards += 1000
        ElseIf Me.Controls("Bblood").Width <= 5 Then
            Me.Controls("Bblood").Size = New Size(1, 5)
        Else
            Me.Controls("Bblood").Size = New Size((Me.Controls("Bblood").Width - damage), 5)
        End If

    End Sub

    Private Sub characters(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        With e.Graphics
            If levelNum = 1 Then
                .FillEllipse(Brushes.Yellow, 0, 0, 56, 56)
            ElseIf levelNum = 2 Then
                .FillEllipse(Brushes.YellowGreen, 0, 0, 56, 56)
            End If
            .FillEllipse(Brushes.Black, 35, 14, 8, 8)
            .FillEllipse(Brushes.Black, 13, 14, 8, 8)
            .DrawArc(New Pen(Color.Black, 2), 10, 10, 35, 30, 20, 145)
        End With
    End Sub
    Private Sub Bcharacters(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        With e.Graphics
            .FillEllipse(Brushes.Green, 0, 0, 156, 156)
            .FillEllipse(Brushes.Black, 90, 30, 30, 30)
            .FillEllipse(Brushes.Black, 30, 30, 30, 30)
            .DrawArc(New Pen(Color.Black, 2), 10, 80, 135, 30, 20, 145)
        End With
    End Sub

    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs)
        sender.Interval = speed
        If IsNothing(Me.Controls("BBOSS")) Then
        Else
            Me.Controls("BBOSS").Location = New Point(Me.Controls("BBOSS").Location.X, Me.Controls("BBOSS").Location.Y + 1)
            Me.Controls("Bblood").Location = New Point(Me.Controls("Bblood").Location.X, Me.Controls("Bblood").Location.Y + 1)
            If Me.Controls("BBOSS").Location.Y > 420 Then
                Me.Controls("Bblood").Dispose()
                Me.Controls("BBOSS").Dispose()
                result = "GAME OVER"
                ii -= 1
                If lives < 1 Then
                    sender.stop()
                    If IsNothing(Me.Controls("BBOSS")) Then
                    Else
                        Me.Controls("Bblood").Dispose()
                        Me.Controls("BBOSS").Dispose()
                        result = "GAME OVER"
                    End If
                    sender.dispose()
                Else
                    lives -= 4
                End If
            End If
        End If
        For Each control In Me.Controls
            If TypeOf (control) Is Label Then
                If result = "True" Then
                    control.text = String.Concat("Level ", levelNum.ToString, "  Lives ", lives.ToString, "  Money: ", rewards.ToString)
                Else
                    control.Text = result
                End If

            End If
        Next
    End Sub
End Class
Begginnerdev 256 Junior Poster

First off, nice article.

Second off, Bug report. If you do not purchase and damage upgrades, but die on the boss. You will still have the option to buy the damage upgrade before starting the next round. This will allow you to buy (in my case) up to 6 upgrades. This subtracts from your credits as usual, but will allow for negative credits.

This is with no modifications to the code posted above.

You might want to add a couple lines in the Timer2_Tick method that will dispose the button and reset the credit count.

Me.Controls("btnNote").Dispose
Me.rewards = 0
oussama_1 39 Posting Whiz in Training

Thank you for your notes.

As a game concept:
i didn't work that much on that part but still everything is editable
(variables, level etc..) and yes you are right i should remove the
upgrade button when the second round starts :D.

As for the bug
now let's talk coding, the upgrade button should be clicked once
(money issue resolved) and then disposed by the clear methode

 Private Sub clear(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
For XX = 0 To 10
On Error Resume Next
Me.Controls("BtnNote").Dispose()
Next
End Sub

yet for some reason i cant dispose this button as easily, that's why i
put a loop on the dispose part because at the first couple times it
will throw this error "object is not set to an instance of a object"
(i handled it by on error resume next) it's like the control "BtnNote"
never existed! But eventually it'll get disposed(according to my run
test), but not according to you..so damn :p i thought i did handle
this bug :D.
again thanks for your notes.

Mr.M 58 Future Programmers

Looool you want me to kill my mouse button now ey ;-) nice game

kplcjl 17 Junior Poster

Line 302 has:
control.text = String.Concat("Level ", levelNum.ToString, " Lives ", lives.ToString, " Money: ", rewards.ToString)
This would do the same thing in a shorter format:
control.text = String.Format("Level {0} Lives {1} Money: {2}", levelNum, lives, rewards)

Mr.M 58 Future Programmers

I have a suggestion for maybe stage 4 but I don't know how I can do it because I'm not a master in game development and math. I was thinking it would be nice if you can add stage 4 and the and the current stage 3 be stage 4 and stage 3 be a smile face that will bounce randomly left and right while moving down, the bounce can be random so that a user won't be able to track the loop meaning to guess the next bounce position like if it had moved 3 times left it my move maybe 4 times to the right and 3 times to the left, center, left, far right as it makes it move down.

This was just an idea I had when I played this game but then I'm not a game master.

Thank you.

oussama_1 39 Posting Whiz in Training

Line 302 has: control.text = String.Concat("Level ", levelNum.ToString, " Lives ", lives.ToString, " Money: ", rewards.ToString) This would do the same thing in a shorter format: control.text = String.Format("Level {0} Lives {1} Money: {2}", levelNum, lives, rewards)

Yes you are right, and there's a lot of other ways to do it, but as long as it doesn't affect the performance or say the memory then there's no need to change it.

@ Mr.M

I have a suggestion...

This is a very simple code, i didn't use the math class or physics or any engine. As for your suggestion, i like the idea, you can add a level in the levels method and call it later, read through the code and you can figure it out easily (i know it a lot to read :D), but here's a sample on changing the position randomly on level 2 try it
add this to Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)

        If levelNum = 2 Then
            Dim Rand = New Random()
            Dim Pos = Rand.Next(-50, 51)
            Dim Smiley = Rand.Next(i + 1)
            If Not IsNothing(Me.Controls("enemy" + Smiley.ToString)) Then
                If Not Me.Controls("enemy" + Smiley.ToString).Location.X + Pos > 250 Then
                    If Not Me.Controls("enemy" + Smiley.ToString).Location.X + Pos < 20 Then
                        Me.Controls("enemy" + Smiley.ToString).Location = New Point(Me.Controls("enemy" + Smiley.ToString).Location.X + Pos, Me.Controls("enemy" + Smiley.ToString).Location.Y)
                        Me.Controls("blood" + Smiley.ToString).Location = New Point(Me.Controls("blood" + Smiley.ToString).Location.X + Pos, Me.Controls("blood" + Smiley.ToString).Location.Y)
                    End If
                End If
            End If

        End If
Mr.M 58 Future Programmers

It doesn't randomize it.

oussama_1 39 Posting Whiz in Training

@Mr.M Ok the full code is in the attached text file. what i did is that i added the above code(randomize) to line 207.
but i have to warn you, i gets pretty hard on level 2 :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.