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.
VB.NET - Simple Game
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
oussama_1 39 Posting Whiz in Training
Mr.M 58 Future Programmers
kplcjl 17 Junior Poster
Mr.M 58 Future Programmers
oussama_1 39 Posting Whiz in Training
Mr.M 58 Future Programmers
oussama_1 39 Posting Whiz in Training
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.