Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tower Defense HP bar help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
childplay534




PostPosted: Tue Aug 16, 2011 11:05 am   Post subject: Tower Defense HP bar help

What is it you are trying to achieve?
i am trying to get a hp bar to stay on top of my mob in a tower defense game


What is the problem you are having?
the hp bar does not have the same values as the hp


Describe what you have tried to solve this problem
i have been working at it for a couple of hours now, and i cant really explain what iv done so far


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:

 setscreen ("offscreenonly")

    var x,y,x1,y1,r: int
    x :=200
    y :=300
    x1 := 0
    y1 := -2
    r :=10
    var hp,dmg,thpx: int
    var dmgx,dmgy :int
    hp :=100
    randint (dmg, 5, 10)
    var hx,hy,hxx,hyy,hpx,co,hpc,mhp,mhpx :int
    var hxp,hxpp:int
    var thpxn :int
   
    loop
    cls
    dmgx :=200
    dmgy :=250   
   
    co :=hp div 10
    hx :=x-10
    hy :=y+25
    hxx :=x+10
    hyy :=y+20
   
    hxp :=x-50
    hxpp :=x+50
   
    hpx := (co)+x
    hpc :=hp+x
    mhp :=100
    mhpx :=(mhp div 10)+x
    thpx := hp div 2 + x
    thpxn := thpx - x
   
    drawfilloval (x,y,r,r,red)                  %main thing
    drawfilloval (x,y,1,1,black)                %to know where x and y are
    drawbox (hx,hy,hxx,hyy, black)              %hp box
    drawfillbox (hx, hy, hpx,hyy,green)         %current hp
   
   
    drawbox (hxp,hy+10,hxpp,hyy+10,black)        %unshrink hp
    drawfillbox (hxp,hy+10,thpx,hyy+10,green)
   
    drawfilloval (dmgx,dmgy,5,5,blue)           %dmg mark
    drawoval (dmgx,dmgy,20,20,black)
     
   
   
    x+=x1
    y+=y1
   
        if y<250+20 and y>250-20 then
        drawfilloval (dmgx,dmgy,5,5,red)
        hp-=dmg
        end if
        put "hp+x ",hpc                         %hp values
        put "hp ",hp
        put thpxn
       
       
         drawbox (150,0,mhp+150,10,black)       %acutal hp
         drawfillbox (150,0,hp+150,10,green)
         
                     
     
    exit when hasch
    delay (50)
    View.Update
    cls
    end loop



Please specify what version of Turing you are using
the newest one
Sponsor
Sponsor
Sponsor
sponsor
TWizard




PostPosted: Tue Aug 16, 2011 1:28 pm   Post subject: RE:Tower Defense HP bar help

Hmm, What you might want to do is when the enemy hp < 0 then hp = 0 and don't draw the enemy anymore, also if you wanted the hp bar to be the same as the hp have them both set at 100 and let them take the same amount of damage. at least that's what i'm getting from this.

I find this hard to follow, and your variables are shorten up to much to read, you should rename them for future notation for example: enemyY or enemyHP, castleHP ect, that way its easier to follow what variable may need some fixing.

If i did not cover what you needed please provide more information.
Zren




PostPosted: Tue Aug 16, 2011 2:43 pm   Post subject: RE:Tower Defense HP bar help

HP bars can be done rather easier if you use a ratio to calculate the coordinates of the hp left when you draw.

When you have 50/100 HP, the ratio of health would be 50% or 0.5 if you just calculate the division. The HP bar would only need to go halfway.

So we just scale the width of the bar by 50%.

Turing:

var width := 300
var HP, maxHP := 100
var ratio := HP / maxHP % = 0.5

The width of the remaining health would be:
Turing:

width * ratio % = 150


Also, if you're going to be drawing lots of bars, proceduralize it (make a procedure to draw an hp bar). You could pass the lower left coordinate as parameters. Also, instead of the top left in drawbox, use width and height so you have width handy to use for calculations.

(x2,y2) can easily be calculated with: (x1+w, y1+h)
childplay534




PostPosted: Tue Aug 16, 2011 5:33 pm   Post subject: Re: RE:Tower Defense HP bar help

TWizard @ Tue Aug 16, 2011 1:28 pm wrote:
Hmm, What you might want to do is when the enemy hp < 0 then hp = 0 and don't draw the enemy anymore, also if you wanted the hp bar to be the same as the hp have them both set at 100 and let them take the same amount of damage. at least that's what i'm getting from this.

I find this hard to follow, and your variables are shorten up to much to read, you should rename them for future notation for example: enemyY or enemyHP, castleHP ect, that way its easier to follow what variable may need some fixing.

If i did not cover what you needed please provide more information.


sry if it was hard to follow lol, i just wrote it in a rush so i didnt really name the variables. and no, i didnt want to stop at 0 hp, im talking about the hp bar at the bottom, thats the "true" hp, and i the hp bars on top of the mob is different value than the hp bar at the bottom, and i want to fix that... if that made sence

@Zren
thanks, that helped, sort of lol but i didnt get the width * ratio part, can you explain that a bit more?
Zren




PostPosted: Tue Aug 16, 2011 7:58 pm   Post subject: RE:Tower Defense HP bar help

It's the scaled width of the box representing the health left. Calculate the coordinates when you draw. Don't keep them in separate variables to increment. You may wish to store the ratio, as the hp may not change every frame. It's much easier to learn the concept when it's all together and break it into a subprogram later when it's all in one place. Ah screw it. Example time.

Turing:

var x, y := 0
var height := maxy
var width := maxx
var HP, maxHP := 100


var font := Font.New ("serif:12")
View.Set ("offscreenonly")
loop
    HP -= 1
    exit when HP < 0
   

    cls
    % Normal Box
    Draw.FillBox (x, y, x + width, y + height, black)

    % Scaled Box [width * ratio]
    var ratio : real := HP / maxHP
    var scaledWidth := round (width * ratio)
    Draw.FillBox (x, y, x + scaledWidth, y + height, green)
   
    Font.Draw ("HP: " + intstr (HP) + "/" + intstr (maxHP), 0, 60, font, white)
    Font.Draw ("Ratio: " + realstr (ratio, 4), 0, 40, font, white)
    Font.Draw ("Scaled Width: " + intstr (scaledWidth), 0, 20, font, white)
    View.Update ()
    delay (20)
end loop
childplay534




PostPosted: Tue Aug 16, 2011 9:42 pm   Post subject: Re: RE:Tower Defense HP bar help

Zren @ Tue Aug 16, 2011 7:58 pm wrote:
It's the scaled width of the box representing the health left. Calculate the coordinates when you draw. Don't keep them in separate variables to increment. You may wish to store the ratio, as the hp may not change every frame. It's much easier to learn the concept when it's all together and break it into a subprogram later when it's all in one place. Ah screw it. Example time.

Turing:

var x, y := 0
var height := maxy
var width := maxx
var HP, maxHP := 100


var font := Font.New ("serif:12")
View.Set ("offscreenonly")
loop
    HP -= 1
    exit when HP < 0
   

    cls
    % Normal Box
    Draw.FillBox (x, y, x + width, y + height, black)

    % Scaled Box [width * ratio]
    var ratio : real := HP / maxHP
    var scaledWidth := round (width * ratio)
    Draw.FillBox (x, y, x + scaledWidth, y + height, green)
   
    Font.Draw ("HP: " + intstr (HP) + "/" + intstr (maxHP), 0, 60, font, white)
    Font.Draw ("Ratio: " + realstr (ratio, 4), 0, 40, font, white)
    Font.Draw ("Scaled Width: " + intstr (scaledWidth), 0, 20, font, white)
    View.Update ()
    delay (20)
end loop


thank you very much. that did the trick
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: