Scratch health bar uses pure brush to realize health of health bar

We all know that the health bar system means that both players and enemies have a certain health value. When the health value reaches zero, they will die. The health bar is usually displayed as a bar and decreases based on the amount of damage done. Some games may allow players to restore their own health through the use of potions or other items, or use health-draining abilities to inflict greater damage.

In the game, when the character or enemy is attacked, the health bar will be reduced, just like being injured after being attacked. It would be cooler if you can add a health bar to the character in scratch games. In the scratch game, how do we achieve the effect of reducing the health bar when a character is attacked in scratch?

Original source of the article: https://blog.csdn.net/haigear/article/details/132617179

1. Preview effect

Let's take a look at the effect. Since I didn't add the attack code here, I directly used health to adjust the blood volume value. But it does not hinder the demonstration of the blood bar effect.Insert image description here

2. Design ideas

Generally there are two ideas, namely picture idea and real-time drawing idea:

1. Picture status ideas

One is to use a character and create N status pictures in the character, such as 10 pictures, representing ten states such as 10%, 20%...90%, 100%, etc. The disadvantage of this idea is that it is not precise, or in other words It's very troublesome, and the precision depends on the number of pictures.

2. Draw ideas in real time

As the name suggests, this idea is to use a brush to draw the length of the health bar according to the needs, so that the health bar can be accurately displayed, that is, the ratio of the existing blood volume to the total blood volume.

3. Design code

1. Health bar drawing module
Insert image description here
Here we use self-made building blocks to create a 6-parameter building block to facilitate the adjustment of the position, size and health of the building block. The instructions are as follows:

parameter name illustrate
x Initial x coordinate of health bar
y Initial y coordinate of health bar
len Initial length of health bar
width Initial width of health bar
health Current blood volume
max Total blood volume

2. Health bar drawing code
Insert image description here
3. Call of the health bar module
Insert image description here
The x and y here are the positions of the characters passed through global variables. You can get the initial positions of x and y according to your own needs.

If we want to make a scratch game with a better experience, we may need to systematically consider the health bar, supply and weapon attack system. If you are interested in the follow-up, we can do a demonstration teaching of the supply system.

The article may be updated at any time, please pay attention to the original source: https://blog.csdn.net/haigear/article/details/132617179

Guess you like

Origin blog.csdn.net/haigear/article/details/132617179