Comment afficher une barre de vie dans un environnement 2D.
Cela revient à afficher un ProgressBar dans un sprite pour lequel on va indiquer la position x & y d'affichage, une valeur à afficher et une valeur maximum.
■ Syntaxe : DisplayProgressBar(Sprite, x, y, Value, Maximum)
■ Exemple avec ce code qui affiche une barre de vie qui décroit de 250 à 0
Code : Tout sélectionner
Declare DisplayProgressBar(Sprite, x, y, Value, Maximum)
;Initialisation
If InitSprite()
InitKeyboard()
InitMouse()
EndIf
;Creation du screen
OpenWindow(0, 0, 0, 800, 600, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
;Création du sprite représentant la barre de vie et initialisation des variables
LifeMaxi = 250
Life.i = LifeMaxi
LifeBar = CreateSprite(#PB_Any, 200, 24)
Repeat
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event=0
ClearScreen(RGB(135, 206, 235))
;Décompte du compteur de vie
If Life > 0
Life-1
EndIf
;Affichage de la barre de vie
DisplayProgressBar(LifeBar, 20, 20, Life, LifeMaxi)
ExamineKeyboard()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Procedure DisplayProgressBar(Sprite, x, y, Value, Maximum)
Protected Width.i = SpriteWidth(Sprite)
Protected Height.i = SpriteHeight(Sprite)
Protected Coef.f = Width/Maximum
Protected Index.i = Value * Coef
Protected Text.s, TextWidth.i, TextHeight.i
StartDrawing(SpriteOutput(Sprite))
DrawingMode(#PB_2DDrawing_Transparent)
;Dessin du fond
Box(0, 0, Width, Height, RGB(128, 128, 128))
;Dessin de l'index
Box(0, 0, Index, Height, RGB(154, 205, 50))
;Dessin du pourcentage
Text = Str(100 * Index / Maximum / Coef) + "%"
TextWidth = TextWidth(Text)
TextHeight= TextHeight(Text)
DrawText((Width - TextWidth)/2, (Height - TextHeight)/2, Text, RGB(255, 255, 255))
;Dessin du contour
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, Width, Height, RGB(0, 0, 0))
StopDrawing()
;Affichage du sprite
DisplaySprite(Sprite, x, y)
EndProcedure