Background
In the game Honor of Kings, at the start of the game, each hero has a health bar above their head. When damaged, the hero’s health decreases, and when close to death, the health bar turns red.
Similarly, when using the EasyX library to develop games, we often encounter the need to create health bars for characters. To facilitate future use, we can encapsulate this functionality in a function for easy invocation.
Draw_Blood
This function is used to draw the health bar.
void Draw_Blood(int x, int y, int blood_num, int blood_max);
Parameters
x The x-coordinate for drawing the health bar.y The y-coordinate for drawing the health bar.blood_num The current health value, which can be modified to change the health amount.blood_max The capacity of the health bar, which can also represent the maximum health value. An example of drawing is as follows: when health is greater than 50, the health bar color is green; when health is greater than 20, the color is yellow; and when health is less than 20, the color is red. This function can be further optimized in the future, such as adding scale values to the health bar.
The function to draw the health bar is as follows:
void Draw_Blood(int x, int y, int blood_num, int blood_max){ int bleed_w = 30; settextstyle(bleed_w - 10, 0, _T("黑体")); settextcolor(RGB(241, 151, 113)); outtextxy(x, y, _T("HP:")); setlinestyle(PS_SOLID, 2); setlinecolor(LIGHTGRAY); setfillcolor(WHITE); fillroundrect(x + 30, y + 5, x + 30 + blood_max, y + 15, 10, 10); if (blood_num >= 50) { setfillcolor(GREEN); } else if (blood_num <= 20) { setfillcolor(RED); } else { setfillcolor(YELLOW); } solidroundrect(x + 32, y + 7, x + 28 + blood_num, y + 12, 10, 10);}
When used, the health bar can move along with the hero, perfectly solving the health bar update issue. Below is a simple example.
The source code is as follows:
///////////////////////////////////////////////////// Program Name: Health Bar// Compilation Environment: Microsoft Visual Studio 2022, EasyX_20200315(beta)// Author: luoyh <[email protected]>// Last Modified: 2023-12-12//#include <graphics.h> #include <conio.h> // Function to draw a health barvoid Draw_Blood(int x, int y, int blood_num, int blood_max);int main(){ // Initialize graphics window initgraph(640, 480); setbkcolor(BLACK); cleardevice(); BeginBatchDraw(); int X = 0; int Y = 0; for (int i = 100; i > 0; i--) { Draw_Blood(X, Y, i, 100); // Draw health bar settextstyle(120, 0, _T("Wingdings")); wchar_t c = 0x4E; settextcolor(WHITE); outtextxy(X + 40, Y + 20, c); // Draw monster X += 1; Y += 1; FlushBatchDraw(); Sleep(100); cleardevice(); } EndBatchDraw(); _getch(); closegraph(); return 0;}void Draw_Blood(int x, int y, int blood_num, int blood_max){ int bleed_w = 30; settextstyle(bleed_w - 10, 0, _T("黑体")); settextcolor(RGB(241, 151, 113)); outtextxy(x, y, _T("HP:")); setlinestyle(PS_SOLID, 2); setlinecolor(LIGHTGRAY); setfillcolor(WHITE); fillroundrect(x + 30, y + 5, x + 30 + blood_max, y + 15, 10, 10); if (blood_num >= 50) { setfillcolor(GREEN); } else if (blood_num <= 20) { setfillcolor(RED); } else { setfillcolor(YELLOW); } solidroundrect(x + 32, y + 7, x + 28 + blood_num, y + 12, 10, 10);}