View Full Version : Percentage Bar

Jon W
03-14-2009, 11:33 PM
Hi, me and my friend are working a on PHP web based game. We ran into a few ideas that we are sure a possible, but not to sure how it would be done.

So one of our ideas was whenever fighting a enemy in the game it would show a Percentage bar of the enemies hit points. So say that the enemy has a base hit points of 20. We are trying to figure how we can make a dynamic bar that would take the percentage of the enemy hits points, and as the player takes away his hit points the bar would go down. Like whenever the enemy is fully maxed in hit points it would be totally green. As the enemy gets hit the bar every time you refresh the page would go down a percentage of the bar and then bar would turn red little bit by little bit. I've already figured out how we can have it turn from red to green, but my question is how can we figure out a percentage and have it take away slowly but surely?

Any ideas on this would be great. Suggestions are welcomed as well.

Jon W

03-15-2009, 12:03 AM
Have 1 image that represents 1% and add to it with every percent??

That could work....

Jon W
03-15-2009, 12:24 AM
Not to sure if that would work. Can you give me a better example?

03-15-2009, 02:00 AM
Make a page named graph.php:

// returns a PNG graph from the $_GET['per'] variable
$per = imagecreate(302,7);
$background = imagecolorallocate($per, 0xFF, 0xFF, 0xFF);
$foreground = imagecolorallocate($per, 0x00, 0x8A, 0x01);
$border = imagecolorallocate($per, 0x99, 0x99, 0x99);
if ($_GET['per'] > 0)
$grad = imagecreatefrompng("images/grad.png");
$per2 = imagecopy($per, $grad, 1, 1, 0, 0, ($_GET['per'] * 3), 5);
imagerectangle($per, 0, 0, 301, 6, $border);
header("Content-type: image/png");
imagepng($per, NULL, 5);

Call the page using:

echo '<img src="/graph.php?per='.$your_percentage_variable.'" />';

03-15-2009, 02:01 AM
It will return something like: