簡體   English   中英

如何在Windows Batch中隨機顯示?

[英]How to get random to display properly in Windows Batch?

我正在嘗試批量制作一個小游戲。 我正在隨機決定玩家受到的傷害,然后從當前的生命值中得出最終答案,然后顯示新的生命值。 但是,與其說:

 Your HP:       %phealth%

它會說類似:

Your HP:       20-32764 * (1 - 0 + 1) / 32768 + 0

這是到目前為止的全部代碼:

@echo off
set phealth=20
set shealth=50
set pdamage=0
set sdamage=0
:start
cls
echo -----------------
echo Your HP:       %phealth%
echo Monster HP:    %shealth%
echo -----------------
echo You are wielding an iron dagger.
echo Choices:
echo --------
echo [1] Strike at the slime
echo [2] Back away
set /p choice=You choose option:
if '%choice%'=='1' goto :choice1
if '%choice%'=='2' goto :choice2
goto start

:choice1
cls
set pdamage=%random% * (5 - 1 + 1) / 32768 + 1
set phealth=%phealth%-%pdamage%
set sdamage=%random% * (8 - 2 + 1) / 32768 + 2
set shealth=%shealth%-%sdamage%
echo -----------------
echo Your HP:       %phealth%
echo Monster HP:    %shealth%
echo -----------------
echo You strike at the slime.
echo The slime strikes you back.
PAUSE
cls
goto start

:choice2
cls
set pdamage=%random% * (1 - 0 + 1) / 32768 + 0
set phealth=%phealth%-%pdamage%
echo -----------------
echo Your HP:       %phealth%
echo Monster HP:    %shealth%
echo -----------------
echo You back away.
echo The slime strikes at you.
echo You took %pdamage% damage.
echo The slime took 0 damage.
PAUSE
cls
goto start

:end

要進行算術運算,請使用set /a例如:

set /a pdamage=%random% * (5 - 1 + 1) / 32768 + 1

如果要在范圍內(%random% %% maxrange) + 1使用(%random% %% maxrange) + 1


演示:

@ECHO OFF
SETLOCAL
ECHO roll 6-sided 5 times
FOR /l %%x IN (1,1,5) DO CALL :roll 6
ECHO roll 4-sided 5 times
FOR /l %%x IN (1,1,5) DO CALL :roll 4
ECHO roll 1-sided 5 times
FOR /l %%x IN (1,1,5) DO CALL :roll 1
GOTO :EOF

:roll
SET /a result=(%RANDOM% %% %1) + 1
ECHO %result%
GOTO :EOF

為我工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM