Health Bar Help

Hello! can somebody please help me with the following? I'm trying to make a vertical health bar in playdate pulp, that keeps track of 10 possible health points by using a full health, half health and no health tile to make this appear in five spaces.

I'm not that great with coding, so can somebody please help me with making this bar work by making it be more filled up with more health, and less with less health? Thanks!

There are various ways to do this, but here's some sample code you can adapt. Paste this into your player tile's script, and you can see it going up and down using the A and B buttons. I thought I'd include two sample scripts - the code in changeHealth_long is maybe a bit easier to read if you're new to pulpscript, while the code in changeHealth_short is shorter and more flexible. They both draw the bar in the same way though, just have different ways to loop through the tiles. In both cases, you can change healthBarX and healthBarY to change where the bar is drawn, and naturally you'll need to call the code differently, depending on how the player gains and loses health.

Hope it helps!

on load do
	health = 5
end

on enter do 
  call "changeHealth"
end

on confirm do
	if health<10 then
		health += 1
		call "changeHealth"
	end
end

on cancel do
	if health>0 then
		health -= 1
		call "changeHealth"
	end
end

on draw do
  label "{health}" at 20, 2
end

on changeHealth do
  call "changeHealth_long"
  call "changeHealth_short"
end

on changeHealth_long do
  
  
  healthToDraw = health
	tilesToDraw = 5
	
	healthBarX = 20
	healthBarY = 4
	
	// Calculate health tile to show
	if health == 1 then
	  health_bar_tile = "health_bar_1"
	elseif health >= 2 then
	  health_bar_tile = "health_bar_2"
	else
	  health_bar_tile = "health_bar_0"
	end
	
	tell healthBarX, healthBarY to
	  // sohw the right tile
	 swap health_bar_tile
	end
	
	// Move down a tile
	healthBarY += 1
	
	// Calculate health tile to show
	if health == 3 then
	  health_bar_tile = "health_bar_1"
	elseif health >= 4 then
	  health_bar_tile = "health_bar_2"
	else
	  health_bar_tile = "health_bar_0"
	end
	
	tell healthBarX, healthBarY to
	  // show the right tile
	 swap health_bar_tile
	end
	
	// Move down a tile
	healthBarY += 1
	
	
	// Calculate health tile to show
	if health == 5 then
	  health_bar_tile = "health_bar_1"
	elseif health >= 6 then
	  health_bar_tile = "health_bar_2"
	else
	  health_bar_tile = "health_bar_0"
	end
	
	tell healthBarX, healthBarY to
	  // sohw the right tile
	 swap health_bar_tile
	end
	
	// Move down a tile
	healthBarY += 1
	
	// Calculate health tile to show
	if health == 7 then
	  health_bar_tile = "health_bar_1"
	elseif health >= 8 then
	  health_bar_tile = "health_bar_2"
	else
	  health_bar_tile = "health_bar_0"
	end
	
	tell healthBarX, healthBarY to
	  // show the right tile
	 swap health_bar_tile
	end
	
	// Move down a tile
	healthBarY += 1
	
	// Calculate health tile to show
	if health == 9 then
	  health_bar_tile = "health_bar_1"
	elseif health >= 10 then
	  health_bar_tile = "health_bar_2"
	else
	  health_bar_tile = "health_bar_0"
	end
	
	tell healthBarX, healthBarY to
	  // sohw the right tile
	 swap health_bar_tile
	end
	
	// Move down a tile
	healthBarY += 1
end

on changeHealth_short do
  // Set up a variable to track how much health we've already drawn
	healthToDraw = health
	tilesToDraw = 5
	
	// tile position to start drawing bar at
	healthBarX = 22
	healthBarY = 4
	
	// move from one tile down to the next, working out for each if
	// we should draw the full, half or empty bar tile.
	// Then, we reduce healthToDraw by the health drawn in the tile
	while tilesToDraw > 0 do
	  if healthToDraw >= 2 then
	    tell healthBarX, healthBarY to
	      swap "health_bar_2"
	    end
	    healthToDraw -= 2
	  elseif healthToDraw == 1 then
	    tell healthBarX, healthBarY to
	      swap "health_bar_1"
	    end
	    healthToDraw -= 1
	  else
   	  tell healthBarX, healthBarY to
	      swap "health_bar_0"
	    end
	  end
	  healthBarY += 1
	  tilesToDraw -= 1
	end
	
end
2 Likes