Drawstring health from an int to a string

Ok so in my game there is player1 and player2. They each have 10 health (protected int health = 10;)
I then used (String hp = String.valueOf(health); to make it a string and then drew it to the screen using drawstring. I want when the player gets shot, to subtract 1, then repaint it, can’t seem to make it work.

Any help?

To subtract, simply use the minus symbol, like this:

health = health - 1;

or

health -= 1

A more impressive way to show health is a health bar. Its not that much harder either.


float max = 10; //Maximum amount of health.  Make sure this is a float!
float health = 10; //Current amount of player health.  Make sure this is a float!
float barWidth = 200; //The length of the bar.  Make sure this is a float!

int x = 0, y = 0; //The x and y of the bar. Would probably be assigned in a constructor instead of here

public void render(Graphics g) {
     g.setColor(Color.red); //Color of the background of the bar
     g.fillRect(x, y, barWidth, 32); //Drawing of the background

     g.setColor(Color.green); //The color of the actual bar that changes
     g.fillRect(x, y, (health / max) * barWidth, 32); //This draw the bar. The fancy algorithm in the width area is important. Make sure that health variable and the max variable and the barWidth are floats because you need to have decimals in that equation!
    
     g.setColor(Color.black); //The color outline of the bar
     g.fillRect(x, y, barWidth, 32); //The drawing of the outline

     g.setColor(Color.black); //Color of the text of the health
     g.drawString("Health: " + health + "/" + max, x + 12, y); //The drawing of the text
}

public void update() {
      //In this method if you wanted the bar to follow a player you would set x = player.x; and y = player.y;
}

That should be it. Very nice stuff here and works flawlessly. Though if you add something like health packs the bar will go past the boundaries so you might want to do something like this:


if(health >= max) {
     //Just draw a full green bar instead of the dynamic bar above
}else{
     //Draw the bar dynamically like the code above

Feel free to leave a medal. :point: :stuck_out_tongue: ;D

Like the others have said, you just have to subtract the health.


int health = 10;

public void shoot()
{
     health -= 1;
}

public void paint(Graphics g)
{
     g.drawString(String.valueOf(health));
}

Ok so now I’m using


String hp = String.valueOf(health);
	     g.drawString(String.valueOf(health), 50, 150);

and it draws the HP, but its using the same hp in the same spot for both player 1 and 2. I can’t figure out how to say g.drawString(String.valueOf(player1.health), x,y);

So what exactly can I do now?

Well here:


String hp1 = String.valueOf(health1); //Health of player 1
String hp2 = String.valueOf(health2); //Health of player 2

g.drawString(hp1, 50, 150); //The method arguments go like this: String, x, y so if you change the x and y for each draw statement they will be in different spots
g.drawString(hp2, 75, 150); //As you can see the x is different from the statement above