Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   Help With Lifebar (http://www.programmingforums.org/showthread.php?t=4133)

Fergutron May 27th, 2005 3:39 PM

Help With Lifebar
 
Hey, whats up everybody? I'm doing a project in programming class and am having trouble with a Lifebar (like in video games). It is very simple, just three pictures of hearts. Every time the label which displays "Computer Wins" is visible, I want a heart to be taken away. How would I go about doing this since I am new?

Thanks

Ooble May 27th, 2005 5:26 PM

If these are just Image controls, it's quite easy:
:

imgHeart2.Visible = False

Rory May 27th, 2005 5:57 PM

If you've got more than three hearts though you'd probably want to use a control array at the least...

Fergutron May 27th, 2005 6:13 PM

I understand the .visible = false thing, but the thing is I want heart1 to disappear when the losing message appears, then when it appears again, I want the next one disappear and so on.

Ooble May 27th, 2005 7:36 PM

Control arrays break the Form Designer in .NET, unfortunately.

Fergutron May 27th, 2005 8:57 PM

what do i do then?

Wraith Daquell May 27th, 2005 9:04 PM

Are there only going to be three hearts, or will this number possibly change?

Assuming you only have one picture box (I'll call it imgHearts),
you could have an integer variable:

Dim NumHeartsLeft as Integer = 3

Then, you could dive into GDI+ and have it draw the hearts on that picturebox:

:

Public Sub DrawHearts()
        Const HeartPath As String = "C:\Heart.bmp"  'Path to the heart image
        Const HeartWidth As Integer = 15 'Width, in pixels, of the heart image
        Dim I As Integer 'Traditional loop variable

        For I = 1 To NumHeartsLeft
            imgHearts.CreateGraphics.DrawImageUnscaled( _
              New Bitmap(HeartPath), I * HeartWidth, 0)
        Next I
End Sub


This draws as many hearts as are in NumHeartsLeft.
You should set HeartPath to the location of the Heart image, and HeartWidth to the actual width of the Heart image.
Whenever your player gets hurt, when you show the Label, you should subtract 1 from NumHeartsLeft and call DrawHearts:

NumHeartsLeft -= 1
DrawHearts()


Some suggestions for the above:
  • Preload the Heart image to improve speed
  • Put the DrawHearts() sub into the imgHearts' Paint event to prevent the hearts being erased when the form is minimized

Hope this helped at all. Good luck!

Fergutron May 28th, 2005 2:34 PM

Thanks a lot. But this is a very simple rock paper scissors program. Every time the computer wins, a label displays saying "Computer Wins" and every time that happens, I want a heart's visibility to be turned off. Once all hearts are off I'm going to have it reset a textbox that adds up the total rounds survived back to zero (I already have the textbox working). I don't know if what you helped me with will help that.

thanks

Wraith Daquell May 28th, 2005 3:44 PM

If it is very simple, then you could just create three Image controls. I'll call them img1, img2, and img3. If you copy the following sub into your program, and call it whenever the label is shown, it will either make an image invisible, or if all are invisible, it will make all three visible again (for restarting and all):

:

Dim NumHearts As Integer = 3
    Public Sub ManageHearts()
        Select Case NumHearts
            Case 3
                img3.Visible = False
            Case 2
                img2.Visible = False
            Case 1
                img1.Visible = False
            Case 0
                img3.Visible = True
                img2.Visible = True
                img1.Visible = True
                NumHearts = 4
        End Select
        NumHearts -= 1
    End Sub


The reason NumHearts is set to 4 instead of 3 in 'Case 0' is because it will have 1 subtracted it from it in the next line, making it 3 and ready to start over again.

Good Luck!

Fergutron May 28th, 2005 4:05 PM

Lifbar *SOLVED*
 
Thanks a lot for being so helpful. I'm sure this will work.


All times are GMT -5. The time now is 12:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC