批量游戏中的健康栏

时间:2014-03-08 03:12:41

标签: batch-file progress-bar

我有一个批处理游戏,我需要为角色创建一个健康栏。我有以下变量:Max_HealthCurrent_HealthHealth_Percent

我想使用Health_Percent变量来创建这样的健康栏。

如果角色有: Max_Health = 500 Current_Health = 500 然后Health_Percent = 100 所以酒吧看起来像:

健康:100%=====。=====。=====。=====。=====

如果角色有: Max_Health = 500 Current_Health = 250 然后Health_Percent = 50 所以酒吧看起来像:

健康:50%=====。=====。===

每个'='表示 4%的健康状况,健康栏每隔20%与'.'分开

如果事情太困难,你可以省略'.'

提前谢谢。

2 个答案:

答案 0 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

rem For this example, take Max_Health and Current_Health from parameters
set /A Max_Health=%1, Current_Health=%2

set "bar======.=====.=====.=====.====="
set /A Health_Percent=Current_Health*100/Max_Health, barLen=Health_Percent*29/100

echo Max_Health=%Max_Health%, Current_Health=%Current_Health%, Health_Percent=%Health_Percent%
echo/
echo Health: %Health_Percent%%% !bar:~0,%barLen%!

输出示例:

C:\> test 500 500
Max_Health=500, Current_Health=500, Health_Percent=100

Health: 100% =====.=====.=====.=====.=====

C:\> test 500 250
Max_Health=500, Current_Health=250, Health_Percent=50

Health: 50% =====.=====.==

答案 1 :(得分:1)

提交我认识Aacini打败了我。但是这个代码可能要好得多,因为它可以进行错误检查并且可以更加自定义(以及他将.作为条形码的一部分包含在内的事实。另外,对于我的代码,你需要在调用之前计算百分比。很久以前,当我认为批量适合游戏开发时(很久很久以前)就已经使用过了。

这是我的代码:

Health.bat

@echo off
setlocal enabledelayedexpansion
set test=%~1
set /a health=%~1
set print=%~2

:: Remember to check if errorlevel is 1 after calling script
:: If not check which error occured based of its value.

if "%health%" EQU "" (
Echo Error: No Health Inputed!
Exit /b 2
)
if "%health%" NEQ "%test%" (
Echo Error: Please Input an integer!
Exit /b 3
)
if %health% GTR 100 (
Echo Error: Please Input a number within 0-100!
Exit /b 4 
)
if %health% LSS 0 (
Echo Error: Please Input a number within 0-100!
Exit /b 5
)
if "%print%" EQu "" (
set print=#
)
if exist tmp.e del tmp.e
<nul set /p=%print% 1>nul 2>tmp.e
set /p wrong=<tmp.e
del tmp.e
if "%worng%" NEQ "" set print=#


<nul set /p"=Health: %health%%%  "
set /a repeat=health/20
set /a remain=health%%20
for /l %%a in (1, 1, %repeat%) do (
<nul set /p=%print%%print%%print%%print%%print%
if %%a LSS !repeat! (
<nul set /p=-
)
)
if %remain% EQU 0 Goto :END

<nul set /p=-
set /a repeat=remain/4
set /a round=remain%%4
for /l %%a in (1, 1, %repeat%) do (
<nul set /p=%print%
)

if %round% GTR 0 (
<nul set /p=%print%
)

:END
Echo.
endlocal
exit /b 1

简单使用:

C:\> Health.bat 43
Health: 43% #####-#####-#

C:\> Health.bat 50 $
Health: 50% $$$$$-$$$$$-$$$

C:\> REM ^B is avhieved by Keystroke of "Ctrl+B"
C:\> Health.bat 67 ^B
Health: 67% ☻☻☻☻☻-☻☻☻☻☻-☻☻☻☻☻-☻☻

C:\> REM Normally if you tried to use an "&" it would cause lots of errors
C:\> Rem However I have speacial code to check for that.
C:\> Health.bat 6 &
Health: 6% ##

C:\> Rem and the best thing is now you can have multichar hp blocks
C:\>Health.bat 26 ^V^U^V
Health: 26%  ▬§▬▬§▬▬§▬▬§▬▬§▬-▬§▬▬§▬

它会做你想做的事。

基本参数是:

 Health [Health Percent]
 Health [Health Percent] [HP Block]

注意我将默认健康栏更改为#####-#####-##,因为它看起来比-----.--好很多。

莫纳