Slick Forums

Discuss the Slick 2D Library
It is currently Sun May 26, 2013 7:50 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Mon Feb 20, 2012 12:24 am 
Offline

Joined: Sun Nov 20, 2011 9:21 pm
Posts: 25
Hi everybody, I'm pretty sure someone has already solved the problem I've encountered :).

I'm trying to create a health\mana\xp bar object but got stuck in the visualization aspect of it. What is the sane way of implementing variable length gradient rectangle? Creating the image from hundreds of small 1 mm long repeating chunks sounds nuts, but what are the other options?


Top
 Profile  
 
PostPosted: Mon Feb 20, 2012 12:54 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1173
Just render a part of the image (eg by using texture coordinates).

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
PostPosted: Mon Feb 20, 2012 2:45 am 
Offline
User avatar

Joined: Sat Apr 30, 2011 6:18 am
Posts: 21
This is how I am doing it in my game,

Code:

final int hpBarWidth = 100;
int currentHp = 6;
int maxHp = 10;

render {

g.fillRect(100, 100, ((hpBarWidth / maxHp) * currentHp), 24, Color.red);

}


_________________
You can never finish a program, only put it down.


Top
 Profile  
 
PostPosted: Mon Feb 20, 2012 8:34 am 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
Just throwing this out here.

In Christopher's example, make maxHP a float, otherwise it will be imprecise if the bar width is an odd number.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Mon Feb 20, 2012 11:44 pm 
Offline

Joined: Sun Dec 25, 2011 5:48 pm
Posts: 34
Copying Christophers code because im lazy:

Code:
final int hpBarWidth = 100;
int currentHp = 6;
int maxHp = 10;

render {
int r, g, b;

if (currentHp/maxHp >= 0.5) {
r = 255 - 255*(currentHp - maxHp/2)/(maxHp/2);
g = 255;
b = 0;
} else {
r = 255;
g = 255*currentHp/(maxHp/2);
b = 0;
}

g.fillRect(100, 100, ((hpBarWidth / maxHp) * currentHp), 24, new Color(r,g,b));

}


This will make the healthbar change from green to red as the health decreases.


Top
 Profile  
 
PostPosted: Wed Feb 22, 2012 8:16 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
Epicbo wrote:
This will make the healthbar change from green to red as the health decreases.


I did something similar.

Code:
      float currentHealthPercent = (Player.health / Player.maxHealth);
      greenvalue = (currentHealthPercent);
      redvalue = (1f - greenvalue);
      playerHealthBarColor.r = redvalue;
      playerHealthBarColor.g = greenvalue;


It's shorter and I can put it in update instead of render, and only putting g.setColor(playerHealthBarColor); before my g.fill(playerHealthBar); in my render method. It makes a very smooth change in color.


Top
 Profile  
 
PostPosted: Fri Feb 24, 2012 12:03 am 
Offline

Joined: Sun Nov 20, 2011 9:21 pm
Posts: 25
Guys, thanks for all the replies, that helped a lot :).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group