Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 85,023 VB.NET Programmers. There are 1,305 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a VB.NET Expert
Powered by LivePerson.com

Register to Make This Box Go Away!


Health Bar Creation

A fully-functional class demonstrating how you can show health bars using VB.NET. Comes complete with drawing, setting the health values and getting the current health value.

Submitted By: RodgerB
Actions:
Rating:
Views: 149

Language: VB.NET

Last Modified: March 26, 2008
Instructions:
1) Create a new class file, call it HPBar.vb
2) Replace all the text in your newly created file with the text in the snippet below.

Snippet


  1. ''' <summary>
  2. ''' A class to create health bars on controls.
  3. ''' </summary>
  4. ''' <remarks></remarks>
  5. Public Class HPBar
  6.     Private FullHP As Single
  7.     Private Health As Single
  8.     Private BarSize As Rectangle
  9.  
  10.     ''' <summary>
  11.     ''' Occurs when there is no longer any HP left.
  12.     ''' </summary>
  13.     ''' <remarks></remarks>
  14.     Public Event OnHPDepleted()
  15.  
  16.     ''' <summary>
  17.     ''' Occurs when the HP is full.
  18.     ''' </summary>
  19.     ''' <remarks></remarks>
  20.     Public Event OnHPFull()
  21.  
  22.     ''' <summary>
  23.     ''' Occurs when the HP has been decremented
  24.     ''' </summary>
  25.     ''' <remarks></remarks>
  26.     Public Event OnHPDecrement()
  27.  
  28.     ''' <summary>
  29.     ''' Occurs when the HP has been incremented
  30.     ''' </summary>
  31.     ''' <remarks></remarks>
  32.     Public Event OnHPIncrement()
  33.  
  34.     ''' <summary>
  35.     ''' Occurs when the HP has changed
  36.     ''' </summary>
  37.     ''' <remarks></remarks>
  38.     Public Event OnHPChange()
  39.  
  40.     ''' <summary>
  41.     ''' The Constructor of the Health Bar class.
  42.     ''' </summary>
  43.     ''' <param name="MaxHP">The maximum HP value</param>
  44.     ''' <param name="BarRect">The rectangle of the HP bar</param>
  45.     ''' <remarks></remarks>
  46.     Public Sub New(ByVal MaxHP As Integer, ByVal BarRect As Rectangle)
  47.         FullHP = MaxHP
  48.         Health = MaxHP
  49.         BarSize = BarRect
  50.     End Sub
  51.  
  52.     ''' <summary>
  53.     ''' The subroutine to draw the health bar.
  54.     ''' </summary>
  55.     ''' <param name="GraphicsToUse">The Graphics object to draw to</param>
  56.     ''' <remarks>Use e.Graphics in the Form's paint event for example :)</remarks>
  57.     Public Sub DrawBar(ByRef GraphicsToUse As Graphics)
  58.         ' The formula used to set the width of the Health bar's width
  59.         ' is (in pseudocode): maximum hp bar width x (current health / maximum hp value)
  60.         GraphicsToUse.FillRectangle(Brushes.Red, BarSize.X, BarSize.Y, _
  61.                                     Convert.ToInt32(BarSize.Width * (Health / FullHP)), _
  62.                                     BarSize.Height)
  63.     End Sub
  64.  
  65.     ''' <summary>
  66.     ''' Set the health to a specified value
  67.     ''' </summary>
  68.     ''' <param name="intHealth">The integer value to set the health to</param>
  69.     ''' <remarks></remarks>
  70.     Public Sub SetHealth(ByVal intHealth As Single)
  71.         If intHealth > FullHP Or intHealth < 0 Then
  72.             Throw New Exception("Health value must be between 0-" + FullHP.ToString() + ".")
  73.         End If
  74.  
  75.         Dim prevHealth As Single = Health
  76.         Health = intHealth
  77.  
  78.         RaiseEvent OnHPChange()
  79.         If prevHealth > Health Then RaiseEvent OnHPDecrement()
  80.         If prevHealth < Health Then RaiseEvent OnHPIncrement()
  81.         If Health <= 0 Then RaiseEvent OnHPDepleted()
  82.         If Health >= FullHP Then RaiseEvent OnHPFull()
  83.     End Sub
  84.  
  85.     ''' <summary>
  86.     ''' Get the current health of the health bar
  87.     ''' </summary>
  88.     ''' <returns>The health value</returns>
  89.     ''' <remarks></remarks>
  90.     Public Function GetHealth() As Single
  91.         Return Health
  92.     End Function
  93.  
  94. End Class

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month