PSP Lua Snippets - Drawing A Healthbar

Drawing a healthbar to the screen is pretty much a standard in most games. This can be a very confusing thing to a new programmer who doesn't know how to approach this. But it actually is a very simple process once you see some ways it can be done. Let's start coding.

First, let's create two colors. We will use red for the healthbar and white to print to the screen in text.

red = Color.new(255,0,0)
white = Color.new(255,255,255)

Next, we will create some arrays to store some healthbar and player information. This could be done with simple variables but we will use arrays since in a real game it would be easier to add more data types for the player and healthbar.
Our first array is for the healthbar. We will create the array and make a type to refer to the healthbar's y position.

healthbar = {}
healthbar.y = 10

Now, we will make an array for our player. In our example game all we need to store in the array is the player's health.

player = {}
player.health = 100

Now, we start our main loop, screen clearing, and button input code:

while true do
screen:clear()
pad = Controls.read()

Now we have started our main program loop, the part of our program which will repeat over and over checking for updates to the game.
Our next command will draw the healthbar to the screen. We will use the drawRect command to do this. This command simply draws a filled rectangle. Here's the code. Take a look and I will explain it.

screen:fillRect(10,10,player.health,healthbar.y,red)

This command can be used to draw a rectangle to an image. But instead we are drawing it to the screen instead.
The two 10's are the x and y position that we will draw this rectangle at.
The next two arguments are the arrays we created earlier.
player.health will be the width of the rectangle. So as our players health increases or decreases so will the bar.
healthbar.y will be the height of the rectangle, which we set to 10 earlier. This value will never change.
Although, you could change the y instead of x value if you wanted a vertical healthbar.

Next, we will print the players health to the screen underneath the healthbar, just so you can see the health visually with text and graphics as well.

screen:print(10,30,player.health,white)

Now, for this example game we are going to increase the health if the up button is held, and decrease the health if the down button is held. Here's the up button code:

if pad:up() and player.health < 100 then
player.health = player.health + 0.5
end

This code will increase the player's health by 0.5 if the up button is pressed, and if the player's health is less than 100 as well, since our health only goes to 100.
Now, let's do the down button.

if pad:down() and player.health > 0 then
player.health = player.health - 0.5 end

This code is the same as up except that the health will decrease if down is pressed, considering our health is greater than zero. Without the check for zero our health could go in to negative numbers!

Let's end our loop and wrap up this tutorial.

screen.flip()
screen.waitVblankStart()
end


Back To Snippet List

Please welcome stepbrother1988, our newest member.

Who's Online:

Total Members: 844
Total Posts: 13009
Total Topics: 1473
Total Categories: 7
Total Boards: 25

Recent Posts:

Re: chek it out i made front page of qj.net by chi-kitory
Re: chek it out i made front page of qj.net by Rick
Re: Hi everyone (remember me? :P) by Tdude_gamer
Re: Hi everyone (remember me? :P) by dan369
Hi everyone (remember me? :P) by Tdude_gamer
Re: Hows everyone doing? by osgeld
Re: Homebrew Idol 3: The Homebrew Horror Show by dan369
Problem on Tag game by andykd7


Copyright © 2006-2009 www.EvilMana.com All rights reserved.
EvilMana Logo by emcp and Charlie