Page 1 of 1

[VB.Net]Health Bar

Posted: Sun Mar 20, 2011 10:25 pm
by ChaosOverLord
ChaosOverLord's Tutorials Present....
A Health Bar In VB.Net
So your making a game and you need a code for a health bar?
Well your in luck today because we got the tutorial for you!

Add 2 buttons and 1 textbox to the form.
Name button 1 Heal
Name button 2 Attack

Now time for some coding!

Declare these when your inside the coding....

Public BMap As New Bitmap(Me.Width, Me.Height, Me.CreateGraphics)
Public GraphicsBuffer As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMap)
Public HpBarG As Integer = 100
Public HpBarR As Integer = 100

Form 1 Load code

drawhpbar()

Then make this code below the form load code

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(BMap, 0, 0)
End Sub

Next add this

Private Sub drawhpbar()
If HpBarG < 0 Then
HpBarG = 0
End If
If HpBarG > 100 Then
HpBarG = 100
End If
GraphicsBuffer.DrawRectangle(Pens.Black, New Rectangle(47 - 1, 5 - 1, 100 + 1, 15))
GraphicsBuffer.FillRectangle(Brushes.Red, New Rectangle(47, 5, HpBarR, 14))
GraphicsBuffer.FillRectangle(Brushes.Lime, New Rectangle(47, 5, HpBarG, 14))
Me.Invalidate(New Rectangle(47 - 1, 5 - 1, 100 + 1, 15))
End Sub

For button 1 code add this

HpBarG += textbox1.Text
drawhpbar()

then for button 2 code add this

HpBarG -= textbox1.Text
drawhpbar()

Thank you for using my 3rd of all my tutorials!

Re: [VB.Net]Health Bar

Posted: Tue Mar 22, 2011 5:11 pm
by Snyper345
Good tut im gonna try it now but next time make to use the code thingo coz people will have a cry , like me :D

Re: [VB.Net]Health Bar

Posted: Tue Mar 22, 2011 5:58 pm
by MrAksel
PLEASE use the CODE tags:
Code: Select all
[code]
[/code]
You tutorials is good, but use the CODE tags!! Please, im begging!

Re: [VB.Net]Health Bar

Posted: Fri Apr 01, 2011 8:36 pm
by zachman61
also how can we use two of these say for an Energy Bar

Re: [VB.Net]Health Bar

Posted: Sun Apr 03, 2011 10:41 am
by tedhead2
Looks nice, I've been trying to code a text based rpg for a while now, but the healthbar I made wasn't good at all...

Re: [VB.Net]Health Bar

Posted: Wed Apr 20, 2011 2:45 pm
by Bogoh67
wait i got lost in the code could you explain it to me i know what this means
Code: Select all
End If
GraphicsBuffer.DrawRectangle(Pens.Black, New Rectangle(47 - 1, 5 - 1, 100 + 1, 15))
GraphicsBuffer.FillRectangle(Brushes.Red, New Rectangle(47, 5, HpBarR, 14))
GraphicsBuffer.FillRectangle(Brushes.Lime, New Rectangle(47, 5, HpBarG, 14))
Me.Invalidate(New Rectangle(47 - 1, 5 - 1, 100 + 1, 15))
End Sub
but what does this "do"
Code: Select all
Public BMap As New Bitmap(Me.Width, Me.Height, Me.CreateGraphics)
Public GraphicsBuffer As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMap)
Public HpBarG As Integer = 100
Public HpBarR As Integer = 100

Re: [VB.Net]Health Bar

Posted: Sat Apr 23, 2011 4:22 am
by Jg99
Looks Awesome! ty for the tut. Have Fun Coding!

Re: [VB.Net]Health Bar

Posted: Sun Apr 24, 2011 4:56 am
by BlakPilar
Ehh... no.
Code: Select all
HpBarG += textbox1.Text
That's a very big no-no. If the user enters anything but a number, you're program will not work the way you want it to. Do this instead:
Code: Select all
Dim newHP As Integer = Val(TextBox1.Text)
HpBarG += newHP
Also, why did you make this cost 5 credits? It's not that hard of a "program" and you have the source listed there.