Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Displaying HP

Credits go to North~ for helping out!
This thread is locked; no one can reply to it. rss feed Print
Displaying HP
HappyBoy
Member #13,462
September 2011

How do I display numeric HP, like in a Final Fantasy RPG game?
Like, I know how to keep count of HP as an integer variable, and I even know how to display a health bar that can go up or down based on my current HP. But, I don't know how to display HP as a number on the screen, as is done in most turn-based fighting games.

NOTE: I am a super, super noob, so please be sure that any answers or example codes are spoon-fed to me painstakingly clearly and fully; please do not underestimate my noobness.
Thank you so much in advance!

Trent Gamblin
Member #261
April 2000
avatar

textprintf_ex or al_draw_textf depending on your Allegro version.

Niunio
Member #1,975
March 2002
avatar

BTW, what programming language are you using?::)

-----------------
Current projects: Allegro.pas | MinGRo

North~
Member #12,192
August 2010

Trent said:

textprintf_ex or al_draw_textf depending on your Allegro version.

Considering that we shouldn't "underestimate your noobyness", I'll assume you're using A4 since A5 is more complicated to setup so, with that being said:

int HP = 100;

textprintf_ex(screen,font,5,5,makecol(255,255,255),-1,"Health: %d",HP);

A little not-so-pseudo-code for you to use to your hearts content. Also. Don't blit to the screen. Use double buffering.

#SelectExpand
1BITMAP* Buffer = create_bitmap(SCREEN_W,SCREEN_H); 2 3//Loop 4while(1) 5{ 6clear_bitmap(Buffer); 7 8// Blit whatever here to the buffer. 9 10blit(Buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); 11}

--------------------------------------------------
http://blog.wolfire.com/2009/04/always-initialize-your-memory/

If this is possible if you don't correctly initialize memory, it should be a priority of the highest order.

Trent Gamblin
Member #261
April 2000
avatar

DOUBLE BUFFER BONUS!!!

North~
Member #12,192
August 2010

DOUBLE BUFFER BONUS!!!

I'm not the best at problem solving or programming concept in general so when someone has a problem that I actually can answer, I go all out with it xD

--------------------------------------------------
http://blog.wolfire.com/2009/04/always-initialize-your-memory/

If this is possible if you don't correctly initialize memory, it should be a priority of the highest order.

Trent Gamblin
Member #261
April 2000
avatar

Cool man.

HappyBoy
Member #13,462
September 2011

I'm using Allegro 4.2.3. Here's my code right now:
-----------------------------------------------------------
#include <allegro.h>
#include <iostream>
#include <cstring>
using namespace std ;

int main ()
{
allegro_init ();
install_keyboard ();
install_mouse ();
set_color_depth (16);
set_gfx_mode (GFX_AUTODETECT_WINDOWED , 640, 480, 0, 0);
set_window_title ("HP in RPG"); // sets title of

BITMAP *Buffer = create_bitmap (800, 600);

string hp1 = "100" ;

textout_ex(Buffer , font , hp1.c_str() , 10 , 10 , makecol(255,255,255) , makecol(0,0,0));

textprintf_ex(Buffer , font , 10 , 30 , makecol(255,255,255) , -1 , "HP: %s" , hp1.c_str());

blit (Buffer , screen , 0, 0, 0, 0, 640, 480);
rest (40) ;
clear_bitmap (Buffer);

readkey (); // just waits for user to press any key to continue
return 0;
}
END_OF_MAIN ()
-----------------------------------------------------------

Notice that we are displaying HP. But, it's a string rather than an integer, so I don't know how to make this value go down when I get hit. Lots of games display your HP, so this must be simple, no?

Trent Gamblin
Member #261
April 2000
avatar

Use %d instead of %s and pass an int.

HappyBoy
Member #13,462
September 2011

Solved! Thank you so much, Norther! Hugs Norther

PS. Huh??? What is Double Buffering? Could you please put it into my updated code?

-----------------
#include <allegro.h>
#include <iostream>
#include <cstring>
using namespace std ;

int main ()
{
allegro_init ();
install_keyboard ();
install_mouse ();
set_color_depth (16);
set_gfx_mode (GFX_AUTODETECT_WINDOWED , 640, 480, 0, 0);
set_window_title ("HP in RPG"); // sets title of

BITMAP *Buffer = create_bitmap (800, 600);
bool done = false ;
int HP = 100;

while (done == false)
{
if (key[KEY_ESC])
done = true ;
if (key[KEY_DOWN])
HP -- ;
if (key[KEY_UP])
HP ++ ;

textprintf_ex(Buffer,font,5,5,makecol(255,255,255),-1,"Health: %d", HP );
blit (Buffer , screen , 0, 0, 0, 0, 640, 480);
rest (40) ;

clear_bitmap (Buffer);
}

readkey (); // just waits for user to press any key to continue
return 0;
}
END_OF_MAIN ()
----------

Please do not answer in the format that Trent Gamblin did. I had no idea what he meant. As I said, I am a complete n00b and will not understand what you mean unless I see the code on action.
Thanks again so much, Norther.

Trent Gamblin
Member #261
April 2000
avatar

Funny how you will not understand, yet I was the only one to mention %d which is what you ended up using. Stop being a lazy prick or maybe people like me won't help you next time.

For the record

HappyBoy said:

be sure that any answers or example codes are spoon-fed to me painstakingly clearly and fully

This is being lazy.

HappyBoy said:

Please do not answer in the format that Trent Gamblin did.

And this is being a prick.

Jonatan Hedborg
Member #4,886
July 2004
avatar

Why are you even helping this entitled-acting child? This might be the worst case of noob-rudeness I've seen on this forum, and we've had a LOT.

Judging by his answers, he lacks the basic level of engagement and/or willingness to search for answers to learn programming, so stop wasting your time.

Tobias Dammers
Member #2,604
August 2002
avatar

Read this:

http://www.catb.org/~esr/faqs/smart-questions.html

This is actually more help than you deserve after showing such attitude.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Matthew Leverton
Supreme Loser
January 1999
avatar

This might be the worst case of noob-rudeness I've seen on this forum, and we've had a LOT.

You have to appreciate his honesty.

This is actually more help than you deserve after showing such attitude.

Attitude? Have a lot of thin skin?

That page is incredibly useless. The intersection of the people willing to read all of that and the people who ask dumb questions is empty.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

North~
Member #12,192
August 2010

Read this:
http://www.catb.org/~esr/faqs/smart-questions.html
This is actually more help than you deserve after showing such attitude.

I still remember that raging frenchman who came here and asked a beginner level question. When we responded with answers about googling and searching the forum, his response contained a commodious amount of profanity, vulgarity, and what I like to call the Angry Ghetto Six Year Old Syndrome :x

@OP: There's only one thing wrong with the double buffering you have now and that's the fact that you made the buffer (800,600) bigger than the screen (640,480). they should be the same size (unless you're doing some sort of software map scrolling, in which case I don't think that's what you're getting at.)

--------------------------------------------------
http://blog.wolfire.com/2009/04/always-initialize-your-memory/

If this is possible if you don't correctly initialize memory, it should be a priority of the highest order.

Go to: